mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-14 04:22:17 +02:00
[CodingStyle] Add ConsistentImplodeRector
This commit is contained in:
parent
45e7f5e454
commit
861a86580b
packages/CodingStyle
src/Rector/FuncCall
tests/Rector/FuncCall/ConsistentImplodeRector
@ -0,0 +1,85 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Rector\FuncCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see http://php.net/manual/en/function.implode.php#refsect1-function.implode-description
|
||||
* @see https://3v4l.org/iYTgh
|
||||
*/
|
||||
final class ConsistentImplodeRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Changes various implode forms to consistent one', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run(array $items)
|
||||
{
|
||||
$itemsAsStrings = implode($items);
|
||||
$itemsAsStrings = implode($items, '|');
|
||||
|
||||
$itemsAsStrings = implode('|', $items);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run(array $items)
|
||||
{
|
||||
$itemsAsStrings = implode('', $items);
|
||||
$itemsAsStrings = implode('|', $items);
|
||||
|
||||
$itemsAsStrings = implode('|', $items);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
if (count($node->args) === 2) {
|
||||
if ($this->isStringyType($node->args[1]->value)) {
|
||||
$node->args = array_reverse($node->args);
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
19
packages/CodingStyle/tests/Rector/FuncCall/ConsistentImplodeRector/ConsistentImplodeRectorTest.php
Normal file
19
packages/CodingStyle/tests/Rector/FuncCall/ConsistentImplodeRector/ConsistentImplodeRectorTest.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\FuncCall\ConsistentImplodeRector;
|
||||
|
||||
use Rector\CodingStyle\Rector\FuncCall\ConsistentImplodeRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class ConsistentImplodeRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return ConsistentImplodeRector::class;
|
||||
}
|
||||
}
|
33
packages/CodingStyle/tests/Rector/FuncCall/ConsistentImplodeRector/Fixture/fixture.php.inc
Normal file
33
packages/CodingStyle/tests/Rector/FuncCall/ConsistentImplodeRector/Fixture/fixture.php.inc
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\FuncCall\ConsistentImplodeRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run(array $items)
|
||||
{
|
||||
$itemsAsStrings = implode($items);
|
||||
$itemsAsStrings = implode($items, '|');
|
||||
|
||||
$itemsAsStrings = implode('|', $items);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\FuncCall\ConsistentImplodeRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run(array $items)
|
||||
{
|
||||
$itemsAsStrings = implode('', $items);
|
||||
$itemsAsStrings = implode('|', $items);
|
||||
|
||||
$itemsAsStrings = implode('|', $items);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user