Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeQuality] Deprecate GetClassToInstanceOfRector as can create invalid comparison #6005

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}