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;
|
|
|
|
use Rector\Core\RectorDefinition\CodeSample;
|
|
|
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
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
|
|
|
|
{
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Changes various implode forms to consistent one', [
|
|
|
|
new CodeSample(
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2019-01-02 15:23:21 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run(array $items)
|
|
|
|
{
|
|
|
|
$itemsAsStrings = implode($items);
|
|
|
|
$itemsAsStrings = implode($items, '|');
|
|
|
|
|
|
|
|
$itemsAsStrings = implode('|', $items);
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2019-01-02 15:23:21 +00:00
|
|
|
,
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2019-01-02 15:23:21 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run(array $items)
|
|
|
|
{
|
|
|
|
$itemsAsStrings = implode('', $items);
|
|
|
|
$itemsAsStrings = implode('|', $items);
|
|
|
|
|
|
|
|
$itemsAsStrings = implode('|', $items);
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2019-01-02 15:23:21 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [FuncCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->isName($node, 'implode')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($node->args) === 1) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2020-01-19 19:45:01 +00:00
|
|
|
if (count($node->args) === 2 && $this->isStringOrUnionStringOnlyType($node->args[1]->value)) {
|
|
|
|
$node->args = array_reverse($node->args);
|
2019-01-02 15:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|