From e8fe4a1a4de08fc0402744cc8e824a1809cb1e82 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 22 Nov 2024 09:52:07 +0100 Subject: [PATCH 1/2] PHPStan fixes --- phpstan-baseline.neon | 12 ------------ src/Environment.php | 7 ++++--- src/ExpressionParser.php | 6 ++---- src/ExtensionSet.php | 12 ++++++++---- src/Runtime/LoopIterator.php | 5 ++++- 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 1121ae1b235..facf2c1361c 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -6,20 +6,8 @@ parameters: count: 1 path: src/Extension/CoreExtension.php - - # Avoid BC-break - message: '#^Constructor of class Twig\\Node\\ForNode has an unused parameter \$ifexpr\.$#' - identifier: constructor.unusedParameter - count: 1 - path: src/Node/ForNode.php - - # 2 parameters will be required message: '#^Method Twig\\Node\\IncludeNode\:\:addGetTemplate\(\) invoked with 2 parameters, 1 required\.$#' identifier: arguments.count count: 1 path: src/Node/IncludeNode.php - - - # int|string will be supported in 4.x - message: '#^PHPDoc tag @param for parameter $name with type int|string is not subtype of native type string\.$#' - identifier: parameter.phpDocType - count: 5 - path: src/Node/Node.php diff --git a/src/Environment.php b/src/Environment.php index 9bce904e09f..fa372d28f9d 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -26,6 +26,7 @@ use Twig\Loader\ArrayLoader; use Twig\Loader\ChainLoader; use Twig\Loader\LoaderInterface; +use Twig\Node\Expression\AbstractExpression; use Twig\Node\Expression\Binary\AbstractBinary; use Twig\Node\Expression\Unary\AbstractUnary; use Twig\Node\ModuleNode; @@ -54,7 +55,7 @@ class Environment private LoaderInterface $loader; private bool $debug; private bool $autoReload; - private CacheInterface|string|false $cache; + private CacheInterface $cache; private ?Lexer $lexer = null; private ?Parser $parser = null; private ?Compiler $compiler = null; @@ -833,7 +834,7 @@ public function resetGlobals(): void /** * @internal * - * @return array}> + * @return array}> */ public function getUnaryOperators(): array { @@ -843,7 +844,7 @@ public function getUnaryOperators(): array /** * @internal * - * @return array, associativity: ExpressionParser::OPERATOR_*}> + * @return array, associativity: ExpressionParser::OPERATOR_*}> */ public function getBinaryOperators(): array { diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index d5fcbd01a43..8d9a9d9f11a 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -25,9 +25,7 @@ use Twig\Node\Expression\MacroReferenceExpression; use Twig\Node\Expression\TestExpression; use Twig\Node\Expression\Unary\AbstractUnary; -use Twig\Node\Expression\Unary\NegUnary; use Twig\Node\Expression\Unary\NotUnary; -use Twig\Node\Expression\Unary\PosUnary; use Twig\Node\Expression\Unary\SpreadUnary; use Twig\Node\Expression\Variable\AssignContextVariable; use Twig\Node\Expression\Variable\ContextVariable; @@ -51,9 +49,9 @@ class ExpressionParser public const OPERATOR_LEFT = 1; public const OPERATOR_RIGHT = 2; - /** @var array}> */ + /** @var array}> */ private array $unaryOperators; - /** @var array, associativity: self::OPERATOR_*}> */ + /** @var array, associativity: self::OPERATOR_*}> */ private array $binaryOperators; private array $precedenceChanges = []; private bool $deprecationCheck = true; diff --git a/src/ExtensionSet.php b/src/ExtensionSet.php index 0cc57bbc3db..d6d6d675831 100644 --- a/src/ExtensionSet.php +++ b/src/ExtensionSet.php @@ -48,9 +48,13 @@ final class ExtensionSet private array $functions; /** @var array */ private array $dynamicFunctions; - /** @var array}> */ + /** + * @var array}> + */ private array $unaryOperators; - /** @var array, associativity: ExpressionParser::OPERATOR_*}> */ + /** + * @var array, associativity: ExpressionParser::OPERATOR_*}> + */ private array $binaryOperators; /** @var array|null */ private ?array $globals = null; @@ -389,7 +393,7 @@ public function getTest(string $name): ?TwigTest } /** - * @return array}> + * @return array}> */ public function getUnaryOperators(): array { @@ -401,7 +405,7 @@ public function getUnaryOperators(): array } /** - * @return array, associativity: ExpressionParser::OPERATOR_*}> + * @return array, associativity: ExpressionParser::OPERATOR_*}> */ public function getBinaryOperators(): array { diff --git a/src/Runtime/LoopIterator.php b/src/Runtime/LoopIterator.php index af20fcfd756..a1112a83ff4 100644 --- a/src/Runtime/LoopIterator.php +++ b/src/Runtime/LoopIterator.php @@ -45,11 +45,14 @@ public function __construct($seq) $this->seq = new \ArrayIterator($seq); } elseif ($seq instanceof \IteratorAggregate) { do { + /** @var \Iterator $seq */ $seq = $seq->getIterator(); } while ($seq instanceof \IteratorAggregate); $this->seq = $seq; - } elseif (is_iterable($seq)) { + } elseif ($seq instanceof \Iterator) { $this->seq = $seq; + } elseif (is_iterable($seq)) { + $this->seq = new \IteratorIterator($seq); } else { $this->seq = new \EmptyIterator(); } From 97ef15c4a0fc83727ff8e98fc78e4f50bfa0b49d Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 22 Nov 2024 10:34:39 +0100 Subject: [PATCH 2/2] Simplify --- src/Runtime/LoopIterator.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Runtime/LoopIterator.php b/src/Runtime/LoopIterator.php index a1112a83ff4..db9c0ce7174 100644 --- a/src/Runtime/LoopIterator.php +++ b/src/Runtime/LoopIterator.php @@ -49,10 +49,9 @@ public function __construct($seq) $seq = $seq->getIterator(); } while ($seq instanceof \IteratorAggregate); $this->seq = $seq; - } elseif ($seq instanceof \Iterator) { - $this->seq = $seq; } elseif (is_iterable($seq)) { - $this->seq = new \IteratorIterator($seq); + /** @var \Iterator $seq */ + $this->seq = $seq; } else { $this->seq = new \EmptyIterator(); }