-
Notifications
You must be signed in to change notification settings - Fork 29
[TwigHooks] Fix Twig 3 deprecations #311
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
Open
acrobat
wants to merge
1
commit into
Sylius:main
Choose a base branch
from
acrobat:fix-twig3-deprecations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
|
|
||
| use Sylius\TwigHooks\Twig\Node\HookNode; | ||
| use Twig\Node\Node; | ||
| use Twig\Node\Nodes; | ||
| use Twig\Token; | ||
| use Twig\TokenParser\AbstractTokenParser; | ||
|
|
||
|
|
@@ -26,11 +27,21 @@ public function parse(Token $token): Node | |
| { | ||
| $lineno = $token->getLine(); | ||
| $stream = $this->parser->getStream(); | ||
| $hooksNames = $this->parser->getExpressionParser()->parseExpression(); | ||
| if (method_exists($this->parser, 'parseExpression')) { | ||
| $hooksNames = $this->parser->parseExpression(); | ||
| } else { | ||
| // Remove when Twig 3.21 support is dropped | ||
| $hooksNames = $this->parser->getExpressionParser()->parseExpression(); | ||
| } | ||
|
|
||
| $hookContext = null; | ||
| if ($stream->nextIf(Token::NAME_TYPE, 'with')) { | ||
| $hookContext = $this->parser->getExpressionParser()->parseMultitargetExpression(); | ||
| if (method_exists($this->parser, 'parseExpression')) { | ||
| $hookContext = $this->parseMultitargetExpression(); | ||
| } else { | ||
| // Remove when Twig 3.21 support is dropped | ||
| $hookContext = $this->parser->getExpressionParser()->parseMultitargetExpression(); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is deprecated and no alternative is provided. In twig the usage of this method was replaced by a private method, so I'm applying the same logic here twigphp/Twig@42c82e1#diff-c4fd0102f3bc6095b6b2c644eed251b98a65e46d2ed80329d97e9da3e97b984f |
||
| } | ||
| } | ||
|
|
||
| $only = false; | ||
|
|
@@ -40,11 +51,29 @@ public function parse(Token $token): Node | |
|
|
||
| $stream->expect(Token::BLOCK_END_TYPE); | ||
|
|
||
| if (class_exists(\Twig\Node\Expression\FunctionNode\EnumCasesFunction::class)) { | ||
| return new HookNode($hooksNames, $hookContext, $only, $lineno); | ||
| } | ||
|
|
||
| // Remove when twig < 3.12 support is dropped | ||
| return new HookNode($hooksNames, $hookContext, $only, $lineno, $this->getTag()); | ||
| } | ||
|
|
||
| public function getTag(): string | ||
| { | ||
| return self::TAG; | ||
| } | ||
|
|
||
| private function parseMultitargetExpression(): Nodes | ||
| { | ||
| $targets = []; | ||
| while (true) { | ||
| $targets[] = $this->parser->parseExpression(); | ||
| if (!$this->parser->getStream()->nextIf(Token::PUNCTUATION_TYPE, ',')) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return new Nodes($targets); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer an early return style.
But maybe in the opposite way to remove the code easily after bumping the package.
That's only a suggestion, cause it's ok like this too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'm also using the early return in regular code but mostly when doing deprecations like this I structure it in an if/else to make more clear what to remove when going to the next major or when dependencies are bumped
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also prefer reversing the conditions and early returning, in other places as well, but ultimately think it's fine as is
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@acrobat Ok, could you try to apply the early returning that will contain the code to remove?