Skip to content

Commit

Permalink
[CodingStyle] Handle null item on ManualJsonStringToJsonEncodeArrayRe…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
samsonasik committed Aug 30, 2021
1 parent 5058437 commit 123ab2a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Assign\ManualJsonStringToJsonEncodeArrayRector\Fixture;

final class NullItem
{
public function run()
{
$json = '{"a":null}';
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Assign\ManualJsonStringToJsonEncodeArrayRector\Fixture;

final class NullItem
{
public function run()
{
$jsonData = ['a' => null];
$json = \Nette\Utils\Json::encode($jsonData);
}
}

?>
4 changes: 4 additions & 0 deletions src/PhpParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,10 @@ public function createClassConstant(string $name, Expr $expr, int $modifier): Cl
private function createArrayItem($item, string | int | null $key = null): ArrayItem
{
$arrayItem = null;
if ($item === null) {
$item = $this->createNull();
$arrayItem = new ArrayItem($item);
}

if ($item instanceof Variable
|| $item instanceof MethodCall
Expand Down

0 comments on commit 123ab2a

Please sign in to comment.