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/set/code-quality/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 25 additions & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 455 Rectors Overview
# All 456 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -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;
}
}