Skip to content

Commit 537e788

Browse files
committed
Address phpstan errors
1 parent 28ec869 commit 537e788

15 files changed

+23
-93
lines changed

phpstan.neon.dist

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ includes:
44
parameters:
55
level: 5
66
inferPrivatePropertyTypeFromConstructor: true
7+
treatPhpDocTypesAsCertain: false
78
paths:
89
- include/
910
- src/
1011
ignoreErrors:
1112
- '#^PHPDoc tag @throws with type AmpProject\\Exception\\FailedRemoteRequest is not subtype of Throwable$#'
1213
- '#^Parameter \#1 (\$exception_handler|\$callback) of function set_exception_handler expects#'
13-
14-
# @see https://github.com/phpstan/phpstan/issues/5655
15-
- '#^PHPDoc tag @var for constant (?:.*) with type array(?:.*) is not subtype of value array(?:.*)\.$#'
1614
excludePaths:
1715
analyse:
1816
- src/FakeEnum.php
@@ -22,3 +20,4 @@ parameters:
2220
- src/Validator/ValidationErrorCollection.php
2321
- src/Validator/ValidationHandler.php
2422
- src/Validator/ValidatorRules.php
23+
- src/Validator/Spec

src/Cli/AmpExecutable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ protected function setup(Options $options)
3838
{
3939
foreach (self::COMMAND_CLASSES as $commandClass) {
4040
/** @var Command $command */
41+
// @phpstan-ignore varTag.nativeType
4142
$command = new $commandClass($this);
4243

4344
$command->register($options);

src/Cli/Options.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,6 @@ private function readPHPArgv()
542542
}
543543

544544
if (
545-
is_array($_SERVER)
546-
&&
547545
array_key_exists('argv', $_SERVER)
548546
&&
549547
is_array($_SERVER['argv'])

src/CssLength.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function validate($allowAuto, $allowFluid)
111111
$pattern = '/^(?<numeral>\d+(?:\.\d+)?)(?<unit>px|em|rem|vh|vw|vmin|vmax)?$/';
112112
if (preg_match($pattern, $this->attrValue, $match)) {
113113
$this->isValid = true;
114-
$this->numeral = isset($match['numeral']) ? (float)$match['numeral'] : $this->numeral;
114+
$this->numeral = (float)$match['numeral'];
115115
$this->unit = $match['unit'] ?? $this->unit;
116116
}
117117
}

src/Dom/Document.php

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,7 @@ public function saveHTMLFragment(?DOMNode $node = null)
434434
}
435435
}
436436

437-
if (null === $node || PHP_VERSION_ID >= 70300) {
438-
$html = parent::saveHTML($node);
439-
} else {
440-
$html = $this->extractNodeViaFragmentBoundaries($node);
441-
}
437+
$html = parent::saveHTML($node);
442438

