mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-24 17:29:45 +01:00
Merge pull request #1827 from rectorphp/string-json-array
simplify ManualJsonStringToJsonEncodeArrayRector
This commit is contained in:
commit
299b7c536b
@ -44,7 +44,7 @@ final class ConcatJoiner
|
||||
} elseif ($expr instanceof Concat) {
|
||||
$this->joinToStringAndPlaceholderNodes($expr);
|
||||
} else {
|
||||
$objectHash = spl_object_hash($expr);
|
||||
$objectHash = '____' . spl_object_hash($expr) . '____';
|
||||
$this->placeholderNodes[$objectHash] = $expr;
|
||||
|
||||
$this->content .= $objectHash;
|
||||
|
@ -103,21 +103,13 @@ CODE_SAMPLE
|
||||
$concatExpressionJoinData = $this->collectContentAndPlaceholderNodesFromNextExpressions($node);
|
||||
|
||||
$stringValue .= $concatExpressionJoinData->getString();
|
||||
if (! $this->isJsonString($stringValue)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// remove nodes
|
||||
$this->removeNodes($concatExpressionJoinData->getNodesToRemove());
|
||||
|
||||
$jsonArray = $this->createArrayNodeFromJsonString($stringValue);
|
||||
|
||||
$this->replaceNodeObjectHashPlaceholdersWithNodes(
|
||||
$jsonArray,
|
||||
$concatExpressionJoinData->getPlaceholdersToNodes()
|
||||
return $this->removeNodesAndCreateJsonEncodeFromStringValue(
|
||||
$concatExpressionJoinData->getNodesToRemove(),
|
||||
$stringValue,
|
||||
$concatExpressionJoinData->getPlaceholdersToNodes(),
|
||||
$node
|
||||
);
|
||||
|
||||
return $this->createAndReturnJsonEncodeFromArray($node, $jsonArray);
|
||||
}
|
||||
|
||||
if ($node->expr instanceof Concat) {
|
||||
@ -137,16 +129,12 @@ CODE_SAMPLE
|
||||
/** @var string $stringValue */
|
||||
$stringValue .= $concatExpressionJoinData->getString();
|
||||
|
||||
if (! $this->isJsonString($stringValue)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$jsonArray = $this->createArrayNodeFromJsonString($stringValue);
|
||||
$this->replaceNodeObjectHashPlaceholdersWithNodes($jsonArray, $placeholderNodes);
|
||||
|
||||
$this->removeNodes($concatExpressionJoinData->getNodesToRemove());
|
||||
|
||||
return $this->createAndReturnJsonEncodeFromArray($node, $jsonArray);
|
||||
return $this->removeNodesAndCreateJsonEncodeFromStringValue(
|
||||
$concatExpressionJoinData->getNodesToRemove(),
|
||||
$stringValue,
|
||||
$placeholderNodes,
|
||||
$node
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
@ -290,7 +278,8 @@ CODE_SAMPLE
|
||||
$concatExpressionJoinData->addPlaceholderToNode($placeholder, $expr);
|
||||
}
|
||||
} elseif ($valueNode instanceof Expr) {
|
||||
$objectHash = spl_object_hash($valueNode);
|
||||
$objectHash = '____' . spl_object_hash($valueNode) . '____';
|
||||
|
||||
$concatExpressionJoinData->addString($objectHash);
|
||||
$concatExpressionJoinData->addPlaceholderToNode($objectHash, $valueNode);
|
||||
}
|
||||
@ -345,4 +334,28 @@ CODE_SAMPLE
|
||||
|
||||
return $placeholderNodes[$node->value] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $nodesToRemove
|
||||
* @param Expr[] $placeholderNodes
|
||||
*/
|
||||
private function removeNodesAndCreateJsonEncodeFromStringValue(
|
||||
array $nodesToRemove,
|
||||
string $stringValue,
|
||||
array $placeholderNodes,
|
||||
Assign $assign
|
||||
): ?Assign {
|
||||
// quote object hashes if needed - https://regex101.com/r/85PZHm/1
|
||||
$stringValue = Strings::replace($stringValue, '#(?<start>[^\"])(?<hash>____\w+____)#', '$1"$2"');
|
||||
if (! $this->isJsonString($stringValue)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->removeNodes($nodesToRemove);
|
||||
|
||||
$jsonArray = $this->createArrayNodeFromJsonString($stringValue);
|
||||
$this->replaceNodeObjectHashPlaceholdersWithNodes($jsonArray, $placeholderNodes);
|
||||
|
||||
return $this->createAndReturnJsonEncodeFromArray($assign, $jsonArray);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\String_\ManualJsonStringToJsonEncodeArrayRector\Fixture;
|
||||
|
||||
final class ArrayConcat
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$json = '{"due_date_days":14,"billing_address":' . '{"name":"A","address":"B","city":"C",';
|
||||
$json .= '"zip":"180 00"},"administrators":' . 5;
|
||||
$json .= ',"settings":['. 3 .','. 4 .']}';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\String_\ManualJsonStringToJsonEncodeArrayRector\Fixture;
|
||||
|
||||
final class ArrayConcat
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$jsonData = ['due_date_days' => 14, 'billing_address' => ['name' => 'A', 'address' => 'B', 'city' => 'C', 'zip' => '180 00'], 'administrators' => 5, 'settings' => [3, 4]];
|
||||
$json = \Nette\Utils\Json::encode($jsonData);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -17,6 +17,7 @@ final class ManualJsonStringToJsonEncodeArrayRectorTest extends AbstractRectorTe
|
||||
__DIR__ . '/Fixture/assign_with_concat.php.inc',
|
||||
__DIR__ . '/Fixture/with_implode.php.inc',
|
||||
__DIR__ . '/Fixture/without_assign.php.inc',
|
||||
__DIR__ . '/Fixture/array_concat.php.inc',
|
||||
]);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user