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/level/dead-code/dead-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ services:
Rector\DeadCode\Rector\For_\RemoveDeadIfForeachForRector: ~
Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector: ~
Rector\DeadCode\Rector\MethodCall\RemoveDefaultArgumentValueRector: ~
Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector: ~
63 changes: 63 additions & 0 deletions packages/DeadCode/src/Rector/Concat/RemoveConcatAutocastRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);

namespace Rector\DeadCode\Rector\Concat;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Concat;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

final class RemoveConcatAutocastRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Remove (string) casting when it comes to concat, that does this by default', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeConcatingClass
{
public function run($value)
{
return 'hi ' . (string) $value;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeConcatingClass
{
public function run($value)
{
return 'hi ' . $value;
}
}
CODE_SAMPLE
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Concat::class];
}

/**
* @param Concat $node
*/
public function refactor(Node $node): ?Node
{
$node->left = $this->removeStringCast($node->left);
$node->right = $this->removeStringCast($node->right);

return $node;
}

private function removeStringCast(Node\Expr $expr): Node
{
return $expr instanceof Node\Expr\Cast\String_ ? $expr->expr : $expr;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\DeadCode\Tests\Rector\Concat\RemoveConcatAutocastRector\Fixture;

class SomeConcatingClass
{
public function run($value)
{
return 'hi ' . (string) $value;
}
}

?>
-----
<?php

namespace Rector\DeadCode\Tests\Rector\Concat\RemoveConcatAutocastRector\Fixture;

class SomeConcatingClass
{
public function run($value)
{
return 'hi ' . $value;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Rector\DeadCode\Tests\Rector\Concat\RemoveConcatAutocastRector;

use Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveConcatAutocastRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
}

protected function getRectorClass(): string
{
return RemoveConcatAutocastRector::class;
}
}