2020-03-28 23:06:05 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\NodeCollector;
|
2020-03-28 23:06:05 +00:00
|
|
|
|
2023-10-26 07:11:41 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
|
|
|
|
use PHPStan\Reflection\ClassReflection;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Util\StringUtils;
|
2020-03-28 23:06:05 +00:00
|
|
|
final class StaticAnalyzer
|
|
|
|
{
|
2023-10-26 07:11:41 +00:00
|
|
|
public function isStaticMethod(ClassReflection $classReflection, string $methodName, ?Class_ $class = null) : bool
|
2020-03-28 23:06:05 +00:00
|
|
|
{
|
2021-06-24 11:03:06 +00:00
|
|
|
if ($classReflection->hasNativeMethod($methodName)) {
|
2022-07-18 15:25:27 +00:00
|
|
|
$extendedMethodReflection = $classReflection->getNativeMethod($methodName);
|
|
|
|
if ($extendedMethodReflection->isStatic()) {
|
2023-10-26 07:11:41 +00:00
|
|
|
// use cached ClassReflection
|
|
|
|
if (!$class instanceof Class_) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
// use non-cached Class_
|
|
|
|
$classMethod = $class->getMethod($methodName);
|
|
|
|
if ($classMethod instanceof ClassMethod && $classMethod->isStatic()) {
|
|
|
|
return \true;
|
|
|
|
}
|
2021-06-24 11:03:06 +00:00
|
|
|
}
|
2020-03-28 23:06:05 +00:00
|
|
|
}
|
2021-06-24 11:03:06 +00:00
|
|
|
// could be static in doc type magic
|
|
|
|
// @see https://regex101.com/r/tlvfTB/1
|
|
|
|
return $this->hasStaticAnnotation($methodName, $classReflection);
|
2020-03-28 23:06:05 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function hasStaticAnnotation(string $methodName, ClassReflection $classReflection) : bool
|
2020-03-28 23:06:05 +00:00
|
|
|
{
|
2021-02-28 07:47:48 +00:00
|
|
|
$resolvedPhpDocBlock = $classReflection->getResolvedPhpDoc();
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$resolvedPhpDocBlock instanceof ResolvedPhpDocBlock) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-02-28 07:47:48 +00:00
|
|
|
}
|
2021-07-19 05:06:26 +00:00
|
|
|
// @see https://regex101.com/r/7Zkej2/1
|
2022-06-07 08:22:29 +00:00
|
|
|
return StringUtils::isMatch($resolvedPhpDocBlock->getPhpDocString(), '#@method\\s*static\\s*((([\\w\\|\\\\]+)|\\$this)*+(\\[\\])*)*\\s+\\b' . $methodName . '\\b#');
|
2020-03-28 23:06:05 +00:00
|
|
|
}
|
|
|
|
}
|