2022-05-26 22:07:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
2022-05-26 22:07:22 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\ClassConstFetch;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\Instanceof_;
|
2023-01-02 11:58:30 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
|
|
use PHPStan\Type\Generic\GenericClassStringType;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use PHPStan\Type\ObjectWithoutClassType;
|
|
|
|
use PHPStan\Type\TypeWithClassName;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2022-05-26 22:07:22 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\FuncCall\InlineIsAInstanceOfRector\InlineIsAInstanceOfRectorTest
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class InlineIsAInstanceOfRector extends AbstractRector
|
2022-05-26 22:07:22 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2022-05-26 22:07:22 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change is_a() with object and class name check to instanceof', [new CodeSample(<<<'CODE_SAMPLE'
|
2022-05-26 22:07:22 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run(object $object)
|
|
|
|
{
|
|
|
|
return is_a($object, SomeType::class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run(object $object)
|
|
|
|
{
|
|
|
|
return $object instanceof SomeType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [FuncCall::class];
|
2022-05-26 22:07:22 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2022-05-26 22:07:22 +00:00
|
|
|
{
|
|
|
|
if (!$this->isName($node->name, 'is_a')) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-11-07 17:28:06 +00:00
|
|
|
if ($node->isFirstClassCallable()) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-05-26 22:07:22 +00:00
|
|
|
$args = $node->getArgs();
|
|
|
|
$firstArgValue = $args[0]->value;
|
|
|
|
if (!$this->isFirstObjectType($firstArgValue)) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-02 11:58:30 +00:00
|
|
|
/**
|
|
|
|
* instanceof with Variable is ok, while on FuncCal with instanceof cause fatal error, see https://3v4l.org/IHb30
|
|
|
|
*/
|
|
|
|
if ($args[1]->value instanceof Variable) {
|
|
|
|
return new Instanceof_($firstArgValue, $args[1]->value);
|
|
|
|
}
|
2022-05-26 22:07:22 +00:00
|
|
|
$className = $this->resolveClassName($args[1]->value);
|
|
|
|
if ($className === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Instanceof_($firstArgValue, new FullyQualified($className));
|
2022-05-26 22:07:22 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolveClassName(Expr $expr) : ?string
|
2022-05-26 22:07:22 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$expr instanceof ClassConstFetch) {
|
2022-05-26 22:07:22 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$type = $this->getType($expr);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($type instanceof GenericClassStringType) {
|
2022-05-26 22:07:22 +00:00
|
|
|
$type = $type->getGenericType();
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$type instanceof TypeWithClassName) {
|
2022-05-26 22:07:22 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $type->getClassName();
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isFirstObjectType(Expr $expr) : bool
|
2022-05-26 22:07:22 +00:00
|
|
|
{
|
|
|
|
$exprType = $this->getType($expr);
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($exprType instanceof ObjectWithoutClassType) {
|
2022-05-26 22:07:22 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return $exprType instanceof ObjectType;
|
2022-05-26 22:07:22 +00:00
|
|
|
}
|
|
|
|
}
|