-
Notifications
You must be signed in to change notification settings - Fork 585
Print literals from their value, not their source spelling, when building expression keys #6196
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
phpstan-bot
wants to merge
3
commits into
phpstan:2.2.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-n24fb23
base: 2.2.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
3 commits
Select commit
Hold shift + click to select a range
41a1c4c
Print literals from their value, not their source spelling, when buil…
phpstan-bot 59dd587
Cover the interpolation syntaxes a formatter rewrites in ExprPrinterTest
phpstan-bot dee330f
Record why interpolation is not normalized into concatenation
phpstan-bot 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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug15060; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function search($searchParams): void | ||
| { | ||
| if (isset($searchParams['test'])) { | ||
| assertType('mixed~null', $searchParams['test']); | ||
| if ($searchParams["test"]) { | ||
| assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $searchParams['test']); | ||
| assertType("mixed~(0|0.0|''|'0'|array{}|false|null)", $searchParams["test"]); | ||
| } | ||
|
|
||
| if (is_array($searchParams["test"])) { | ||
| assertType('array<mixed, mixed>', $searchParams['test']); | ||
| assertType('array<mixed, mixed>', $searchParams["test"]); | ||
| } | ||
|
|
||
| if (is_array($searchParams["test"]) && $searchParams["test"]) { | ||
| assertType('non-empty-array<mixed, mixed>', $searchParams['test']); | ||
| assertType('non-empty-array<mixed, mixed>', $searchParams["test"]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function otherStringSpellings($m): void | ||
| { | ||
| if (is_array($m['test']) && $m['test']) { | ||
| assertType('non-empty-array<mixed, mixed>', $m['test']); | ||
| assertType('non-empty-array<mixed, mixed>', $m["test"]); | ||
| assertType('non-empty-array<mixed, mixed>', $m["\x74est"]); | ||
| assertType('non-empty-array<mixed, mixed>', $m[<<<'NOWDOC' | ||
| test | ||
| NOWDOC]); | ||
| assertType('non-empty-array<mixed, mixed>', $m[<<<HEREDOC | ||
| test | ||
| HEREDOC]); | ||
| } | ||
| } | ||
|
|
||
| function intSpellings($m): void | ||
| { | ||
| if (is_array($m[1]) && $m[1]) { | ||
| assertType('non-empty-array<mixed, mixed>', $m[1]); | ||
| assertType('non-empty-array<mixed, mixed>', $m[0x1]); | ||
| assertType('non-empty-array<mixed, mixed>', $m[01]); | ||
| assertType('non-empty-array<mixed, mixed>', $m[0b1]); | ||
| assertType('mixed', $m[10]); | ||
| } | ||
| } | ||
|
|
||
| function interpolatedSpellings($m, string $k): void | ||
| { | ||
| if (is_array($m["x$k"]) && $m["x$k"]) { | ||
| assertType('non-empty-array<mixed, mixed>', $m["x$k"]); | ||
| assertType('non-empty-array<mixed, mixed>', $m["x{$k}"]); | ||
| assertType('non-empty-array<mixed, mixed>', $m[<<<HEREDOC | ||
| x$k | ||
| HEREDOC]); | ||
| } | ||
| } | ||
|
|
||
| function constFetchSpellings($m): void | ||
| { | ||
| if (is_array($m[true]) && $m[true]) { | ||
| assertType('non-empty-array<mixed, mixed>', $m[true]); | ||
| assertType('non-empty-array<mixed, mixed>', $m[TRUE]); | ||
| assertType('non-empty-array<mixed, mixed>', $m[True]); | ||
| } | ||
|
|
||
| if (is_array($m[null]) && $m[null]) { | ||
| assertType('non-empty-array<mixed, mixed>', $m[null]); | ||
| assertType('non-empty-array<mixed, mixed>', $m[NULL]); | ||
| } | ||
|
|
||
| if (is_array($m[false]) && $m[false]) { | ||
| assertType('non-empty-array<mixed, mixed>', $m[false]); | ||
| assertType('non-empty-array<mixed, mixed>', $m[FALSE]); | ||
| } | ||
| } |
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,154 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Node\Printer; | ||
|
|
||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Stmt; | ||
| use PHPStan\Parser\Parser; | ||
| use PHPStan\ShouldNotHappenException; | ||
| use PHPStan\Testing\PHPStanTestCase; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use function count; | ||
| use function get_class; | ||
| use function sprintf; | ||
|
|
||
| class ExprPrinterTest extends PHPStanTestCase | ||
| { | ||
|
|
||
| public static function dataEquivalentSpellings(): array | ||
| { | ||
| return [ | ||
| 'string quoting' => [ | ||
| ['$a[\'test\']', '$a["test"]', '$a["\x74est"]'], | ||
| ], | ||
| 'string heredoc' => [ | ||
| ['$a[\'test\']', "\$a[<<<EOT\ntest\nEOT]", "\$a[<<<'EOT'\ntest\nEOT]"], | ||
| ], | ||
| 'string escaping' => [ | ||
| ['$a["a\nb"]', "\$a[<<<EOT\na\nb\nEOT]"], | ||
| ], | ||
| 'int base' => [ | ||
| ['$a[1]', '$a[0x1]', '$a[01]', '$a[0b1]'], | ||
| ], | ||
| 'int separator' => [ | ||
| ['$a[10]', '$a[1_0]'], | ||
| ], | ||
| 'float' => [ | ||
| ['$a[1.5]', '$a[1.50]', '$a[15e-1]'], | ||
| ], | ||
| 'interpolated string' => [ | ||
| ['$a["x$b"]', '$a["x{$b}"]', "\$a[<<<EOT\nx\$b\nEOT]"], | ||
| ], | ||
| // The three interpolation syntaxes a formatter rewrites into each | ||
| // other (php-cs-fixer's explicit_string_variable, Rector's | ||
| // SimpleToComplexStringVariableRector). | ||
| 'interpolated variable syntax' => [ | ||
| ['$a["$b"]', '$a["{$b}"]', '$a["${b}"]'], | ||
| ], | ||
| 'interpolated property syntax' => [ | ||
| ['$a["$o->p"]', '$a["{$o->p}"]'], | ||
| ], | ||
| 'interpolated offset syntax' => [ | ||
| ['$a["$b[k]"]', '$a["{$b[\'k\']}"]'], | ||
| ], | ||
| 'variable variable' => [ | ||
| ['$$v', '${$v}'], | ||
| ], | ||
| 'true' => [ | ||
| ['$a[true]', '$a[TRUE]', '$a[True]', '$a[\true]'], | ||
| ], | ||
| 'false' => [ | ||
| ['$a[false]', '$a[FALSE]', '$a[\FALSE]'], | ||
| ], | ||
| 'null' => [ | ||
| ['$a[null]', '$a[NULL]', '$a[\null]'], | ||
| ], | ||
| 'object property' => [ | ||
| ['$a->b', '$a->{\'b\'}', '$a->{"b"}'], | ||
| ], | ||
| 'method name' => [ | ||
| ['$a->b()', '$a->{\'b\'}()', '$a->{"b"}()'], | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @param non-empty-list<string> $codes | ||
| */ | ||
| #[DataProvider('dataEquivalentSpellings')] | ||
| public function testEquivalentSpellingsPrintTheSame(array $codes): void | ||
| { | ||
| $exprPrinter = self::getContainer()->getByType(ExprPrinter::class); | ||
|
|
||
| $expected = null; | ||
| foreach ($codes as $code) { | ||
| $printed = $exprPrinter->printExpr($this->parseExpr($code)); | ||
| if ($expected === null) { | ||
| $expected = $printed; | ||
| continue; | ||
| } | ||
|
|
||
| $this->assertSame($expected, $printed, sprintf('%s should print the same as %s', $code, $codes[0])); | ||
| } | ||
| } | ||
|
|
||
| public static function dataDifferentSpellings(): array | ||
| { | ||
| return [ | ||
| 'numeric string vs int' => [ | ||
| '$a[1]', | ||
| '$a[\'1\']', | ||
| ], | ||
| 'single quoted backslash is literal' => [ | ||
| '$a[\'a\nb\']', | ||
| '$a["a\nb"]', | ||
| ], | ||
| 'different int' => [ | ||
| '$a[1]', | ||
| '$a[10]', | ||
| ], | ||
| 'other constant case is significant' => [ | ||
| '$a[FOO]', | ||
| '$a[foo]', | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| #[DataProvider('dataDifferentSpellings')] | ||
| public function testDifferentSpellingsPrintDifferently(string $code, string $otherCode): void | ||
| { | ||
| $exprPrinter = self::getContainer()->getByType(ExprPrinter::class); | ||
|
|
||
| $this->assertNotSame( | ||
| $exprPrinter->printExpr($this->parseExpr($code)), | ||
| $exprPrinter->printExpr($this->parseExpr($otherCode)), | ||
| ); | ||
| } | ||
|
|
||
| public function testPrintedFormNeverContainsNewline(): void | ||
| { | ||
| $exprPrinter = self::getContainer()->getByType(ExprPrinter::class); | ||
|
|
||
| foreach (["\$a[<<<EOT\na\nb\nEOT]", "\$a[<<<'EOT'\na\nb\nEOT]", "\$a[<<<EOT\nx\$b\ny\nEOT]"] as $code) { | ||
| $this->assertStringNotContainsString("\n", $exprPrinter->printExpr($this->parseExpr($code)), $code); | ||
| } | ||
| } | ||
|
|
||
| private function parseExpr(string $code): Expr | ||
| { | ||
| /** @var Parser $parser */ | ||
| $parser = self::getContainer()->getService('currentPhpVersionRichParser'); | ||
|
|
||
| /** @var Stmt[] $stmts */ | ||
| $stmts = $parser->parseString(sprintf('<?php %s;', $code)); | ||
| if (count($stmts) !== 1) { | ||
| throw new ShouldNotHappenException('Expecting code which evaluates to a single statement, got: ' . count($stmts)); | ||
| } | ||
| if (!$stmts[0] instanceof Stmt\Expression) { | ||
| throw new ShouldNotHappenException('Expecting code contains a single statement expression, got: ' . get_class($stmts[0])); | ||
| } | ||
|
|
||
| return $stmts[0]->expr; | ||
| } | ||
|
|
||
| } |
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
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.
instead of paying the costs for this check on every scalar-string, string-interpolation, const-fetch could we scope it instead on offsets of array-dim-fetch only?
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'll stop polling and wait for the monitor.