Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PHPStan for Twig 4.x #4466

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 4 additions & 3 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +55,7 @@ class Environment
private LoaderInterface $loader;
private bool $debug;
private bool $autoReload;
private CacheInterface|string|false $cache;
fabpot marked this conversation as resolved.
Show resolved Hide resolved
private CacheInterface $cache;
private ?Lexer $lexer = null;
private ?Parser $parser = null;
private ?Compiler $compiler = null;
Expand Down Expand Up @@ -833,7 +834,7 @@ public function resetGlobals(): void
/**
* @internal
*
* @return array<string, array{precedence: int, class: class-string<AbstractUnary>}>
* @return array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class: class-string<AbstractExpression>}>
*/
public function getUnaryOperators(): array
{
Expand All @@ -843,7 +844,7 @@ public function getUnaryOperators(): array
/**
* @internal
*
* @return array<string, array{precedence: int, class: class-string<AbstractBinary>, associativity: ExpressionParser::OPERATOR_*}>
* @return array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class: class-string<AbstractExpression>, associativity: ExpressionParser::OPERATOR_*}>
*/
public function getBinaryOperators(): array
{
Expand Down
6 changes: 2 additions & 4 deletions src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,9 +49,9 @@ class ExpressionParser
public const OPERATOR_LEFT = 1;
public const OPERATOR_RIGHT = 2;

/** @var array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class: class-string<AbstractUnary>}> */
/** @var array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class: class-string<AbstractExpression>}> */
private array $unaryOperators;
/** @var array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class: class-string<AbstractBinary>, associativity: self::OPERATOR_*}> */
/** @var array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class: class-string<AbstractExpression>, associativity: self::OPERATOR_*}> */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 changes look wrong to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those values are set in construct

 $this->unaryOperators = $env->getUnaryOperators();
$this->binaryOperators = $env->getBinaryOperators();

They call the method on $this->extensionSet.
The ExtensionSet gets them from multiple ExtensionInterface,
and getOperators is

/**
     * Returns a list of operators to add to the existing list.
     *
     * @return array<array> First array of unary operators, second array of binary operators
     *
     * @psalm-return array{
     *     array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class: class-string<AbstractExpression>}>,
     *     array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class?: class-string<AbstractExpression>, associativity: ExpressionParser::OPERATOR_*}>
     * }
     */
    public function getOperators(): array;

This is because

'??' => ['precedence' => 5, 'class' => NullCoalesceExpression::class, 'associativity' => ExpressionParser::OPERATOR_RIGHT],

is not an AbstractBinary but just an AbtractExpression.

private array $binaryOperators;
private array $precedenceChanges = [];
private bool $deprecationCheck = true;
Expand Down
12 changes: 8 additions & 4 deletions src/ExtensionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ final class ExtensionSet
private array $functions;
/** @var array<string, TwigFunction> */
private array $dynamicFunctions;
/** @var array<string, array{precedence: int, class: class-string<AbstractExpression>}> */
/**
* @var array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class: class-string<AbstractExpression>}>
*/
private array $unaryOperators;
/** @var array<string, array{precedence: int, class?: class-string<AbstractExpression>, associativity: ExpressionParser::OPERATOR_*}> */
/**
* @var array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class?: class-string<AbstractExpression>, associativity: ExpressionParser::OPERATOR_*}>
*/
private array $binaryOperators;
/** @var array<string, mixed>|null */
private ?array $globals = null;
Expand Down Expand Up @@ -389,7 +393,7 @@ public function getTest(string $name): ?TwigTest
}

/**
* @return array<string, array{precedence: int, class: class-string<AbstractExpression>}>
* @return array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class: class-string<AbstractExpression>}>
*/
public function getUnaryOperators(): array
{
Expand All @@ -401,7 +405,7 @@ public function getUnaryOperators(): array
}

/**
* @return array<string, array{precedence: int, class?: class-string<AbstractExpression>, associativity: ExpressionParser::OPERATOR_*}>
* @return array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class?: class-string<AbstractExpression>, associativity: ExpressionParser::OPERATOR_*}>
*/
public function getBinaryOperators(): array
{
Expand Down
2 changes: 2 additions & 0 deletions src/Runtime/LoopIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ public function __construct($seq)
$this->seq = new \ArrayIterator($seq);
} elseif ($seq instanceof \IteratorAggregate) {
do {
/** @var \Iterator<TKey, TValue> $seq */
$seq = $seq->getIterator();
} while ($seq instanceof \IteratorAggregate);
$this->seq = $seq;
} elseif (is_iterable($seq)) {
fabpot marked this conversation as resolved.
Show resolved Hide resolved
/** @var \Iterator<TKey, TValue> $seq */
$this->seq = $seq;
} else {
$this->seq = new \EmptyIterator();
Expand Down
Loading