mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 11:44:14 +01:00
Merge pull request #971 from rectorphp/is-static-method-fix
Fix isStaticMethod() for method static annotation
This commit is contained in:
commit
6a6ddb76b4
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -96,7 +96,6 @@ CODE_SAMPLE
|
||||
}
|
||||
|
||||
$isStaticMethod = $this->functionLikeNodeCollector->isStaticMethod($methodName, $className);
|
||||
|
||||
if ($isStaticMethod) {
|
||||
return null;
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Php\Tests\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector\Fixture;
|
||||
|
||||
/**
|
||||
* @method static array fetchAll(...$args)
|
||||
*/
|
||||
final class HarryPotter
|
||||
{
|
||||
}
|
||||
|
||||
final class Hermione
|
||||
{
|
||||
public function cast()
|
||||
{
|
||||
HarryPotter::fetchAll();
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@ final class StaticCallOnNonStaticToInstanceCallRectorTest extends AbstractRector
|
||||
__DIR__ . '/Fixture/with_constructor.php.inc',
|
||||
__DIR__ . '/Fixture/keep.php.inc',
|
||||
__DIR__ . '/Fixture/keep_parent_static.php.inc',
|
||||
__DIR__ . '/Fixture/keep_annotated.php.inc',
|
||||
]);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user