Skip to content

Commit

Permalink
[DowngradePhp72] Handle in assign on DowngradePregUnmatchedAsNullCons…
Browse files Browse the repository at this point in the history
…tantRector (#1872)

* [DowngradePhp72] Handle in assign on DowngradePregUnmatchedAsNullConstantRector

* update fixture

* create assign ternary

* Fixed 🎉

* phpstan

* phpstan
  • Loading branch information
samsonasik committed Feb 26, 2022
1 parent 9d077f0 commit f686cba
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector\Fixture;

function inAssign($pattern, $subject, $matches, $flags, $offset)
{
$result = preg_match($pattern, $subject, $matches, $flags | PREG_UNMATCHED_AS_NULL, $offset);
if ($result === false) {
throw \Composer\Pcre\PcreException::fromFunction('preg_match', $pattern);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector\Fixture;

function inAssign($pattern, $subject, $matches, $flags, $offset)
{
preg_match($pattern, $subject, $matches, $flags, $offset);
array_walk_recursive($matches, function (&$value) {
if ($value === '') {
$value = null;
}
});
$result = $matches === [] ? false : 1;
if ($result === false) {
throw \Composer\Pcre\PcreException::fromFunction('preg_match', $pattern);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

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;
use PhpParser\Node\Expr\BooleanNot;
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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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');
Expand All @@ -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) {
Expand All @@ -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();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit f686cba

Please sign in to comment.