2019-10-13 05:59:52 +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\Php70\Rector\StaticCall;
|
2018-12-31 16:45:31 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\New_;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PHPStan\Analyser\Scope;
|
2023-06-07 13:21:23 +00:00
|
|
|
use PHPStan\Reflection\ClassReflection;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Reflection\MethodReflection;
|
|
|
|
use PHPStan\Reflection\ReflectionProvider;
|
|
|
|
use PHPStan\Type\ObjectType;
|
2022-09-20 05:49:46 +00:00
|
|
|
use Rector\CodingStyle\ValueObject\ObjectMagicMethods;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Enum\ObjectReference;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver;
|
|
|
|
use Rector\NodeCollector\StaticAnalyzer;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
|
|
|
use Rector\Reflection\ReflectionResolver;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2021-01-20 11:41:35 +00:00
|
|
|
use ReflectionMethod;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-31 16:45:31 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://thephp.cc/news/2017/07/dont-call-instance-methods-statically https://3v4l.org/tQ32f https://3v4l.org/jB9jn
|
2020-07-30 13:42:21 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector\StaticCallOnNonStaticToInstanceCallRectorTest
|
2018-12-31 16:45:31 +00:00
|
|
|
*/
|
2023-05-10 18:50:31 +00:00
|
|
|
final class StaticCallOnNonStaticToInstanceCallRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
|
2018-12-31 16:45:31 +00:00
|
|
|
{
|
2020-03-28 23:06:05 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeCollector\StaticAnalyzer
|
2020-03-28 23:06:05 +00:00
|
|
|
*/
|
|
|
|
private $staticAnalyzer;
|
2021-02-28 07:47:48 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \PHPStan\Reflection\ReflectionProvider
|
2021-02-28 07:47:48 +00:00
|
|
|
*/
|
|
|
|
private $reflectionProvider;
|
2021-07-31 11:52:34 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\Reflection\ReflectionResolver
|
2021-07-31 11:52:34 +00:00
|
|
|
*/
|
|
|
|
private $reflectionResolver;
|
2021-03-17 21:54:40 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver
|
2021-03-17 21:54:40 +00:00
|
|
|
*/
|
|
|
|
private $parentClassScopeResolver;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(StaticAnalyzer $staticAnalyzer, ReflectionProvider $reflectionProvider, ReflectionResolver $reflectionResolver, ParentClassScopeResolver $parentClassScopeResolver)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2020-03-28 23:06:05 +00:00
|
|
|
$this->staticAnalyzer = $staticAnalyzer;
|
2021-02-28 07:47:48 +00:00
|
|
|
$this->reflectionProvider = $reflectionProvider;
|
2021-07-31 11:52:34 +00:00
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
2021-03-17 21:54:40 +00:00
|
|
|
$this->parentClassScopeResolver = $parentClassScopeResolver;
|
2018-12-31 16:45:31 +00:00
|
|
|
}
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::INSTANCE_CALL;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-31 16:45:31 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes static call to instance call, where not useful', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-31 16:45:31 +00:00
|
|
|
class Something
|
|
|
|
{
|
|
|
|
public function doWork()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Another
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return Something::doWork();
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-31 16:45:31 +00:00
|
|
|
class Something
|
|
|
|
{
|
|
|
|
public function doWork()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Another
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return (new Something)->doWork();
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-31 16:45:31 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-31 16:45:31 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-31 16:45:31 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [StaticCall::class];
|
2018-12-31 16:45:31 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param StaticCall $node
|
2018-12-31 16:45:31 +00:00
|
|
|
*/
|
2023-05-10 18:50:31 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2018-12-31 16:45:31 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->name instanceof Expr) {
|
2020-07-30 13:42:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-02-21 14:54:51 +00:00
|
|
|
$methodName = $this->getName($node->name);
|
2019-12-18 12:28:58 +00:00
|
|
|
$className = $this->resolveStaticCallClassName($node);
|
2020-12-24 16:28:56 +00:00
|
|
|
if ($methodName === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($className === null) {
|
2019-01-14 14:10:41 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-10 18:50:31 +00:00
|
|
|
if ($this->shouldSkip($methodName, $className, $node, $scope)) {
|
2019-02-09 13:22:20 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-07-20 07:43:17 +00:00
|
|
|
if ($this->isInstantiable($className, $scope)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$new = new New_($node->class);
|
|
|
|
return new MethodCall($new, $node->name, $node->args);
|
2019-02-09 13:22:20 +00:00
|
|
|
}
|
|
|
|
return null;
|
2018-12-31 16:45:31 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolveStaticCallClassName(StaticCall $staticCall) : ?string
|
2020-02-01 16:04:38 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($staticCall->class instanceof PropertyFetch) {
|
2021-10-07 17:46:41 +00:00
|
|
|
$objectType = $this->getType($staticCall->class);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($objectType instanceof ObjectType) {
|
2020-02-01 16:04:38 +00:00
|
|
|
return $objectType->getClassName();
|
|
|
|
}
|
|
|
|
}
|
2020-12-20 12:50:55 +00:00
|
|
|
return $this->getName($staticCall->class);
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
2023-05-10 18:50:31 +00:00
|
|
|
private function shouldSkip(string $methodName, string $className, StaticCall $staticCall, Scope $scope) : bool
|
2020-08-19 14:14:08 +00:00
|
|
|
{
|
2022-09-20 05:49:46 +00:00
|
|
|
if (\in_array($methodName, ObjectMagicMethods::METHOD_NAMES, \true)) {
|
|
|
|
return \true;
|
|
|
|
}
|
2022-06-19 08:57:14 +00:00
|
|
|
if (!$this->reflectionProvider->hasClass($className)) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
$classReflection = $this->reflectionProvider->getClass($className);
|
2022-09-19 17:42:48 +00:00
|
|
|
if ($classReflection->isAbstract()) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
// does the method even exist?
|
2022-06-19 08:57:14 +00:00
|
|
|
if (!$classReflection->hasMethod($methodName)) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
$isStaticMethod = $this->staticAnalyzer->isStaticMethod($classReflection, $methodName);
|
2020-08-19 14:14:08 +00:00
|
|
|
if ($isStaticMethod) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-08-19 14:14:08 +00:00
|
|
|
}
|
2023-06-07 13:21:23 +00:00
|
|
|
$reflection = $scope->getClassReflection();
|
|
|
|
if ($reflection instanceof ClassReflection && $reflection->isSubclassOf($className)) {
|
|
|
|
return \true;
|
|
|
|
}
|
2021-10-25 13:44:53 +00:00
|
|
|
$className = $this->getName($staticCall->class);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (\in_array($className, [ObjectReference::PARENT, ObjectReference::SELF, ObjectReference::STATIC], \true)) {
|
2021-10-25 13:44:53 +00:00
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
if ($className === 'class') {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-08-19 14:14:08 +00:00
|
|
|
}
|
2021-10-26 23:10:48 +00:00
|
|
|
$parentClassName = $this->parentClassScopeResolver->resolveParentClassName($scope);
|
2020-12-20 21:05:48 +00:00
|
|
|
return $className === $parentClassName;
|
2020-08-19 14:14:08 +00:00
|
|
|
}
|
2023-07-20 07:43:17 +00:00
|
|
|
private function isInstantiable(string $className, Scope $scope) : bool
|
2018-12-31 16:45:31 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->reflectionProvider->hasClass($className)) {
|
|
|
|
return \false;
|
2020-12-23 14:37:17 +00:00
|
|
|
}
|
2023-07-20 07:43:17 +00:00
|
|
|
$methodReflection = $this->reflectionResolver->resolveMethodReflection($className, '__callStatic', $scope);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($methodReflection instanceof MethodReflection) {
|
2021-07-31 11:52:34 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2021-02-28 07:47:48 +00:00
|
|
|
$classReflection = $this->reflectionProvider->getClass($className);
|
2022-05-24 13:01:35 +00:00
|
|
|
$nativeReflection = $classReflection->getNativeReflection();
|
|
|
|
$reflectionMethod = $nativeReflection->getConstructor();
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$reflectionMethod instanceof ReflectionMethod) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2019-02-09 13:22:20 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$reflectionMethod->isPublic()) {
|
|
|
|
return \false;
|
2018-12-31 16:45:31 +00:00
|
|
|
}
|
|
|
|
// required parameters in constructor, nothing we can do
|
2021-05-09 20:15:43 +00:00
|
|
|
return !(bool) $reflectionMethod->getNumberOfRequiredParameters();
|
2018-12-31 16:45:31 +00:00
|
|
|
}
|
|
|
|
}
|