2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-01-02 15:23:21 +00:00
|
|
|
|
2019-02-23 12:00:59 +00:00
|
|
|
namespace Rector\CodingStyle\Rector\FuncCall;
|
2019-01-02 15:23:21 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-02-08 21:00:45 +00:00
|
|
|
use Rector\NodeTypeResolver\TypeAnalyzer\StringTypeAnalyzer;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-01-02 15:23:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see http://php.net/manual/en/function.implode.php#refsect1-function.implode-description
|
|
|
|
* @see https://3v4l.org/iYTgh
|
2019-09-03 09:11:45 +00:00
|
|
|
* @see \Rector\CodingStyle\Tests\Rector\FuncCall\ConsistentImplodeRector\ConsistentImplodeRectorTest
|
2019-01-02 15:23:21 +00:00
|
|
|
*/
|
|
|
|
final class ConsistentImplodeRector extends AbstractRector
|
|
|
|
{
|
2021-02-08 21:00:45 +00:00
|
|
|
/**
|
|
|
|
* @var StringTypeAnalyzer
|
|
|
|
*/
|
|
|
|
private $stringTypeAnalyzer;
|
|
|
|
|
|
|
|
public function __construct(StringTypeAnalyzer $stringTypeAnalyzer)
|
|
|
|
{
|
|
|
|
$this->stringTypeAnalyzer = $stringTypeAnalyzer;
|
|
|
|
}
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2019-01-02 15:23:21 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Changes various implode forms to consistent one',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
2019-01-02 15:23:21 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run(array $items)
|
|
|
|
{
|
|
|
|
$itemsAsStrings = implode($items);
|
|
|
|
$itemsAsStrings = implode($items, '|');
|
|
|
|
|
|
|
|
$itemsAsStrings = implode('|', $items);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
2019-01-02 15:23:21 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run(array $items)
|
|
|
|
{
|
|
|
|
$itemsAsStrings = implode('', $items);
|
|
|
|
$itemsAsStrings = implode('|', $items);
|
|
|
|
|
|
|
|
$itemsAsStrings = implode('|', $items);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-03-03 08:49:57 +00:00
|
|
|
),
|
2020-11-16 17:50:38 +00:00
|
|
|
]);
|
2019-01-02 15:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-01-02 15:23:21 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [FuncCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->isName($node, 'implode')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-25 00:22:45 +00:00
|
|
|
if (count($node->args) === 1) {
|
2019-01-02 15:23:21 +00:00
|
|
|
// complete default value ''
|
|
|
|
$node->args[1] = $node->args[0];
|
|
|
|
$node->args[0] = new Arg(new String_(''));
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:12:28 +00:00
|
|
|
$firstArgumentValue = $node->args[0]->value;
|
|
|
|
if ($firstArgumentValue instanceof String_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-02-08 21:00:45 +00:00
|
|
|
if (count($node->args) === 2 && $this->stringTypeAnalyzer->isStringOrUnionStringOnlyType(
|
|
|
|
$node->args[1]->value
|
|
|
|
)) {
|
2020-01-19 19:45:01 +00:00
|
|
|
$node->args = array_reverse($node->args);
|
2019-01-02 15:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|