mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 22:08:00 +01:00
Merge pull request #706 from rectorphp/php-unset-case
[PHP] Add UnsetCastRector
This commit is contained in:
commit
25e3c6c511
@ -1,3 +1,4 @@
|
||||
services:
|
||||
Rector\Php\Rector\While_\WhileEachToForeachRector: ~
|
||||
Rector\Php\Rector\Each\ListEachRector: ~
|
||||
Rector\Php\Rector\Unset_\UnsetCastRector: ~
|
||||
|
1
ecs.yml
1
ecs.yml
@ -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'
|
||||
|
||||
|
45
packages/Php/src/Rector/Unset_/UnsetCastRector.php
Normal file
45
packages/Php/src/Rector/Unset_/UnsetCastRector.php
Normal 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'));
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
|
||||
$value = null;
|
||||
|
@ -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';
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
|
||||
$value = (unset) $value;
|
||||
|
@ -0,0 +1,2 @@
|
||||
services:
|
||||
Rector\Php\Rector\Unset_\UnsetCastRector: ~
|
Loading…
x
Reference in New Issue
Block a user