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
1 change: 1 addition & 0 deletions config/set/coding-style/coding-style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ services:
Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector: ~
Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector: ~
Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector: ~
Rector\CodingStyle\Rector\String_\ManualJsonStringToJsonEncodeArrayRector: ~
5 changes: 5 additions & 0 deletions ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ services:
- 'PHPStan\Analyser\Scope'
- 'PhpParser\NodeVisitor\NameResolver'
- 'PhpParser\Node\*'
- '*Data'
- 'PhpParser\Comment'
- 'PhpParser\Lexer'
- 'PhpParser\Comment\Doc'
Expand Down Expand Up @@ -156,3 +157,7 @@ parameters:
Symplify\CodingStandard\Sniffs\DependencyInjection\NoClassInstantiationSniff:
# 3rd party api
- 'src/PhpParser/Node/Value/ValueResolver.php'

PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer:
# intentional equals
- 'tests/PhpParser/Node/NodeFactoryTest.php'
59 changes: 59 additions & 0 deletions packages/CodingStyle/src/Node/ConcatJoiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types=1);

namespace Rector\CodingStyle\Node;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\String_;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class ConcatJoiner
{
/**
* @var string
*/
private $content;

/**
* @var Expr[]
*/
private $placeholderNodes = [];

/**
* Joins all String_ nodes to string.
* Returns that string + array of non-string nodes that were replaced by hash placeholders
*
* @return string[]|Expr[][]
*/
public function joinToStringAndPlaceholderNodes(Concat $concat): array
{
if (! $concat->getAttribute(AttributeKey::PARENT_NODE) instanceof Concat) {
$this->reset();
}

$this->processConcatSide($concat->left);
$this->processConcatSide($concat->right);

return [$this->content, $this->placeholderNodes];
}

private function processConcatSide(Expr $expr): void
{
if ($expr instanceof String_) {
$this->content .= $expr->value;
} elseif ($expr instanceof Concat) {
$this->joinToStringAndPlaceholderNodes($expr);
} else {
$objectHash = spl_object_hash($expr);
$this->placeholderNodes[$objectHash] = $expr;

$this->content .= $objectHash;
}
}

private function reset(): void
{
$this->content = '';
$this->placeholderNodes = [];
}
}
67 changes: 67 additions & 0 deletions packages/CodingStyle/src/Node/ConcatManipulator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);

namespace Rector\CodingStyle\Node;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\PhpParser\Printer\BetterStandardPrinter;

final class ConcatManipulator
{
/**
* @var BetterStandardPrinter
*/
private $betterStandardPrinter;

/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

public function __construct(
BetterStandardPrinter $betterStandardPrinter,
CallableNodeTraverser $callableNodeTraverser
) {
$this->betterStandardPrinter = $betterStandardPrinter;
$this->callableNodeTraverser = $callableNodeTraverser;
}

public function getFirstConcatItem(Concat $concat): Node
{
// go to the deep, until there is no concat
while ($concat->left instanceof Concat) {
$concat = $concat->left;
}

return $concat->left;
}

public function removeFirstItemFromConcat(Concat $concat): Node
{
// just 2 items, return right one
if (! $concat->left instanceof Concat) {
return $concat->right;
}

$newConcat = clone $concat;
$firstConcatItem = $this->getFirstConcatItem($concat);

$this->callableNodeTraverser->traverseNodesWithCallable([$newConcat], function (Node $node) use (
$firstConcatItem
): ?Expr {
if (! $node instanceof Concat) {
return null;
}

if (! $this->betterStandardPrinter->areNodesEqual($node->left, $firstConcatItem)) {
return null;
}

return $node->right;
});

return $newConcat;
}
}
Loading