Skip to content

Commit

Permalink
[CodeQuality] Deprecate GetClassToInstanceOfRector as can create inva…
Browse files Browse the repository at this point in the history
…lid comparison (#6005)
  • Loading branch information
TomasVotruba committed Jun 22, 2024
1 parent d069a99 commit abd008b
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 239 deletions.
2 changes: 0 additions & 2 deletions config/set/instanceof.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Rector\CodeQuality\Rector\FuncCall\InlineIsAInstanceOfRector;
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
use Rector\CodeQuality\Rector\Identical\GetClassToInstanceOfRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
use Rector\Instanceof_\Rector\Ternary\FlipNegatedTernaryInstanceofRector;
Expand All @@ -15,7 +14,6 @@
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
EmptyOnNullableObjectToInstanceOfRector::class,
GetClassToInstanceOfRector::class,
InlineIsAInstanceOfRector::class,
FlipTypeControlToUseExclusiveTypeRector::class,
RemoveDeadInstanceOfRector::class,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

99 changes: 12 additions & 87 deletions rules/CodeQuality/Rector/Identical/GetClassToInstanceOfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,17 @@
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use Rector\Enum\ObjectReference;
use Rector\NodeManipulator\BinaryOpManipulator;
use Rector\Php71\ValueObject\TwoNodeMatch;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\Identical\GetClassToInstanceOfRector\GetClassToInstanceOfRectorTest
* @deprecated Deprecated since 1.1.2, as can create invalid checks. See https://github.com/rectorphp/rector/issues/8639
* Use PHPStan to find those case and upgrade manually with care instead.
*/
final class GetClassToInstanceOfRector extends AbstractRector
{
/**
* @var string[]
*/
private const NO_NAMESPACED_CLASSNAMES = ['self', 'static'];

public function __construct(
private readonly BinaryOpManipulator $binaryOpManipulator,
private readonly ValueResolver $valueResolver
) {
}
private bool $hasWarned = false;

public function getRuleDefinition(): RuleDefinition
{
Expand Down Expand Up @@ -64,75 +45,19 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode(
$node,
fn (Node $node): bool => $this->isClassReference($node),
fn (Node $node): bool => $this->isGetClassFuncCallNode($node)
);

if (! $twoNodeMatch instanceof TwoNodeMatch) {
return null;
}

/** @var ClassConstFetch|String_ $firstExpr */
$firstExpr = $twoNodeMatch->getFirstExpr();

/** @var FuncCall $secondExpr */
$secondExpr = $twoNodeMatch->getSecondExpr();

if ($secondExpr->isFirstClassCallable()) {
return null;
}

if (! isset($secondExpr->getArgs()[0])) {
if ($this->hasWarned) {
return null;
}

$firstArg = $secondExpr->getArgs()[0];

$varNode = $firstArg->value;

if ($firstExpr instanceof String_) {
$className = $this->valueResolver->getValue($firstExpr);
} else {
$className = $this->getName($firstExpr->class);
}

if ($className === null) {
return null;
}

if ($className === ObjectReference::PARENT) {
return null;
}

$class = in_array($className, self::NO_NAMESPACED_CLASSNAMES, true)
? new Name($className)
: new FullyQualified($className);
$instanceof = new Instanceof_($varNode, $class);
if ($node instanceof NotIdentical) {
return new BooleanNot($instanceof);
}

return $instanceof;
}

private function isClassReference(Node $node): bool
{
if (! $node instanceof ClassConstFetch) {
// might be
return $node instanceof String_;
}

return $this->isName($node->name, 'class');
}
trigger_error(
sprintf(
'The "%s" rule was deprecated, as its functionality caused bugs. See https://github.com/rectorphp/rector/issues/8639',
self::class,
)
);

private function isGetClassFuncCallNode(Node $node): bool
{
if (! $node instanceof FuncCall) {
return false;
}
$this->hasWarned = true;

return $this->isName($node, 'get_class');
return null;
}
}

0 comments on commit abd008b

Please sign in to comment.