-
Notifications
You must be signed in to change notification settings - Fork 554
Fix phpstan/phpstan#8744: Fix deprecated string interpolation syntax #5038
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
codebymikey
wants to merge
2
commits into
phpstan:2.1.x
Choose a base branch
from
codebymikey:feat/string-interpolation
base: 2.1.x
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Parser; | ||
|
|
||
| use Override; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\ArrayDimFetch; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PhpParser\Node\Scalar\InterpolatedString; | ||
| use PhpParser\NodeVisitorAbstract; | ||
| use PhpParser\Token; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
|
|
||
| #[AutowiredService] | ||
| final class DeprecatedInterpolatedStringVisitor extends NodeVisitorAbstract implements TokenAwareVisitor | ||
| { | ||
| public const ATTRIBUTE_NAME = 'isDeprecatedInterpolatedString'; | ||
|
|
||
| /** | ||
| * @var Token[] | ||
| */ | ||
| protected array $tokens = []; | ||
|
|
||
| #[Override] | ||
| public function setTokens(array $tokens): void | ||
| { | ||
| $this->tokens = $tokens; | ||
| } | ||
|
|
||
| #[Override] | ||
| public function enterNode(Node $node): ?Node | ||
| { | ||
| if (!$node instanceof InterpolatedString) { | ||
| return null; | ||
| } | ||
|
|
||
| foreach ($node->parts as $part) { | ||
| if (!$part instanceof Variable && !($part instanceof ArrayDimFetch && $part->var instanceof Variable)) { | ||
| continue; | ||
| } | ||
| $startTokenPos = $part->getStartTokenPos(); | ||
| if (!isset($this->tokens[$startTokenPos])) { | ||
| continue; | ||
| } | ||
| $startToken = (string) $this->tokens[$startTokenPos]; | ||
|
Check failure on line 45 in src/Parser/DeprecatedInterpolatedStringVisitor.php
|
||
| if ($startToken !== '${') { | ||
| continue; | ||
| } | ||
|
|
||
| $node->setAttribute(self::ATTRIBUTE_NAME, true); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Parser; | ||
|
|
||
| use PhpParser\Token; | ||
|
|
||
| /** | ||
| * A node visitor that is aware of the parser tokens. | ||
| * | ||
| * @api | ||
| */ | ||
| interface TokenAwareVisitor | ||
| { | ||
|
|
||
| /** | ||
| * @param Token[] $tokens | ||
| */ | ||
| public function setTokens(array $tokens): void; | ||
|
|
||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\String; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\ArrayDimFetch; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PhpParser\Node\Scalar\InterpolatedString; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\RegisteredRule; | ||
| use PHPStan\Parser\DeprecatedInterpolatedStringVisitor; | ||
| use PHPStan\Php\PhpVersion; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use function is_string; | ||
|
|
||
| /** | ||
| * @implements Rule<InterpolatedString> | ||
| */ | ||
| #[RegisteredRule(level: 0)] | ||
| final class InterpolatedStringRule implements Rule | ||
| { | ||
|
|
||
| public function __construct( | ||
| private PhpVersion $phpVersion, | ||
| ) | ||
| { | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return InterpolatedString::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (!$this->phpVersion->deprecatesStringInterpolation()) { | ||
| return []; | ||
| } | ||
|
|
||
| if ($node->getAttribute(DeprecatedInterpolatedStringVisitor::ATTRIBUTE_NAME) !== true) { | ||
| return []; | ||
| } | ||
|
|
||
| foreach ($node->parts as $part) { | ||
| if (!$part instanceof Variable && !($part instanceof ArrayDimFetch && $part->var instanceof Variable)) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($part instanceof ArrayDimFetch || (is_string($part->name))) { | ||
| $deprecatedMessage = 'Using ${var} in strings is deprecated in PHP 8.2. Use {$var} instead.'; | ||
| } else { | ||
| $deprecatedMessage = 'Using ${expr} (variable variables) in strings is deprecated in PHP 8.2. Use {${expr}} instead.'; | ||
| } | ||
|
|
||
| return [ | ||
| RuleErrorBuilder::message($deprecatedMessage) | ||
| ->identifier('interpolatedstring.deprecated') | ||
| ->line($node->getStartLine()) | ||
| ->fixNode($node, static fn () => new InterpolatedString($node->parts, $node->getAttributes())) | ||
| ->build(), | ||
| ]; | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\String; | ||
|
|
||
| use PHPStan\Php\PhpVersion; | ||
| use PHPStan\Rules\Rule as TRule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use PHPUnit\Framework\Attributes\RequiresPhp; | ||
| use const PHP_VERSION_ID; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<InterpolatedStringRule> | ||
| */ | ||
| class InterpolatedStringRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): TRule | ||
| { | ||
| return new InterpolatedStringRule(new PhpVersion(PHP_VERSION_ID)); | ||
| } | ||
|
|
||
| #[RequiresPhp('>= 8.2')] | ||
| public function testRule(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/interpolated-string.php'], [ | ||
| [ | ||
| 'Using ${var} in strings is deprecated in PHP 8.2. Use {$var} instead.', | ||
| 17, | ||
| ], | ||
| [ | ||
| 'Using ${expr} (variable variables) in strings is deprecated in PHP 8.2. Use {${expr}} instead.', | ||
| 18, | ||
| ], | ||
| [ | ||
| 'Using ${expr} (variable variables) in strings is deprecated in PHP 8.2. Use {${expr}} instead.', | ||
| 19, | ||
| ], | ||
| [ | ||
| 'Using ${var} in strings is deprecated in PHP 8.2. Use {$var} instead.', | ||
| 20, | ||
| ], | ||
| [ | ||
| 'Using ${expr} (variable variables) in strings is deprecated in PHP 8.2. Use {${expr}} instead.', | ||
| 23, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| #[RequiresPhp('>= 8.2')] | ||
| public function testFix(): void | ||
| { | ||
| $this->fix(__DIR__ . '/data/interpolated-string.php', __DIR__ . '/data/interpolated-string.php.fixed'); | ||
| } | ||
|
|
||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php // lint >= 8.2 | ||
|
|
||
| namespace Bug8744Traits; | ||
|
|
||
| class Test | ||
| { | ||
| public function getMethod() | ||
| { | ||
| return 'foo'; | ||
| } | ||
| } | ||
|
|
||
| const foo = 'bar'; | ||
| $foo = 'foo'; | ||
| $bar = 'bar'; | ||
| $array = ['baz']; | ||
| var_dump("${foo}"); | ||
| var_dump("${(foo)}"); | ||
| var_dump("${$foo}"); | ||
| var_dump("${array[0]}"); | ||
|
|
||
| $object = new Test(); | ||
| var_dump("${$object->getMethod()}"); |
23 changes: 23 additions & 0 deletions
23
tests/PHPStan/Rules/String/data/interpolated-string.php.fixed
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php // lint >= 8.2 | ||
|
|
||
| namespace Bug8744Traits; | ||
|
|
||
| class Test | ||
| { | ||
| public function getMethod() | ||
| { | ||
| return 'foo'; | ||
| } | ||
| } | ||
|
|
||
| const foo = 'bar'; | ||
| $foo = 'foo'; | ||
| $bar = 'bar'; | ||
| $array = ['baz']; | ||
| var_dump("{$foo}"); | ||
| var_dump("{${foo}}"); | ||
| var_dump("{${$foo}}"); | ||
| var_dump("{$array[0]}"); | ||
|
|
||
| $object = new Test(); | ||
| var_dump("{${$object->getMethod()}}"); |
Oops, something went wrong.
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.
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.
If it's a deprecation, and not a real error, should it be in deprecation-rules instead ?
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.
Potentially, my finding was that the rule needs access to the parser tokens in some form to check the original syntax.
So as long as that API is exposed in the main phpstan library, then the rule could be moved into deprecation-rules if that's the most sensible part for it.