Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\NullableObjectToInstanceofFactory;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
use Rector\TypeDeclaration\TypeAnalyzer\NullableTypeAnalyzer;

/**
Expand All @@ -28,6 +24,7 @@ final class FlipTypeControlToUseExclusiveTypeRector extends AbstractRector
public function __construct(
private readonly NullableTypeAnalyzer $nullableTypeAnalyzer,
private readonly ValueResolver $valueResolver,
private readonly NullableObjectToInstanceofFactory $nullableObjectToInstanceofFactory,
) {
}

Expand Down Expand Up @@ -79,24 +76,7 @@ public function refactor(Node $node): ?Node
return null;
}

return $this->processConvertToExclusiveType($nullableObjectType, $expr, $node);
}

private function processConvertToExclusiveType(
ObjectType $objectType,
Expr $expr,
Identical|NotIdentical $binaryOp
): BooleanNot|Instanceof_ {
$fullyQualifiedType = $objectType instanceof ShortenedObjectType || $objectType instanceof AliasedObjectType
? $objectType->getFullyQualifiedName()
: $objectType->getClassName();

$instanceof = new Instanceof_($expr, new FullyQualified($fullyQualifiedType));
if ($binaryOp instanceof NotIdentical) {
return $instanceof;
}

return new BooleanNot($instanceof);
return $this->nullableObjectToInstanceofFactory->create($expr, $nullableObjectType, $node instanceof Identical);
}

private function matchNullComparedExpr(Identical|NotIdentical $binaryOp): ?Expr
Expand Down
20 changes: 7 additions & 13 deletions rules/CodeQuality/Rector/If_/ObjectExplicitBoolCompareRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
namespace Rector\CodeQuality\Rector\If_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\ObjectType;
use Rector\CodeQuality\NodeAnalyzer\ExplicitBoolConditionResolver;
use Rector\CodeQuality\ValueObject\ExplicitBoolCondition;
use Rector\PhpParser\Node\NullableObjectToInstanceofFactory;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
Expand All @@ -26,6 +23,7 @@ final class ObjectExplicitBoolCompareRector extends AbstractRector
{
public function __construct(
private readonly ExplicitBoolConditionResolver $explicitBoolConditionResolver,
private readonly NullableObjectToInstanceofFactory $nullableObjectToInstanceofFactory,
) {
}

Expand Down Expand Up @@ -85,16 +83,12 @@ public function refactor(Node $node): ?Node
return null;
}

$node->cond = $this->resolveNullable($explicitBoolCondition->isNegated(), $expr, $objectType);
$node->cond = $this->nullableObjectToInstanceofFactory->create(
$expr,
$objectType,
$explicitBoolCondition->isNegated()
);

return $node;
}

private function resolveNullable(bool $isNegated, Expr $expr, ObjectType $objectType): BooleanNot|Instanceof_
{
$fullyQualified = new FullyQualified($objectType->getClassName());
$instanceof = new Instanceof_($expr, $fullyQualified);

return $isNegated ? new BooleanNot($instanceof) : $instanceof;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\NullableObjectToInstanceofFactory;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
Expand All @@ -28,6 +28,7 @@ final class BinaryOpNullableToInstanceofRector extends AbstractRector
{
public function __construct(
private readonly NullableTypeAnalyzer $nullableTypeAnalyzer,
private readonly NullableObjectToInstanceofFactory $nullableObjectToInstanceofFactory,
) {
}

Expand Down Expand Up @@ -96,15 +97,15 @@ private function processNullableInstance(BooleanAnd|BooleanOr $node): null|Boole

$hasChanged = false;
if ($nullableObjectType instanceof ObjectType) {
$node->left = $this->createExprInstanceof($node->left, $nullableObjectType);
$node->left = $this->nullableObjectToInstanceofFactory->create($node->left, $nullableObjectType, false);

$hasChanged = true;
}

$nullableObjectType = $this->resolveNullableObjectType($node->right);

if ($nullableObjectType instanceof ObjectType) {
$node->right = $this->createExprInstanceof($node->right, $nullableObjectType);
$node->right = $this->nullableObjectToInstanceofFactory->create($node->right, $nullableObjectType, false);

$hasChanged = true;
}
Expand All @@ -123,7 +124,7 @@ private function processNegationBooleanOr(BooleanOr $booleanOr): ?BooleanOr
$nullableObjectType = $this->resolveNullableObjectType($booleanOr->left->expr);

if ($nullableObjectType instanceof ObjectType) {
$booleanOr->left->expr = $this->createExprInstanceof($booleanOr->left->expr, $nullableObjectType);
$booleanOr->left->expr = $this->nullableObjectToInstanceofFactory->create($booleanOr->left->expr, $nullableObjectType, false);
$hasChanged = true;
}
}
Expand All @@ -132,7 +133,7 @@ private function processNegationBooleanOr(BooleanOr $booleanOr): ?BooleanOr
$nullableObjectType = $this->resolveNullableObjectType($booleanOr->right->expr);

if ($nullableObjectType instanceof ObjectType) {
$booleanOr->right->expr = $this->createExprInstanceof($booleanOr->right->expr, $nullableObjectType);
$booleanOr->right->expr = $this->nullableObjectToInstanceofFactory->create($booleanOr->right->expr, $nullableObjectType, false);

$hasChanged = true;
}
Expand Down Expand Up @@ -164,10 +165,4 @@ private function isNeverNullableObject(Expr $expr): bool
|| $expr instanceof Empty_
|| $expr instanceof Isset_;
}

private function createExprInstanceof(Expr $expr, ObjectType $objectType): Instanceof_
{
$fullyQualified = new FullyQualified($objectType->getClassName());
return new Instanceof_($expr, $fullyQualified);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
namespace Rector\TypeDeclaration\Rector\While_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\While_;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\PhpParser\Node\NullableObjectToInstanceofFactory;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
Expand All @@ -28,6 +26,7 @@ final class WhileNullableToInstanceofRector extends AbstractRector
public function __construct(
private readonly NullableTypeAnalyzer $nullableTypeAnalyzer,
private readonly ValueResolver $valueResolver,
private readonly NullableObjectToInstanceofFactory $nullableObjectToInstanceofFactory,
) {
}

Expand Down Expand Up @@ -88,17 +87,11 @@ public function refactor(Node $node): ?Node
return null;
}

$node->cond = $this->createInstanceof($node->cond, $condNullableObjectType);
$node->cond = $this->nullableObjectToInstanceofFactory->create($node->cond, $condNullableObjectType, false);

return $node;
}

private function createInstanceof(Expr $expr, ObjectType $objectType): Instanceof_
{
$fullyQualified = new FullyQualified($objectType->getClassName());
return new Instanceof_($expr, $fullyQualified);
}

/**
* @template TLoop of While_|Do_
* @param TLoop $while
Expand All @@ -115,7 +108,7 @@ private function refactorNotIdentical(While_|Do_ $while, NotIdentical $notIdenti
return null;
}

$while->cond = $this->createInstanceof($notIdentical->left, $condNullableObjectType);
$while->cond = $this->nullableObjectToInstanceofFactory->create($notIdentical->left, $condNullableObjectType, false);
return $while;
}
}
27 changes: 27 additions & 0 deletions src/PhpParser/Node/NullableObjectToInstanceofFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Rector\PhpParser\Node;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;

final class NullableObjectToInstanceofFactory
{
public function create(Expr $expr, ObjectType $objectType, bool $isNegated): BooleanNot|Instanceof_
{
$className = $objectType instanceof ShortenedObjectType || $objectType instanceof AliasedObjectType
? $objectType->getFullyQualifiedName()
: $objectType->getClassName();

$instanceof = new Instanceof_($expr, new FullyQualified($className));

return $isNegated ? new BooleanNot($instanceof) : $instanceof;
}
}
Loading