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

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Nov 22, 2024

Hi @fabpot, to help introducing PHPStan on 4.x, I made the merge 3.x into 4.x

  • Fixing the conflits
  • Fixing PHPStan build

I have

  • A commit with the merge 6a82e16

  • A commit with the update needed to get PHPStan build green in 4.x cb89b75

src/Environment.php Show resolved Hide resolved
src/Node/EmptyNode.php Outdated Show resolved Hide resolved
src/Runtime/LoopIterator.php Show resolved Hide resolved
@fabpot
Copy link
Contributor

fabpot commented Nov 22, 2024

@VincentLanglet I've just merged 3.x into 4.x to keep this PR only about phpstan.

@fabpot fabpot changed the title Merge 3.x into 4.x PHPStan for Twig 4.x Nov 22, 2024
@fabpot fabpot changed the title PHPStan for Twig 4.x Fix PHPStan for Twig 4.x Nov 22, 2024
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.

src/Runtime/LoopIterator.php Show resolved Hide resolved
@VincentLanglet
Copy link
Contributor Author

Can you add a test that would break without this change?

For the iterable/Iterator issue, I think it's an issue with PHPStan instead.
So I created an issue on phpstan/phpstan#12102 (and a fix on phpstan side)

@fabpot
Copy link
Contributor

fabpot commented Nov 22, 2024

Can you add a test that would break without this change?

For the iterable/Iterator issue, I think it's an issue with PHPStan instead. So I created an issue on phpstan/phpstan#12102 (and a fix on phpstan side)

Let's revert the change then.

@VincentLanglet
Copy link
Contributor Author

Can you add a test that would break without this change?

For the iterable/Iterator issue, I think it's an issue with PHPStan instead. So I created an issue on phpstan/phpstan#12102 (and a fix on phpstan side)

Let's revert the change then.

I reverted the extra if I added.

We still need /** @var \Iterator<TKey, TValue> $seq */ to not lose the template type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants