mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 00:10:51 +00:00
[CodeQuality] Add InlineIfToExplicitIfRector
This commit is contained in:
parent
ac093877b8
commit
93633a4c04
|
@ -52,3 +52,4 @@ services:
|
|||
Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector: null
|
||||
Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector: null
|
||||
Rector\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector: null
|
||||
Rector\CodeQuality\Rector\BinaryOp\InlineIfToExplicitIfRector: null
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# All 455 Rectors Overview
|
||||
# All 456 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
- [General](#general)
|
||||
|
@ -1190,6 +1190,30 @@ Simplify `in_array` and `array_keys` functions combination into `array_key_exist
|
|||
|
||||
<br>
|
||||
|
||||
### `InlineIfToExplicitIfRector`
|
||||
|
||||
- class: [`Rector\CodeQuality\Rector\BinaryOp\InlineIfToExplicitIfRector`](/../master/rules/code-quality/src/Rector/BinaryOp/InlineIfToExplicitIfRector.php)
|
||||
- [test fixtures](/../master/rules/code-quality/tests/Rector/BinaryOp/InlineIfToExplicitIfRector/Fixture)
|
||||
|
||||
Change inline if to explicit if
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$userId = null;
|
||||
|
||||
- is_null($userId) && $userId = 5;
|
||||
+ if (is_null($userId)) {
|
||||
+ $userId = 5;
|
||||
+ }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `IntvalToTypeCastRector`
|
||||
|
||||
- class: [`Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector`](/../master/rules/code-quality/src/Rector/FuncCall/IntvalToTypeCastRector.php)
|
||||
|
|
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Rector\BinaryOp;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PHPStan\Type\BooleanType;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see https://3v4l.org/dmHCC
|
||||
*
|
||||
* @see \Rector\CodeQuality\Tests\Rector\BinaryOp\InlineIfToExplicitIfRector\InlineIfToExplicitIfRectorTest
|
||||
*/
|
||||
final class InlineIfToExplicitIfRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Change inline if to explicit if', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$userId = null;
|
||||
|
||||
is_null($userId) && $userId = 5;
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$userId = null;
|
||||
|
||||
if (is_null($userId)) {
|
||||
$userId = 5;
|
||||
}
|
||||
}
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Expression::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Expression $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $node->expr instanceof BooleanAnd) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$booleanAnd = $node->expr;
|
||||
|
||||
$leftStaticType = $this->getStaticType($booleanAnd->left);
|
||||
if (! $leftStaticType instanceof BooleanType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $booleanAnd->right instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$if = new If_($booleanAnd->left);
|
||||
$if->stmts[] = new Expression($booleanAnd->right);
|
||||
|
||||
return $if;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\BinaryOp\InlineIfToExplicitIfRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$userId = null;
|
||||
|
||||
is_null($userId) && $userId = 5;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\BinaryOp\InlineIfToExplicitIfRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$userId = null;
|
||||
|
||||
if (is_null($userId)) {
|
||||
$userId = 5;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\BinaryOp\InlineIfToExplicitIfRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\CodeQuality\Rector\BinaryOp\InlineIfToExplicitIfRector;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class InlineIfToExplicitIfRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return InlineIfToExplicitIfRector::class;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user