Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Expression;
use Rector\CodingStyle\Node\ConcatJoiner;
use Rector\CodingStyle\Node\ConcatManipulator;
use Rector\CodingStyle\ValueObject\ConcatExpressionJoinData;
Expand Down Expand Up @@ -190,15 +191,18 @@ private function createAndReturnJsonEncodeFromArray(Assign $assign, Array_ $json
*/
private function replaceNodeObjectHashPlaceholdersWithNodes(Array_ $array, array $placeholderNodes): void
{
// traverse and replace placeholdes by original nodes
// traverse and replace placeholder by original nodes
$this->traverseNodesWithCallable($array, function (Node $node) use ($placeholderNodes): ?Expr {
if (! $node instanceof String_) {
return null;
}
if ($node instanceof Array_ && count($node->items) === 1) {
$placeholderNode = $this->matchPlaceholderNode($node->items[0]->value, $placeholderNodes);

$stringValue = $node->value;
if ($placeholderNode && $this->isImplodeToJson($placeholderNode)) {
/** @var Expr\FuncCall $placeholderNode */
return $placeholderNode->args[1]->value;
}
}

return $placeholderNodes[$stringValue] ?? null;
return $this->matchPlaceholderNode($node, $placeholderNodes);
});
}

Expand All @@ -209,7 +213,7 @@ private function replaceNodeObjectHashPlaceholdersWithNodes(Array_ $array, array
private function matchNextExpressionAssignConcatToSameVariable(Expr $expr, Node $currentNode): ?array
{
$nextExpression = $this->getNextExpression($currentNode);
if (! $nextExpression instanceof Node\Stmt\Expression) {
if (! $nextExpression instanceof Expression) {
return null;
}

Expand Down Expand Up @@ -298,4 +302,43 @@ private function collectContentAndPlaceholderNodesFromNextExpressions(

return $concatExpressionJoinData;
}

/**
* Matches: "implode('","', $items)"
*/
private function isImplodeToJson(Node $node): bool
{
if (! $node instanceof Expr\FuncCall) {
return false;
}

if (! $this->isName($node, 'implode')) {
return false;
}

if (! isset($node->args[1])) {
return false;
}

$firstArgumentValue = $node->args[0]->value;
if ($firstArgumentValue instanceof String_) {
if ($firstArgumentValue->value !== '","') {
return false;
}
}

return true;
}

/**
* @param Expr[] $placeholderNodes
*/
private function matchPlaceholderNode(Node $node, array $placeholderNodes): ?Expr
{
if (! $node instanceof String_) {
return null;
}

return $placeholderNodes[$node->value] ?? null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

final class WithImplode
{
public function run()
{
$jsonRequest = '{"categories":["'.implode(
'","',
[1, 2, 3]
).'"],"order_by":"random"}';
}
}

?>
-----
<?php

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

final class WithImplode
{
public function run()
{
$jsonData = ['categories' => [1, 2, 3], 'order_by' => 'random'];
$jsonRequest = \Nette\Utils\Json::encode($jsonData);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function test(): void
__DIR__ . '/Fixture/multiline_concat_json.php.inc',
__DIR__ . '/Fixture/tripleline_multiline_concat_json.php.inc',
__DIR__ . '/Fixture/assign_with_concat.php.inc',
__DIR__ . '/Fixture/with_implode.php.inc',
]);
}

Expand Down