Merge pull request #971 from rectorphp/is-static-method-fix

Fix isStaticMethod() for method static annotation
This commit is contained in:
Tomáš Votruba 2019-01-20 18:17:33 -08:00 committed by GitHub
commit 6a6ddb76b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 4 deletions

View File

@ -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;
}
}

View File

@ -96,7 +96,6 @@ CODE_SAMPLE
}
$isStaticMethod = $this->functionLikeNodeCollector->isStaticMethod($methodName, $className);
if ($isStaticMethod) {
return null;
}

View File

@ -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();
}
}

View File

@ -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',
]);
}