From df459e711ae5bd8f359df40fbef863cc386bf107 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 21 Jun 2023 16:40:17 +0700 Subject: [PATCH] Handle AlwaysRememberedExpr in deep Expr (#4310) * add test fixture for match print * Fix deep AlwaysRememberedExpr * clean up * clean up --------- Co-authored-by: Tomas Votruba --- .../WrappedNodeRestoringNodeVisitor.php | 11 ++++++-- .../PrintMatchPhpstan/Fixture/fixture.php.inc | 26 +++++++++++++++++ .../PrintMatchPhpstanTest.php | 28 +++++++++++++++++++ .../config/configured_rule.php | 10 +++++++ 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 tests/Issues/PrintMatchPhpstan/Fixture/fixture.php.inc create mode 100644 tests/Issues/PrintMatchPhpstan/PrintMatchPhpstanTest.php create mode 100644 tests/Issues/PrintMatchPhpstan/config/configured_rule.php diff --git a/src/PHPStan/NodeVisitor/WrappedNodeRestoringNodeVisitor.php b/src/PHPStan/NodeVisitor/WrappedNodeRestoringNodeVisitor.php index 98de90cbf87..2f9cb600d3c 100644 --- a/src/PHPStan/NodeVisitor/WrappedNodeRestoringNodeVisitor.php +++ b/src/PHPStan/NodeVisitor/WrappedNodeRestoringNodeVisitor.php @@ -16,10 +16,15 @@ final class WrappedNodeRestoringNodeVisitor extends NodeVisitorAbstract { public function enterNode(Node $node): ?Node { - if ($node instanceof AlwaysRememberedExpr) { - return $node->getExpr(); + if (! $node instanceof AlwaysRememberedExpr) { + return null; } - return null; + $expr = $node; + while ($expr instanceof AlwaysRememberedExpr) { + $expr = $expr->getExpr(); + } + + return $expr; } } diff --git a/tests/Issues/PrintMatchPhpstan/Fixture/fixture.php.inc b/tests/Issues/PrintMatchPhpstan/Fixture/fixture.php.inc new file mode 100644 index 00000000000..2a87bea77e5 --- /dev/null +++ b/tests/Issues/PrintMatchPhpstan/Fixture/fixture.php.inc @@ -0,0 +1,26 @@ +getType(); + + $arguments[] = match ($reflectionType->getName()) { + DefinitionConfigurator::class => $configurator, + FileLoader::class, self::class => $this, + }; + } + } +} diff --git a/tests/Issues/PrintMatchPhpstan/PrintMatchPhpstanTest.php b/tests/Issues/PrintMatchPhpstan/PrintMatchPhpstanTest.php new file mode 100644 index 00000000000..dc8f2ac1e6e --- /dev/null +++ b/tests/Issues/PrintMatchPhpstan/PrintMatchPhpstanTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/PrintMatchPhpstan/config/configured_rule.php b/tests/Issues/PrintMatchPhpstan/config/configured_rule.php new file mode 100644 index 00000000000..e132df5672e --- /dev/null +++ b/tests/Issues/PrintMatchPhpstan/config/configured_rule.php @@ -0,0 +1,10 @@ +import(DowngradeLevelSetList::DOWN_TO_PHP_81); +};