Skip to content

Commit

Permalink
[CodeQuality] Add Variable support on class arg on InlineIsAInstanceO…
Browse files Browse the repository at this point in the history
…fRector (#3261)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 2, 2023
1 parent cba980b commit 47fa234
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 15 deletions.
6 changes: 3 additions & 3 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public function removeByType(string $typeToRemove): void
$phpDocNodeTraverser->traverseWithCallable($this->phpDocNode, '', function (Node $node) use (
$typeToRemove
): ?int {
if ($node instanceof PhpDocTagNode && is_a($node->value, $typeToRemove, true)) {
if ($node instanceof PhpDocTagNode && $node->value instanceof $typeToRemove) {
// keep special annotation for tools
if (str_starts_with($node->name, '@psalm-')) {
return null;
Expand All @@ -284,7 +284,7 @@ public function removeByType(string $typeToRemove): void
return PhpDocNodeTraverser::NODE_REMOVE;
}

if (! is_a($node, $typeToRemove, true)) {
if (! $node instanceof $typeToRemove) {
return null;
}

Expand Down Expand Up @@ -434,7 +434,7 @@ private function resolveNameForPhpDocTagValueNode(PhpDocTagValueNode $phpDocTagV
{
foreach (self::TAGS_TYPES_TO_NAMES as $tagValueNodeType => $name) {
/** @var class-string<PhpDocTagNode> $tagValueNodeType */
if (is_a($phpDocTagValueNode, $tagValueNodeType, true)) {
if ($phpDocTagValueNode instanceof $tagValueNodeType) {
return $name;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function findByType(PhpDocNode $phpDocNode, string $desiredType): array
&$foundNodes,
$desiredType
): Node {
if (! is_a($node, $desiredType, true)) {
if (! $node instanceof $desiredType) {
return $node;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function findParent(Node $node, callable $callable, array $allowedTypes):
}

foreach ($parentNestingBreakTypes as $parentNestingBreakType) {
if (! is_a($node, $parentNestingBreakType, true)) {
if (! $node instanceof $parentNestingBreakType) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function isNullableTypeOfSpecificType(Node $node, string $desiredType): b
}

$bareType = TypeCombinator::removeNull($nodeType);
return is_a($bareType, $desiredType, true);
return $bareType instanceof $desiredType;
}

public function getFullyQualifiedClassName(TypeWithClassName $typeWithClassName): string
Expand Down Expand Up @@ -325,7 +325,7 @@ private function isMatchingUnionType(Type $resolvedType, ObjectType $requiredObj
private function resolveByNodeTypeResolvers(Node $node): ?Type
{
foreach ($this->nodeTypeResolvers as $nodeClass => $nodeTypeResolver) {
if (! is_a($node, $nodeClass, true)) {
if (! $node instanceof $nodeClass) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getNodeClasses(): array
public function resolve(Node $node): Type
{
foreach (self::CAST_CLASS_TO_TYPE_MAP as $castClass => $typeClass) {
if (is_a($node, $castClass, true)) {
if ($node instanceof $castClass) {
return new $typeClass();
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/PhpDocParser/NodeValue/NodeValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function resolveByNode(Expr $expr): mixed

// these values cannot be resolved in reliable way
foreach (self::UNRESOLVABLE_TYPES as $unresolvableType) {
if (is_a($expr, $unresolvableType, true)) {
if ($expr instanceof $unresolvableType) {
throw new ConstExprEvaluationException(
'The node "%s" value is not possible to resolve. Provide different one.'
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\InlineIsAInstanceOfRector\Fixture;

class VariableClass
{
/**
* @param class-string<\PHPStan\Type\Type> $type
*/
public function run(object $object, string $type)
{
return is_a($object, $type);
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\InlineIsAInstanceOfRector\Fixture;

class VariableClass
{
/**
* @param class-string<\PHPStan\Type\Type> $type
*/
public function run(object $object, string $type)
{
return $object instanceof $type;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -78,6 +79,13 @@ public function refactor(Node $node): ?Node
return null;
}

/**
* 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);
}

$className = $this->resolveClassName($args[1]->value);
if ($className === null) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion rules/DeadCode/Rector/Cast/RecastingRemovalRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function refactor(Node $node): ?Node
}

$sameNodeType = self::CAST_CLASS_TO_NODE_TYPE[$nodeClass];
if (! is_a($nodeType, $sameNodeType, true)) {
if (! $nodeType instanceof $sameNodeType) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion rules/DeadCode/SideEffect/SideEffectNodeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function detect(Expr $expr): bool
}

foreach (self::SIDE_EFFECT_NODE_TYPES as $sideEffectNodeType) {
if (is_a($expr, $sideEffectNodeType, true)) {
if ($expr instanceof $sideEffectNodeType) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class AlwaysStrictBoolExprAnalyzer
public function isStrictBoolExpr(Expr $expr): bool
{
foreach (self::BOOL_TYPE_NODES as $boolTypeNode) {
if (is_a($expr, $boolTypeNode, true)) {
if ($expr instanceof $boolTypeNode) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NodeAnalyzer/CallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function isObjectCall(Expr $expr): bool
}

foreach (self::OBJECT_CALL_TYPES as $objectCallType) {
if (is_a($expr, $objectCallType, true)) {
if ($expr instanceof $objectCallType) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NodeManipulator/BinaryOpManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private function normalizeCondition(callable|string $condition): callable
return $condition;
}

return static fn (Node $node): bool => is_a($node, $condition, true);
return static fn (Node $node): bool => $node instanceof $condition;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/NodeManipulator/IfManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,6 @@ private function hasOnlyStmtOfType(If_ $if, string $desiredType): bool
return false;
}

return is_a($stmts[0], $desiredType);
return $stmts[0] instanceof $desiredType;
}
}

0 comments on commit 47fa234

Please sign in to comment.