Skip to content

Commit

Permalink
[DowngradePhp72] Fix downgrade json null as true for DowngradeJsonDec…
Browse files Browse the repository at this point in the history
…odeNullAssociativeArgRector (#1724)

* [DowngradePhp72] Fix downgrade json null as true for DowngradeJsonDecodeNullAssociativeArgRector

* fix possibly null
  • Loading branch information
samsonasik committed Jan 25, 2022
1 parent 7cf2421 commit 420dc93
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Fixture
{
public function run()
{
$value = json_decode('{}', false);
$value = json_decode('{}', true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class PossiblyNull
{
function run(string $json, ?bool $associative)
{
$value = json_decode($json, (bool) $associative);
if ($associative === null) {
$associative = true;
}
$value = json_decode($json, $associative);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
namespace Rector\DowngradePhp72\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Type\BooleanType;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -21,7 +28,8 @@
final class DowngradeJsonDecodeNullAssociativeArgRector extends AbstractRector
{
public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly IfManipulator $ifManipulator
) {
}

Expand Down Expand Up @@ -49,12 +57,15 @@ function possiblyNull(string $json, ?bool $associative)
function exactlyNull(string $json)
{
$value = json_decode($json, false);
$value = json_decode($json, true);
}
function possiblyNull(string $json, ?bool $associative)
{
$value = json_decode($json, (bool) $associative);
if ($associative === null) {
$associative = true;
}
$value = json_decode($json, $associative);
}
CODE_SAMPLE
),
Expand Down Expand Up @@ -105,11 +116,25 @@ public function refactor(Node $node): ?Node
}

if ($associativeValue instanceof ConstFetch && $this->valueResolver->isNull($associativeValue)) {
$node->args[1]->value = $this->nodeFactory->createFalse();
$node->args[1]->value = $this->nodeFactory->createTrue();
return $node;
}

$node->args[1]->value = new Bool_($associativeValue);
if (! in_array(
$associativeValue::class,
[Variable::class, PropertyFetch::class, StaticPropertyFetch::class],
true
)) {
return null;
}

$currentExpression = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
$if = $this->ifManipulator->createIfExpr(
new Identical($associativeValue, $this->nodeFactory->createNull()),
new Expression(new Assign($associativeValue, $this->nodeFactory->createTrue()))
);
$this->nodesToAddCollector->addNodeBeforeNode($if, $currentExpression);

return $node;
}
}

0 comments on commit 420dc93

Please sign in to comment.