[PHP] Add UnsetCastRector

This commit is contained in:
Tomas Votruba 2018-10-17 05:21:02 +02:00
parent a88c3ba9da
commit e2d68961f6
7 changed files with 87 additions and 0 deletions

View File

@ -1,3 +1,4 @@
services:
Rector\Php\Rector\While_\WhileEachToForeachRector: ~
Rector\Php\Rector\Each\ListEachRector: ~
Rector\Php\Rector\Unset_\UnsetCastRector: ~

View File

@ -105,6 +105,7 @@ parameters:
- '*Command.php'
- '*NodeVisitor.php'
- '*CompilerPass.php'
- 'packages/Php/src/Rector/Unset_/UnsetCastRector.php'
# array type check
- 'src/RectorDefinition/RectorDefinition.php'

View File

@ -0,0 +1,45 @@
<?php declare(strict_types=1);
namespace Rector\Php\Rector\Unset_;
use PhpParser\Node;
use PhpParser\Node\Expr\Cast\Unset_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class UnsetCastRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Removes (unset) cast', [
new CodeSample(
<<<'CODE_SAMPLE'
$value = (unset) $value;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$value = null;
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Unset_::class];
}
/**
* @param Unset_ $node
*/
public function refactor(Node $node): ?Node
{
return new ConstFetch(new Name('null'));
}
}

View File

@ -0,0 +1,4 @@
<?php
$value = null;

View File

@ -0,0 +1,30 @@
<?php declare(strict_types=1);
namespace Rector\Php\Tests\Rector\Unset_\UnsetCastRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
/**
* @covers \Rector\Php\Rector\Unset_\UnsetCastRector
*/
final class UnsetCastRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideWrongToFixedFiles()
*/
public function test(string $wrong, string $fixed): void
{
$this->doTestFileMatchesExpectedContent($wrong, $fixed);
}
public function provideWrongToFixedFiles(): Iterator
{
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
}
protected function provideConfig(): string
{
return __DIR__ . '/config.yml';
}
}

View File

@ -0,0 +1,4 @@
<?php
$value = (unset) $value;

View File

@ -0,0 +1,2 @@
services:
Rector\Php\Rector\Unset_\UnsetCastRector: ~