-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Add ReturnTypeFromStrictNewArrayRector
- Loading branch information
1 parent
d52fc67
commit c9850fb
Showing
8 changed files
with
315 additions
and
0 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
35 changes: 35 additions & 0 deletions
35
...ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/return_override_with_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,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture; | ||
|
||
final class ReturnOverrideWithArray | ||
{ | ||
public function run() | ||
{ | ||
$values = []; | ||
|
||
$values = [1000]; | ||
|
||
return $values; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture; | ||
|
||
final class ReturnOverrideWithArray | ||
{ | ||
public function run(): array | ||
{ | ||
$values = []; | ||
|
||
$values = [1000]; | ||
|
||
return $values; | ||
} | ||
} | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
...ation/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/skip_override.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\ReturnTypeFromStrictNewArrayRector\Fixture; | ||
|
||
final class SkipOverride | ||
{ | ||
public function run() | ||
{ | ||
$values = []; | ||
|
||
$values = 100; | ||
|
||
return $values; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...on/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/skip_two_returns.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,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture; | ||
|
||
final class SkipTwoReturns | ||
{ | ||
public function run() | ||
{ | ||
$values = []; | ||
|
||
if (mt_rand(1, 2)) { | ||
return $values; | ||
} | ||
|
||
return null; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...laration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/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\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$values = []; | ||
|
||
return $values; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run(): array | ||
{ | ||
$values = []; | ||
|
||
return $values; | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...ClassMethod/ReturnTypeFromStrictNewArrayRector/ReturnTypeFromStrictNewArrayRectorTest.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\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ReturnTypeFromStrictNewArrayRectorTest 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'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...laration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/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\ReturnTypeFromStrictNewArrayRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ReturnTypeFromStrictNewArrayRector::class); | ||
}; |
172 changes: 172 additions & 0 deletions
172
rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.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,172 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Expr\Yield_; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\Return_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\PhpVersion; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector\ReturnTypeFromStrictNewArrayRectorTest | ||
*/ | ||
final class ReturnTypeFromStrictNewArrayRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Add strict return array type based on created empty array and returned', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$values = []; | ||
return $values; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run(): array | ||
{ | ||
$values = []; | ||
return $values; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node->returnType !== null) { | ||
return null; | ||
} | ||
|
||
// 1. is variable instantiated with array | ||
$stmts = $node->stmts; | ||
if ($stmts === null) { | ||
return null; | ||
} | ||
|
||
$variable = $this->matchArrayAssignedVariable($stmts); | ||
if (! $variable instanceof Variable) { | ||
return null; | ||
} | ||
|
||
// 2. skip yields | ||
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($node, [Yield_::class])) { | ||
return null; | ||
} | ||
|
||
/** @var Return_[] $returns */ | ||
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($node, Return_::class); | ||
if (count($returns) !== 1) { | ||
return null; | ||
} | ||
|
||
if ($this->isVariableOverridenWithNonArray($node, $variable)) { | ||
return null; | ||
} | ||
|
||
$onlyReturn = $returns[0]; | ||
if (! $onlyReturn->expr instanceof Variable) { | ||
return null; | ||
} | ||
|
||
if (! $this->nodeNameResolver->areNamesEqual($onlyReturn->expr, $variable)) { | ||
return null; | ||
} | ||
|
||
// 3. always returns array | ||
$node->returnType = new Identifier('array'); | ||
|
||
return $node; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersion::PHP_70; | ||
} | ||
|
||
public function isVariableOverridenWithNonArray(ClassMethod|Node $node, Variable $variable): bool | ||
{ | ||
// is variable overriden? | ||
/** @var Assign[] $assigns */ | ||
$assigns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($node, Assign::class); | ||
foreach ($assigns as $assign) { | ||
if (! $assign->var instanceof Variable) { | ||
continue; | ||
} | ||
|
||
if (! $this->nodeNameResolver->areNamesEqual($assign->var, $variable)) { | ||
continue; | ||
} | ||
|
||
if (! $assign->expr instanceof Array_) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param Stmt[] $stmts | ||
*/ | ||
private function matchArrayAssignedVariable(array $stmts): Variable|null | ||
{ | ||
foreach ($stmts as $stmt) { | ||
if (! $stmt instanceof Expression) { | ||
continue; | ||
} | ||
if (! $stmt->expr instanceof Assign) { | ||
continue; | ||
} | ||
|
||
$assign = $stmt->expr; | ||
if (! $assign->var instanceof Variable) { | ||
continue; | ||
} | ||
|
||
if (! $assign->expr instanceof Array_) { | ||
continue; | ||
} | ||
|
||
return $assign->var; | ||
} | ||
|
||
return null; | ||
} | ||
} |