-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add InlineArrayReturnAssignRector (#2183)
* [CodeQuality] Add InlineArrayReturnAssignRector * enable rule in code-quality * [ci-review] Rector Rectify Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
aa89fde
commit 08bb10d
Showing
12 changed files
with
435 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
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
41 changes: 41 additions & 0 deletions
41
...s/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/include_if.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,41 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture; | ||
|
||
final class IncludeIf | ||
{ | ||
public function run() | ||
{ | ||
if (mt_rand(0, 1)) { | ||
$person = []; | ||
$person['name'] = 'Timmy'; | ||
$person['surname'] = 'Back'; | ||
|
||
return $person; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture; | ||
|
||
final class IncludeIf | ||
{ | ||
public function run() | ||
{ | ||
if (mt_rand(0, 1)) { | ||
return ['name' => 'Timmy', 'surname' => 'Back']; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
|
||
?> |
16 changes: 16 additions & 0 deletions
16
...odeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/skip_variable.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,16 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture; | ||
|
||
final class SkipVariable | ||
{ | ||
public function run($person2) | ||
{ | ||
$person = []; | ||
|
||
$person2['name'] = 'Timmy'; | ||
$person['surname'] = 'Back'; | ||
|
||
return $person; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...s/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/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,25 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture; | ||
|
||
function getPerson() | ||
{ | ||
$person = []; | ||
$person['name'] = 'Timmy'; | ||
$person['surname'] = 'Back'; | ||
|
||
return $person; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture; | ||
|
||
function getPerson() | ||
{ | ||
return ['name' => 'Timmy', 'surname' => 'Back']; | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...ty/Rector/ClassMethod/InlineArrayReturnAssignRector/InlineArrayReturnAssignRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class InlineArrayReturnAssignRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...s/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector; | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(InlineArrayReturnAssignRector::class); | ||
}; |
67 changes: 67 additions & 0 deletions
67
rules/CodeQuality/NodeAnalyzer/VariableDimFetchAssignResolver.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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Stmt\Expression; | ||
use Rector\CodeQuality\ValueObject\KeyAndExpr; | ||
use Rector\Core\PhpParser\Comparing\NodeComparator; | ||
|
||
final class VariableDimFetchAssignResolver | ||
{ | ||
public function __construct( | ||
private readonly NodeComparator $nodeComparator | ||
) { | ||
} | ||
|
||
/** | ||
* @param Stmt[] $stmts | ||
* @return KeyAndExpr[] | ||
*/ | ||
public function resolveFromStmtsAndVariable(array $stmts, Variable $variable): array | ||
{ | ||
$keysAndExprs = []; | ||
|
||
foreach ($stmts as $stmt) { | ||
if (! $stmt instanceof Expression) { | ||
return []; | ||
} | ||
|
||
$stmtExpr = $stmt->expr; | ||
if (! $stmtExpr instanceof Assign) { | ||
return []; | ||
} | ||
|
||
$assign = $stmtExpr; | ||
|
||
$keyExpr = $this->matchKeyOnArrayDimFetchOfVariable($assign, $variable); | ||
if (! $keyExpr instanceof Expr) { | ||
return []; | ||
} | ||
|
||
$keysAndExprs[] = new KeyAndExpr($keyExpr, $assign->expr); | ||
} | ||
|
||
return $keysAndExprs; | ||
} | ||
|
||
public function matchKeyOnArrayDimFetchOfVariable(Assign $assign, Variable $variable): ?Expr | ||
{ | ||
if (! $assign->var instanceof ArrayDimFetch) { | ||
return null; | ||
} | ||
|
||
$arrayDimFetch = $assign->var; | ||
if (! $this->nodeComparator->areNodesEqual($arrayDimFetch->var, $variable)) { | ||
return null; | ||
} | ||
|
||
return $arrayDimFetch->dim; | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Do_; | ||
use PhpParser\Node\Stmt\Else_; | ||
use PhpParser\Node\Stmt\ElseIf_; | ||
use PhpParser\Node\Stmt\Foreach_; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PhpParser\Node\Stmt\If_; | ||
use PhpParser\Node\Stmt\TryCatch; | ||
use PhpParser\Node\Stmt\While_; | ||
|
||
final class NodeTypeGroup | ||
{ | ||
/** | ||
* These nodes have a public $stmts property that can be iterated - based on https://github.com/rectorphp/php-parser-nodes-docs | ||
* @todo create a virtual node, getStmts(): array | ||
* @var array<class-string<Node>> | ||
*/ | ||
public const STMTS_AWARE = [ | ||
ClassMethod::class, Function_::class, If_::class, Else_::class, ElseIf_::class, Do_::class, Foreach_::class, TryCatch::class, While_::class, | ||
]; | ||
} |
Oops, something went wrong.