From bf3c583cc2b57cb51694ea1a537211a6b0141979 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 21 Jan 2019 02:43:46 +0100 Subject: [PATCH] Fix isStaticMethod() for method static annotation --- .../Application/FunctionLikeNodeCollector.php | 20 ++++++++++++++++--- ...ticCallOnNonStaticToInstanceCallRector.php | 1 - .../Fixture/keep_annotated.php.inc | 18 +++++++++++++++++ ...allOnNonStaticToInstanceCallRectorTest.php | 1 + 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 packages/Php/tests/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector/Fixture/keep_annotated.php.inc diff --git a/packages/NodeTypeResolver/src/Application/FunctionLikeNodeCollector.php b/packages/NodeTypeResolver/src/Application/FunctionLikeNodeCollector.php index 112308f5701..e89e3790884 100644 --- a/packages/NodeTypeResolver/src/Application/FunctionLikeNodeCollector.php +++ b/packages/NodeTypeResolver/src/Application/FunctionLikeNodeCollector.php @@ -2,10 +2,12 @@ namespace Rector\NodeTypeResolver\Application; +use Nette\Utils\Strings; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use Rector\NodeTypeResolver\Node\Attribute; use Rector\PhpParser\Node\Resolver\NameResolver; +use ReflectionClass; final class FunctionLikeNodeCollector { @@ -67,10 +69,22 @@ final class FunctionLikeNodeCollector public function isStaticMethod(string $methodName, string $className): bool { $methodNode = $this->findMethod($methodName, $className); - if ($methodNode === null) { - return false; + if ($methodNode) { + return $methodNode->isStatic(); } - return $methodNode->isStatic(); + // could be static in doc type magic + // @see https://regex101.com/r/tlvfTB/1 + if (class_exists($className) || trait_exists($className)) { + $reflectionClass = new ReflectionClass($className); + if (Strings::match( + (string) $reflectionClass->getDocComment(), + '#@method\s*static\s*(.*?)\b' . $methodName . '\b#' + )) { + return true; + } + } + + return false; } } diff --git a/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php b/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php index c561565ab7f..702751a605b 100644 --- a/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php +++ b/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php @@ -96,7 +96,6 @@ CODE_SAMPLE } $isStaticMethod = $this->functionLikeNodeCollector->isStaticMethod($methodName, $className); - if ($isStaticMethod) { return null; } diff --git a/packages/Php/tests/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector/Fixture/keep_annotated.php.inc b/packages/Php/tests/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector/Fixture/keep_annotated.php.inc new file mode 100644 index 00000000000..a8fa235654f --- /dev/null +++ b/packages/Php/tests/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector/Fixture/keep_annotated.php.inc @@ -0,0 +1,18 @@ +