443439
foreach ($filtersInReverse as $filter) {
444440
if ($filter instanceof AfterSaveFilter) {
@@ -474,43 +470,6 @@ private function insertMissingCharset()
474470
$this->head->insertBefore($charset, $this->head->firstChild);
475471
}
476472

477-
/**
478-
* Extract a node's HTML via fragment boundaries.
479-
*
480-
* Temporarily adds fragment boundary comments in order to locate the desired node to extract from
481-
* the given HTML document. This is required because libxml seems to only preserve whitespace when
482-
* serializing when calling DOMDocument::saveHTML() on the entire document. If you pass the element
483-
* to DOMDocument::saveHTML() then formatting whitespace gets added unexpectedly. This is seen to
484-
* be fixed in PHP 7.3, but for older versions of PHP the following workaround is needed.
485-
*
486-
* @param DOMNode $node Node to extract the HTML for.
487-
* @return string Extracted HTML string.
488-
*/
489-
private function extractNodeViaFragmentBoundaries(DOMNode $node)
490-
{
491-
$boundary = $this->uniqueIdManager->getUniqueId('fragment_boundary');
492-
$startBoundary = $boundary . ':start';
493-
$endBoundary = $boundary . ':end';
494-
$commentStart = $this->createComment($startBoundary);
495-
$commentEnd = $this->createComment($endBoundary);
496-
497-
$node->parentNode->insertBefore($commentStart, $node);
498-
$node->parentNode->insertBefore($commentEnd, $node->nextSibling);
499-
500-
$pattern = '/^.*?'
501-
. preg_quote("<!--{$startBoundary}-->", '/')
502-
. '(.*)'
503-
. preg_quote("<!--{$endBoundary}-->", '/')
504-
. '.*?\s*$/s';
505-
506-
$html = preg_replace($pattern, '$1', parent::saveHTML());
507-
508-
$node->parentNode->removeChild($commentStart);
509-
$node->parentNode->removeChild($commentEnd);
510-
511-
return $html;
512-
}
513-
514473
/**
515474
* Normalize the document structure.
516475
*
@@ -1144,16 +1103,10 @@ private function instantiateFilter($filterClass)
11441103
foreach ($parameters as $parameter) {
11451104
$dependencyType = null;
11461105

1147-
// The use of `ReflectionParameter::getClass()` is deprecated in PHP 8, and is superseded
1148-
// by `ReflectionParameter::getType()`. See https://github.com/php/php-src/pull/5209.
1149-
if (PHP_VERSION_ID >= 70100) {
1150-
if ($parameter->getType()) {
1151-
/** @var ReflectionNamedType $returnType */
1152-
$returnType = $parameter->getType();
1153-
$dependencyType = new ReflectionClass($returnType->getName());
1154-
}
1155-
} else {
1156-
$dependencyType = $parameter->getClass();
1106+
if ($parameter->getType()) {
1107+
/** @var ReflectionNamedType $returnType */
1108+
$returnType = $parameter->getType();
1109+
$dependencyType = new ReflectionClass($returnType->getName());
11571110
}
11581111

11591112
if ($dependencyType === null) {

src/Dom/Document/Filter/MustacheScriptTemplates.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ static function ($matches) use ($mustacheTagPlaceholders) {
127127
// entities. In the case of a URL value like '/foo/?bar=1&baz=2' the result is a warning for an
128128
// unterminated entity reference "baz". When the attribute value is updated via setAttribute() this
129129
// same problem does not occur, so that is why the following is used.
130+
// @phpstan-ignore method.notFound
130131
$attribute->parentNode->setAttribute($attribute->nodeName, $value);
131132

132133
$this->mustacheTagsReplaced = true;

src/Dom/Element.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function addInlineStyle($style, $prepend = false)
7777
* @return DOMAttr|false The new or modified DOMAttr or false if an error occurred.
7878
* @throws MaxCssByteCountExceeded If the allowed max byte count is exceeded.
7979
*/
80-
public function setAttribute($name, $value)
80+
public function setAttribute($name, $value) // @phpstan-ignore return.unusedType
8181
{
8282
// Make sure $value is always a string and not null.
8383
$value = strval($value);
@@ -191,7 +191,7 @@ public static function mergeAmpActions($first, $second)
191191
$matches = [];
192192
$results = preg_match_all(self::AMP_EVENT_ACTIONS_REGEX_PATTERN, $eventActionString, $matches);
193193

194-
if (! $results || ! isset($matches['event'])) {
194+
if (! $results) {
195195
continue;
196196
}
197197

@@ -209,7 +209,7 @@ static function ($actions) use (&$actionsArray) {
209209
$matches = [];
210210
$results = preg_match_all(self::AMP_ACTION_REGEX_PATTERN, $actions, $matches);
211211

212-
if (! $results || ! isset($matches['action'])) {
212+
if (! $results) {
213213
$actionsArray[] = $actions;
214214
return;
215215
}

src/Html/Parser/ParsedTag.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,6 @@ public function __construct($tagName, $alternatingAttributes = [])
7777

7878
// Sort the attribute array by (lower case) name.
7979
usort($this->attributes, function (ParsedAttribute $a, ParsedAttribute $b) {
80-
if (PHP_MAJOR_VERSION < 7 && $a->name() === $b->name()) {
81-
// Hack required for PHP 5.6, as it does not maintain stable order for equal items.
82-
// See https://bugs.php.net/bug.php?id=69158.
83-
// To get around this, we compare the index within $this->attributes instead to maintain existing order.
84-
return strcmp(array_search($a, $this->attributes, true), array_search($b, $this->attributes, true));
85-
}
86-
8780
return strcmp($a->name(), $b->name());
8881
});
8982

src/Optimizer/ImageDimensions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ final class ImageDimensions
4747
/**
4848
* Unit of the width of the image.
4949
*
50-
* @var int|float|string|null
50+
* @var string|null
5151
*/
5252
private $widthUnit;
5353

5454
/**
5555
* Unit of the height of the image.
5656
*
57-
* @var int|float|string|null
57+
* @var string|null
5858
*/
5959
private $heightUnit;
6060

src/Optimizer/TransformationEngine.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,10 @@ private function getTransformerDependencies($transformerClass)
127127
foreach ($constructor->getParameters() as $parameter) {
128128
$dependencyType = null;
129129

130-
// The use of `ReflectionParameter::getClass()` is deprecated in PHP 8, and is superseded
131-
// by `ReflectionParameter::getType()`. See https://github.com/php/php-src/pull/5209.
132-
if (PHP_VERSION_ID >= 70100) {
133-
if ($parameter->getType()) {
134-
/** @var \ReflectionNamedType $returnType */
135-
$returnType = $parameter->getType();
136-
$dependencyType = new ReflectionClass($returnType->getName());
137-
}
138-
} else {
139-
$dependencyType = $parameter->getClass();
130+
if ($parameter->getType()) {
131+
/** @var \ReflectionNamedType $returnType */
132+
$returnType = $parameter->getType();
133+
$dependencyType = new ReflectionClass($returnType->getName());
140134
}
141135

142136
if ($dependencyType === null) {

0 commit comments

Comments
 (0)