diff --git a/extension.neon b/extension.neon index 380c3e31..5844c1ca 100644 --- a/extension.neon +++ b/extension.neon @@ -307,3 +307,7 @@ services: class: mglaman\PHPStanDrupal\DeprecatedScope\GroupLegacyScope tags: - phpstan.deprecations.deprecatedScopeResolver + - + class: mglaman\PHPStanDrupal\Parser\DeprecatedHelperVisitor + tags: + - phpstan.parser.richParserNodeVisitor diff --git a/phpstan.neon b/phpstan.neon index 0d88f5fb..30d9e546 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -11,6 +11,7 @@ parameters: - tests/src/data/*.php - tests/src/Type/data/*.php - tests/src/Rules/data/*.php + - tests/src/Parser/DeprecatedHelperVisitor/data/*.php - tests/src/DeprecatedScope/data/*.php dynamicConstantNames: - Drupal::VERSION diff --git a/src/Parser/DeprecatedHelperVisitor.php b/src/Parser/DeprecatedHelperVisitor.php new file mode 100644 index 00000000..b7c43041 --- /dev/null +++ b/src/Parser/DeprecatedHelperVisitor.php @@ -0,0 +1,39 @@ +class instanceof Node\Name\FullyQualified && $node->class->toString() === 'Drupal\Component\Utility\DeprecationHelper') { + $this->deprecatedCall = $node->getArgs()[2]; + return null; + } + } + + if ($this->deprecatedCall !== null && $node instanceof Node\Arg && $this->isSavedArgument($node)) { + $this->deprecatedCall = null; + return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + + return null; + } + + private function isSavedArgument(Node\Arg $node): bool + { + if ($this->deprecatedCall !== null && $node->getAttributes() === $this->deprecatedCall->getAttributes()) { + return true; + } + return false; + } +} diff --git a/tests/src/Parser/DeprecatedHelperVisitor/DeprecatedHelperVisitorTest.php b/tests/src/Parser/DeprecatedHelperVisitor/DeprecatedHelperVisitorTest.php new file mode 100644 index 00000000..f82b7161 --- /dev/null +++ b/tests/src/Parser/DeprecatedHelperVisitor/DeprecatedHelperVisitorTest.php @@ -0,0 +1,38 @@ +getByType(DeprecatedScopeHelper::class) + ); + } + + public function testCustomScope(): void + { + require_once __DIR__ . '/data/deprecated-data-definition.php'; + $this->analyse( + [ + __DIR__ . '/data/deprecation-helper-test.php', + ], + [ + [ + 'Call to deprecated function Deprecated\deprecated_function_call().', + 16, + ], + ] + ); + } +} diff --git a/tests/src/Parser/DeprecatedHelperVisitor/data/deprecated-data-definition.php b/tests/src/Parser/DeprecatedHelperVisitor/data/deprecated-data-definition.php new file mode 100644 index 00000000..2b641cce --- /dev/null +++ b/tests/src/Parser/DeprecatedHelperVisitor/data/deprecated-data-definition.php @@ -0,0 +1,11 @@ + deprecated_function_call(), fn() => count([])); + + DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => deprecated_function_call(), fn() => count([])); + + deprecated_function_call(); + + DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', function() { + deprecated_function_call(); + }, function() { + count([]); + }); + + } + +}