Skip to content

Commit

Permalink
Add more type hints to class properties
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jun 8, 2024
1 parent 9dab336 commit d89d113
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 71 deletions.
7 changes: 1 addition & 6 deletions src/Cms/Ingredients.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@
*/
class Ingredients
{
/**
* @var array
*/
protected $ingredients = [];

/**
* Creates a new ingredient collection
*/
public function __construct(array $ingredients)
public function __construct(protected array $ingredients)
{
$this->ingredients = $ingredients;
}
Expand Down
12 changes: 3 additions & 9 deletions src/Cms/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,18 @@ class Pagination extends BasePagination
{
/**
* Pagination method (param, query, none)
*
* @var string
*/
protected $method;
protected string $method;

/**
* The base URL
*
* @var string
*/
protected $url;
protected Uri $url;

/**
* Variable name for query strings
*
* @var string
*/
protected $variable;
protected string $variable;

/**
* Creates the pagination object. As a new
Expand Down
4 changes: 1 addition & 3 deletions src/Filesystem/Mime.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ class Mime
{
/**
* Extension to MIME type map
*
* @var array
*/
public static $types = [
public static array $types = [
'ai' => 'application/postscript',
'aif' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
Expand Down
4 changes: 1 addition & 3 deletions src/Sane/DomHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ class DomHandler extends Handler

/**
* Allowed hostnames for HTTP(S) URLs
*
* @var array|true
*/
public static array|bool $allowedDomains = true;
public static array|true $allowedDomains = true;

/**
* Whether URLs that begin with `/` should be allowed even if the
Expand Down
4 changes: 1 addition & 3 deletions src/Sane/Svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,8 @@ class Svg extends Xml

/**
* Allowed hostnames for HTTP(S) URLs
*
* @var array|true
*/
public static array|bool $allowedDomains = [];
public static array|true $allowedDomains = [];

/**
* Associative array of all allowed namespace URIs
Expand Down
57 changes: 19 additions & 38 deletions src/Toolkit/Dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,30 @@ class Dom
{
/**
* Cache for the HTML body
*
* @var \DOMElement|null
*/
protected $body;

/**
* The original input code as
* passed to the constructor
*
* @var string
*/
protected $code;
protected DOMElement|null $body;

/**
* Document object
*
* @var \DOMDocument
*/
protected $doc;

/**
* Document type (`'HTML'` or `'XML'`)
*
* @var string
*/
protected $type;
protected DOMDocument $doc;

/**
* Class constructor
*
* @param string $code XML or HTML code
* @param string $code original XML or HTML input
* @param string $type Document type (`'HTML'` or `'XML'`)
*/
public function __construct(string $code, string $type = 'HTML')
{
$this->code = $code;
public function __construct(
protected string $code,
protected string $type = 'HTML'
) {
$this->type = strtoupper($type);
$this->doc = new DOMDocument();

$loaderSetting = null;

// switch to "user error handling"
$intErrorsSetting = libxml_use_internal_errors(true);
$use_errors = libxml_use_internal_errors(true);

$this->type = strtoupper($type);
if ($this->type === 'HTML') {
// ensure proper parsing for HTML snippets
if (preg_match('/<(html|body)[> ]/i', $code) !== 1) {
Expand All @@ -82,13 +62,13 @@ public function __construct(string $code, string $type = 'HTML')

// the loadHTML() method expects ISO-8859-1 by default;
// force parsing as UTF-8 by injecting an XML declaration
$xmlDeclaration = 'encoding="UTF-8" id="' . Str::random(10) . '"';
$load = $this->doc->loadHTML('<?xml ' . $xmlDeclaration . '>' . $code);
$xml = 'encoding="UTF-8" id="' . Str::random(10) . '"';
$load = $this->doc->loadHTML('<?xml ' . $xml . '>' . $code);

// remove the injected XML declaration again
$pis = $this->query('//processing-instruction()');
foreach (iterator_to_array($pis, false) as $pi) {
if ($pi->data === $xmlDeclaration) {
if ($pi->data === $xml) {
static::remove($pi);
}
}
Expand All @@ -104,7 +84,7 @@ public function __construct(string $code, string $type = 'HTML')
// get one error for use below and reset the global state
$error = libxml_get_last_error();
libxml_clear_errors();
libxml_use_internal_errors($intErrorsSetting);
libxml_use_internal_errors($use_errors);

if ($load !== true) {
$message = 'The markup could not be parsed';
Expand Down Expand Up @@ -169,8 +149,7 @@ public static function isAllowedAttr(
DOMAttr $attr,
array $options
): true|string {
$options = static::normalizeSanitizeOptions($options);

$options = static::normalizeSanitizeOptions($options);
$allowedTags = $options['allowedTags'];

// check if the attribute is in the list of global allowed attributes
Expand Down Expand Up @@ -219,8 +198,7 @@ public static function isAllowedGlobalAttr(
DOMAttr $attr,
array $options
): true|string {
$options = static::normalizeSanitizeOptions($options);

$options = static::normalizeSanitizeOptions($options);
$allowedAttrs = $options['allowedAttrs'];

if ($allowedAttrs === true) {
Expand Down Expand Up @@ -280,7 +258,10 @@ public static function isAllowedUrl(

// allow site-internal URLs that didn't match the
// protocol-relative check above
if (mb_substr($url, 0, 1) === '/' && $options['allowHostRelativeUrls'] !== true) {
if (
mb_substr($url, 0, 1) === '/' &&
$options['allowHostRelativeUrls'] !== true
) {
// if a CMS instance is active, only allow the URL
// if it doesn't point outside of the index URL
if ($kirby = App::instance(null, true)) {
Expand Down
8 changes: 2 additions & 6 deletions src/Toolkit/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,13 @@ class Html extends Xml
* ```php
* Html::$void = ' />'
* ```
*
* @var string
*/
public static $void = '>';
public static string $void = '>';

/**
* List of HTML tags that are considered to be self-closing
*
* @var array
*/
public static $voidList = [
public static array $voidList = [
'area',
'base',
'br',
Expand Down
4 changes: 1 addition & 3 deletions src/Toolkit/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ class Xml

/**
* Closing string for void tags
*
* @var string
*/
public static $void = ' />';
public static string $void = ' />';

/**
* Generates a single attribute or a list of attributes
Expand Down

0 comments on commit d89d113

Please sign in to comment.