From f686cbac1cfba1808e8ce9fa9cc0b9c39e3ee079 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 26 Feb 2022 11:16:57 +0700 Subject: [PATCH] [DowngradePhp72] Handle in assign on DowngradePregUnmatchedAsNullConstantRector (#1872) * [DowngradePhp72] Handle in assign on DowngradePregUnmatchedAsNullConstantRector * update fixture * create assign ternary * Fixed :tada: * phpstan * phpstan --- .../Fixture/in_assign.php.inc | 33 ++++++++++++++++ ...gradePregUnmatchedAsNullConstantRector.php | 38 ++++++++++++++++--- 2 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 rules-tests/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector/Fixture/in_assign.php.inc diff --git a/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector/Fixture/in_assign.php.inc b/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector/Fixture/in_assign.php.inc new file mode 100644 index 00000000000..553ec570b64 --- /dev/null +++ b/rules-tests/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector/Fixture/in_assign.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/rules/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector.php b/rules/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector.php index c84700e278a..fd6a72a161f 100644 --- a/rules/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector.php +++ b/rules/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector.php @@ -6,6 +6,7 @@ use Nette\NotImplementedException; use PhpParser\Node; +use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\BinaryOp\BitwiseOr; use PhpParser\Node\Expr\BinaryOp\Identical; @@ -13,6 +14,7 @@ use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Param; use PhpParser\Node\Scalar\LNumber; @@ -96,8 +98,12 @@ public function refactor(Node $node): ?Node } $node = $this->handleEmptyStringToNullMatch($node, $variable); - unset($node->args[3]); + if ($node instanceof Ternary) { + return $node; + } + + unset($node->args[3]); return $node; } @@ -154,7 +160,7 @@ private function refactorClassConst(ClassConst $classConst): ?ClassConst return null; } - private function handleEmptyStringToNullMatch(FuncCall $funcCall, Variable $variable): FuncCall + private function handleEmptyStringToNullMatch(FuncCall $funcCall, Variable $variable): FuncCall|Ternary { $closure = new Closure(); $variablePass = new Variable('value'); @@ -177,7 +183,7 @@ private function handleEmptyStringToNullMatch(FuncCall $funcCall, Variable $vari return $this->processReplace($funcCall, $replaceEmptyStringToNull); } - private function processReplace(FuncCall $funcCall, FuncCall $replaceEmptystringToNull): FuncCall + private function processReplace(FuncCall $funcCall, FuncCall $replaceEmptystringToNull): FuncCall|Ternary { $parent = $funcCall->getAttribute(AttributeKey::PARENT_NODE); if ($parent instanceof Expression) { @@ -198,6 +204,10 @@ private function processReplace(FuncCall $funcCall, FuncCall $replaceEmptystring return $this->processInIf($if, $funcCall, $replaceEmptystringToNull); } + if ($parent instanceof Assign && $parent->expr === $funcCall) { + return $this->processInAssign($parent, $funcCall, $replaceEmptystringToNull); + } + if (! $parent instanceof Identical) { throw new NotImplementedYetException(); } @@ -209,6 +219,22 @@ private function processReplace(FuncCall $funcCall, FuncCall $replaceEmptystring return $this->processInIf($if, $funcCall, $replaceEmptystringToNull); } + private function processInAssign(Assign $assign, FuncCall $funcCall, FuncCall $replaceEmptyStringToNull): Ternary + { + $matchesVariable = $funcCall->args[2]->value; + + $identical = new Identical($matchesVariable, new Array_([])); + $lNumber = new LNumber(1); + $ternary = new Ternary($identical, $this->nodeFactory->createFalse(), $lNumber); + + $currentStatement = $assign->getAttribute(AttributeKey::CURRENT_STATEMENT); + + $expressions = [new Expression($funcCall), new Expression($replaceEmptyStringToNull)]; + $this->nodesToAddCollector->addNodesBeforeNode($expressions, $currentStatement); + + return $ternary; + } + private function processInIf(If_ $if, FuncCall $funcCall, FuncCall $replaceEmptyStringToNull): FuncCall { $cond = $if->cond; @@ -238,8 +264,10 @@ private function handleNotInIdenticalAndBooleanNot(If_ $if, FuncCall $funcCall): if ($if->stmts !== []) { $firstStmt = $if->stmts[0]; $this->nodesToAddCollector->addNodeBeforeNode($funcCall, $firstStmt); - } else { - $if->stmts[0] = new Expression($funcCall); + + return; } + + $if->stmts[0] = new Expression($funcCall); } }