-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add TernaryEmptyArrayArrayDimFetchToCoalesceRector (#3027)
- Loading branch information
1 parent
7b7d5a4
commit 9e4a9c0
Showing
8 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...tor/Ternary/TernaryEmptyArrayArrayDimFetchToCoalesceRector/Fixture/skip_non_array.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector\Fixture; | ||
|
||
final class SkipNonArray | ||
{ | ||
private $items; | ||
|
||
public function run() | ||
{ | ||
return ! empty($this->items) ? $this->items[0] : 'default'; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...or/Ternary/TernaryEmptyArrayArrayDimFetchToCoalesceRector/Fixture/skip_only_empty.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector\Fixture; | ||
|
||
final class SkipOnlyEmpty | ||
{ | ||
private array $items = []; | ||
|
||
public function run() | ||
{ | ||
return empty($this->items) ? $this->items[0] : 'default'; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../Rector/Ternary/TernaryEmptyArrayArrayDimFetchToCoalesceRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
private array $items = []; | ||
|
||
public function run() | ||
{ | ||
return ! empty($this->items) ? $this->items[0] : 'default'; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
private array $items = []; | ||
|
||
public function run() | ||
{ | ||
return $this->items[0] ?? 'default'; | ||
} | ||
} | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...ArrayArrayDimFetchToCoalesceRector/TernaryEmptyArrayArrayDimFetchToCoalesceRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class TernaryEmptyArrayArrayDimFetchToCoalesceRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<string> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
.../Rector/Ternary/TernaryEmptyArrayArrayDimFetchToCoalesceRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector; | ||
|
||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(TernaryEmptyArrayArrayDimFetchToCoalesceRector::class); | ||
}; |
93 changes: 93 additions & 0 deletions
93
rules/CodeQuality/Rector/Ternary/TernaryEmptyArrayArrayDimFetchToCoalesceRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\Ternary; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\BinaryOp\Coalesce; | ||
use PhpParser\Node\Expr\BooleanNot; | ||
use PhpParser\Node\Expr\Empty_; | ||
use PhpParser\Node\Expr\Ternary; | ||
use PHPStan\Type\ArrayType; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector\TernaryEmptyArrayArrayDimFetchToCoalesceRectorTest | ||
*/ | ||
final class TernaryEmptyArrayArrayDimFetchToCoalesceRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change ternary empty on array property with array dim fetch to coalesce operator', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
private array $items = []; | ||
public function run() | ||
{ | ||
return ! empty($this->items) ? $this->items[0] : 'default'; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
private array $items = []; | ||
public function run() | ||
{ | ||
return $this->items[0] ?? 'default'; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Ternary::class]; | ||
} | ||
|
||
/** | ||
* @param Ternary $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $node->cond instanceof BooleanNot) { | ||
return null; | ||
} | ||
|
||
$negagedExpr = $node->cond->expr; | ||
if (! $negagedExpr instanceof Empty_) { | ||
return null; | ||
} | ||
|
||
$emptyExprType = $this->getType($negagedExpr->expr); | ||
if (! $emptyExprType instanceof ArrayType) { | ||
return null; | ||
} | ||
|
||
if (! $node->if instanceof ArrayDimFetch) { | ||
return null; | ||
} | ||
|
||
$dimFetchVar = $node->if->var; | ||
if (! $this->nodeComparator->areNodesEqual($negagedExpr->expr, $dimFetchVar)) { | ||
return null; | ||
} | ||
|
||
return new Coalesce($node->if, $node->else); | ||
} | ||
} |