-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Add ReturnTypeFromStrictScalarReturnExprRector (#2601)
- Loading branch information
1 parent
ed8e26e
commit 89a7a4d
Showing
11 changed files
with
462 additions
and
53 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
...ector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/Fixture/multi_return.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\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture; | ||
|
||
final class MultiReturn | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value) { | ||
return false; | ||
} | ||
|
||
return substr('warning', 1); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture; | ||
|
||
final class MultiReturn | ||
{ | ||
public function run($value): bool|string | ||
{ | ||
if ($value) { | ||
return false; | ||
} | ||
|
||
return substr('warning', 1); | ||
} | ||
} | ||
|
||
?> |
35 changes: 35 additions & 0 deletions
35
.../Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/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,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value) { | ||
return 'yes'; | ||
} | ||
|
||
return 'no'; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run($value): string | ||
{ | ||
if ($value) { | ||
return 'yes'; | ||
} | ||
|
||
return 'no'; | ||
} | ||
} | ||
|
||
?> |
27 changes: 27 additions & 0 deletions
27
...od/ReturnTypeFromStrictScalarReturnExprRector/Fixture/with_return_function_strict.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\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture; | ||
|
||
final class WithReturnFunctionStrict | ||
{ | ||
public function run() | ||
{ | ||
return substr('warning', 1); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture; | ||
|
||
final class WithReturnFunctionStrict | ||
{ | ||
public function run(): string | ||
{ | ||
return substr('warning', 1); | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...rnTypeFromStrictScalarReturnExprRector/ReturnTypeFromStrictScalarReturnExprRectorTest.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\ReturnTypeFromStrictScalarReturnExprRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ReturnTypeFromStrictScalarReturnExprRectorTest 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
.../Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/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\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ReturnTypeFromStrictScalarReturnExprRector::class); | ||
}; |
98 changes: 98 additions & 0 deletions
98
rules/CodeQuality/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector.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,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PHPStan\Type\Type; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\PhpVersion; | ||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||
use Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictScalarReturnTypeAnalyzer; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\ReturnTypeFromStrictScalarReturnExprRectorTest | ||
*/ | ||
final class ReturnTypeFromStrictScalarReturnExprRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function __construct( | ||
private readonly StrictScalarReturnTypeAnalyzer $strictScalarReturnTypeAnalyzer | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change return type based on strict scalar returns - string, int, float or bool', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value) { | ||
return 'yes'; | ||
} | ||
return 'no'; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run($value): string | ||
{ | ||
if ($value) { | ||
return 'yes'; | ||
} | ||
return 'no'; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class, Function_::class, Closure::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod|Function_|Closure $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node->returnType !== null) { | ||
return null; | ||
} | ||
|
||
$scalarReturnType = $this->strictScalarReturnTypeAnalyzer->matchAlwaysScalarReturnType($node); | ||
if (! $scalarReturnType instanceof Type) { | ||
return null; | ||
} | ||
|
||
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($scalarReturnType, TypeKind::RETURN); | ||
|
||
$node->returnType = $returnTypeNode; | ||
return $node; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersion::PHP_70; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/AlwaysStrictReturnAnalyzer.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,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\Expr\Yield_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PhpParser\Node\Stmt\Return_; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
|
||
final class AlwaysStrictReturnAnalyzer | ||
{ | ||
public function __construct( | ||
private readonly BetterNodeFinder $betterNodeFinder, | ||
) { | ||
} | ||
|
||
/** | ||
* @return Return_[]|null | ||
*/ | ||
public function matchAlwaysStrictReturns(ClassMethod|Closure|Function_ $functionLike): ?array | ||
{ | ||
if ($functionLike->stmts === null) { | ||
return null; | ||
} | ||
|
||
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($functionLike, [Yield_::class])) { | ||
return null; | ||
} | ||
|
||
/** @var Return_[] $returns */ | ||
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($functionLike, Return_::class); | ||
if ($returns === []) { | ||
return null; | ||
} | ||
|
||
// is one statement depth 3? | ||
if (! $this->areExclusiveExprReturns($returns)) { | ||
return null; | ||
} | ||
|
||
// has root return? | ||
if (! $this->hasClassMethodRootReturn($functionLike)) { | ||
return null; | ||
} | ||
|
||
return $returns; | ||
} | ||
|
||
/** | ||
* @param Return_[] $returns | ||
*/ | ||
private function areExclusiveExprReturns(array $returns): bool | ||
{ | ||
foreach ($returns as $return) { | ||
if (! $return->expr instanceof Expr) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function hasClassMethodRootReturn(ClassMethod|Function_|Closure $functionLike): bool | ||
{ | ||
foreach ((array) $functionLike->stmts as $stmt) { | ||
if ($stmt instanceof Return_) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.