Skip to content

Commit

Permalink
[CodeQuality] Handle negated isset on IssetOnPropertyObjectToProperty…
Browse files Browse the repository at this point in the history
…ExistsRector on property not exists (#5206)

* [CodeQuality] Handle negated isset on IssetOnPropertyObjectToPropertyExistsRector on property not exists

* Fix

* [ci-review] Rector Rectify

* fix

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 27, 2023
1 parent 89b3e62 commit a273705
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector\Fixture;

final class NegatedIssetPropertyNotExists
{
public function run()
{
return ! isset($this->x);
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector\Fixture;

final class NegatedIssetPropertyNotExists
{
public function run()
{
return !property_exists($this, 'x') || $this->x === null;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
Expand Down Expand Up @@ -125,7 +126,8 @@ public function refactor(Node $node): ?Node
$newNodes[] = $this->replaceToPropertyExistsWithNullCheck(
$issetExpr->var,
$propertyFetchName,
$issetExpr
$issetExpr,
$isNegated
);
} elseif ($isNegated) {
$newNodes[] = $this->createIdenticalToNull($issetExpr);
Expand All @@ -140,11 +142,17 @@ public function refactor(Node $node): ?Node
private function replaceToPropertyExistsWithNullCheck(
Expr $expr,
string $property,
PropertyFetch $propertyFetch
): BooleanAnd {
PropertyFetch $propertyFetch,
bool $isNegated
): BooleanAnd|BooleanOr {
$args = [new Arg($expr), new Arg(new String_($property))];
$propertyExistsFuncCall = $this->nodeFactory->createFuncCall('property_exists', $args);

if ($isNegated) {
$booleanNot = new BooleanNot($propertyExistsFuncCall);
return new BooleanOr($booleanNot, $this->createIdenticalToNull($propertyFetch));
}

return new BooleanAnd($propertyExistsFuncCall, $this->createNotIdenticalToNull($propertyFetch));
}

Expand Down
3 changes: 2 additions & 1 deletion src/PhpParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
Expand Down Expand Up @@ -321,7 +322,7 @@ public function createClassConstFetchFromName(Name $className, string $constantN
}

/**
* @param array<NotIdentical|BooleanAnd|Identical> $newNodes
* @param array<NotIdentical|BooleanAnd|BooleanOr|Identical> $newNodes
*/
public function createReturnBooleanAnd(array $newNodes): ?Expr
{
Expand Down

0 comments on commit a273705

Please sign in to comment.