-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Add StrictArrayParamDimFetchRector (#4384)
- Loading branch information
1 parent
205df90
commit 8324cf4
Showing
8 changed files
with
222 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
15 changes: 15 additions & 0 deletions
15
...on/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_behind_is_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,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture; | ||
|
||
final class SkipBehindIsArray | ||
{ | ||
public function resolve($item) | ||
{ | ||
if (is_array($item)) { | ||
return $item['name']; | ||
} | ||
|
||
return $item; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...eDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/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,27 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function resolve($item) | ||
{ | ||
return $item['name']; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function resolve(array $item) | ||
{ | ||
return $item['name']; | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
.../Rector/ClassMethod/StrictArrayParamDimFetchRector/StrictArrayParamDimFetchRectorTest.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class StrictArrayParamDimFetchRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...eDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(StrictArrayParamDimFetchRector::class); | ||
}; |
118 changes: 118 additions & 0 deletions
118
rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.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,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PhpParser\NodeTraverser; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\StrictArrayParamDimFetchRectorTest | ||
*/ | ||
final class StrictArrayParamDimFetchRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Add array type based on array dim fetch use', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function resolve($item) | ||
{ | ||
return $item['name']; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function resolve(array $item) | ||
{ | ||
return $item['name']; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class, Function_::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod|Function_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$hasChanged = false; | ||
|
||
foreach ($node->getParams() as $param) { | ||
if ($param->type instanceof Node) { | ||
continue; | ||
} | ||
|
||
if (! $this->isParamAccessedArrayDimFetch($param, $node)) { | ||
continue; | ||
} | ||
|
||
$param->type = new Identifier('array'); | ||
$hasChanged = true; | ||
} | ||
|
||
if ($hasChanged) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function_ $functionLike): bool | ||
{ | ||
$paramName = $this->getName($param); | ||
|
||
$isParamAccessedArrayDimFetch = false; | ||
|
||
$this->traverseNodesWithCallable($functionLike, function (Node $node) use ( | ||
$paramName, | ||
&$isParamAccessedArrayDimFetch | ||
): int|null { | ||
if ($node instanceof FuncCall && $this->isName($node, 'is_array')) { | ||
$firstArg = $node->getArgs()[0]; | ||
if ($this->isName($firstArg->value, $paramName)) { | ||
return NodeTraverser::STOP_TRAVERSAL; | ||
} | ||
} | ||
|
||
if (! $node instanceof ArrayDimFetch) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->var, $paramName)) { | ||
return null; | ||
} | ||
|
||
$isParamAccessedArrayDimFetch = true; | ||
return null; | ||
}); | ||
|
||
return $isParamAccessedArrayDimFetch; | ||
} | ||
} |