Skip to content

Commit

Permalink
[CodeQuality] Handle on assign on FlipTypeControlToUseExclusiveTypeRe…
Browse files Browse the repository at this point in the history
…ctor (#6653)

* [CodeQuality] Handle on assign on FlipTypeControlToUseExclusiveTypeRector

* fix

* skip on assign

* clean

* fix

* fix

* fix
  • Loading branch information
samsonasik authored Jan 6, 2025
1 parent 532b39d commit da80966
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector\Fixture;

class OnAssign
{
public function run(?\DateTimeImmutable $param)
{
if (null === ($paramValue = $param)) {
return 'no';
}

return $paramValue;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector\Fixture;

class OnAssign
{
public function run(?\DateTimeImmutable $param)
{
if (!($paramValue = $param) instanceof \DateTimeImmutable) {
return 'no';
}

return $paramValue;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
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\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
Expand Down Expand Up @@ -91,6 +93,10 @@ private function processConvertToExclusiveType(
? $objectType->getFullyQualifiedName()
: $objectType->getClassName();

if ($expr instanceof Assign) {
$expr->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
}

$instanceof = new Instanceof_($expr, new FullyQualified($fullyQualifiedType));
if ($binaryOp instanceof NotIdentical) {
return $instanceof;
Expand Down
2 changes: 2 additions & 0 deletions rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use Rector\NodeManipulator\IfManipulator;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -110,6 +111,7 @@ private function refactorStmtAndInstanceof(If_ $if, Instanceof_ $instanceof): nu
}

if ($instanceof->expr instanceof Assign) {
$instanceof->expr->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, false);
$assignExpression = new Expression($instanceof->expr);
return array_merge([$assignExpression], $if->stmts);
}
Expand Down

0 comments on commit da80966

Please sign in to comment.