2021-01-16 20:11:11 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-01-16 20:11:11 +01:00
|
|
|
namespace Rector\CodingStyle\NodeFactory;
|
|
|
|
|
2021-05-10 00:23:30 +00:00
|
|
|
use RectorPrefix20210510\Nette\Utils\Json;
|
2021-01-16 20:11:11 +01:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
2021-01-24 23:46:06 +07:00
|
|
|
use PhpParser\Node\Expr\ArrayItem;
|
2021-01-16 20:11:11 +01:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use Rector\CodingStyle\NodeAnalyzer\ImplodeAnalyzer;
|
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
|
|
|
use Rector\Core\PhpParser\Node\NodeFactory;
|
2021-05-10 00:23:30 +00:00
|
|
|
use RectorPrefix20210510\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
|
2021-01-16 20:11:11 +01:00
|
|
|
final class JsonArrayFactory
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var NodeFactory
|
|
|
|
*/
|
|
|
|
private $nodeFactory;
|
|
|
|
/**
|
|
|
|
* @var ImplodeAnalyzer
|
|
|
|
*/
|
|
|
|
private $implodeAnalyzer;
|
|
|
|
/**
|
|
|
|
* @var SimpleCallableNodeTraverser
|
|
|
|
*/
|
|
|
|
private $simpleCallableNodeTraverser;
|
2021-05-10 22:10:16 +00:00
|
|
|
public function __construct(NodeFactory $nodeFactory, ImplodeAnalyzer $implodeAnalyzer, SimpleCallableNodeTraverser $simpleCallableNodeTraverser)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-01-16 20:11:11 +01:00
|
|
|
$this->nodeFactory = $nodeFactory;
|
|
|
|
$this->implodeAnalyzer = $implodeAnalyzer;
|
|
|
|
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
|
|
|
}
|
2021-05-10 22:10:16 +00:00
|
|
|
public function createFromJsonString(string $stringValue) : Array_
|
2021-01-16 20:11:11 +01:00
|
|
|
{
|
2021-05-10 22:10:16 +00:00
|
|
|
$array = Json::decode($stringValue, Json::FORCE_ARRAY);
|
2021-01-16 20:11:11 +01:00
|
|
|
return $this->nodeFactory->createArray($array);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Expr[] $placeholderNodes
|
|
|
|
*/
|
2021-05-10 22:10:16 +00:00
|
|
|
public function createFromJsonStringAndPlaceholders(string $jsonString, array $placeholderNodes) : Array_
|
2021-01-16 20:11:11 +01:00
|
|
|
{
|
|
|
|
$jsonArray = $this->createFromJsonString($jsonString);
|
|
|
|
$this->replaceNodeObjectHashPlaceholdersWithNodes($jsonArray, $placeholderNodes);
|
|
|
|
return $jsonArray;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Expr[] $placeholderNodes
|
|
|
|
*/
|
2021-05-10 22:10:16 +00:00
|
|
|
private function replaceNodeObjectHashPlaceholdersWithNodes(Array_ $array, array $placeholderNodes) : void
|
2021-01-16 20:11:11 +01:00
|
|
|
{
|
|
|
|
// traverse and replace placeholder by original nodes
|
2021-05-10 22:10:16 +00:00
|
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($array, function (Node $node) use($placeholderNodes) : ?Expr {
|
|
|
|
if ($node instanceof Array_ && \count($node->items) === 1) {
|
2021-01-16 20:11:11 +01:00
|
|
|
$onlyItem = $node->items[0];
|
2021-05-10 22:10:16 +00:00
|
|
|
if (!$onlyItem instanceof ArrayItem) {
|
|
|
|
throw new ShouldNotHappenException();
|
2021-01-16 20:11:11 +01:00
|
|
|
}
|
|
|
|
$placeholderNode = $this->matchPlaceholderNode($onlyItem->value, $placeholderNodes);
|
|
|
|
if ($placeholderNode && $this->implodeAnalyzer->isImplodeToJson($placeholderNode)) {
|
|
|
|
/** @var FuncCall $placeholderNode */
|
|
|
|
return $placeholderNode->args[1]->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->matchPlaceholderNode($node, $placeholderNodes);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Expr[] $placeholderNodes
|
|
|
|
*/
|
2021-05-10 22:10:16 +00:00
|
|
|
private function matchPlaceholderNode(Node $node, array $placeholderNodes) : ?Expr
|
2021-01-16 20:11:11 +01:00
|
|
|
{
|
2021-05-10 22:10:16 +00:00
|
|
|
if (!$node instanceof String_) {
|
2021-01-16 20:11:11 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $placeholderNodes[$node->value] ?? null;
|
|
|
|
}
|
|
|
|
}
|