-
-
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.
[TypeDeclaration] Kick off ReturnTypeFromStrictReturnExprRector (#2563)
- Loading branch information
1 parent
564c127
commit 4270770
Showing
10 changed files
with
294 additions
and
16 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
13 changes: 13 additions & 0 deletions
13
...ector/ClassMethod/ReturnTypeFromStrictReturnExprRector/Fixture/skip_nested_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,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\Fixture; | ||
|
||
final class SkipNestedReturn | ||
{ | ||
public function run(array $values) | ||
{ | ||
foreach ($values as $value) { | ||
return true; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
.../Rector/ClassMethod/ReturnTypeFromStrictReturnExprRector/Fixture/skip_void_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,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\Fixture; | ||
|
||
final class SkipVoidReturn | ||
{ | ||
public function run() | ||
{ | ||
return; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ration/Rector/ClassMethod/ReturnTypeFromStrictReturnExprRector/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,37 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
return $this->first() && true; | ||
} | ||
|
||
public function first() | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run(): bool | ||
{ | ||
return $this->first() && true; | ||
} | ||
|
||
public function first(): bool | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...sMethod/ReturnTypeFromStrictReturnExprRector/ReturnTypeFromStrictReturnExprRectorTest.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\ReturnTypeFromStrictReturnExprRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ReturnTypeFromStrictReturnExprRectorTest 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
...ration/Rector/ClassMethod/ReturnTypeFromStrictReturnExprRector/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\ReturnTypeFromStrictReturnExprRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ReturnTypeFromStrictReturnExprRector::class); | ||
}; |
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
149 changes: 149 additions & 0 deletions
149
rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictReturnExprRector.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,149 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd; | ||
use PhpParser\Node\Expr\BinaryOp\BooleanOr; | ||
use PhpParser\Node\Expr\BinaryOp\Equal; | ||
use PhpParser\Node\Expr\BinaryOp\Identical; | ||
use PhpParser\Node\Expr\BinaryOp\NotEqual; | ||
use PhpParser\Node\Expr\BinaryOp\NotIdentical; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Expr\Empty_; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Return_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\ReturnTypeFromStrictReturnExprRectorTest | ||
*/ | ||
final class ReturnTypeFromStrictReturnExprRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Add strict return type based on returned strict expr type', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
return $this->first() && true; | ||
} | ||
public function first() | ||
{ | ||
return true; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
return $this->first() && true; | ||
} | ||
public function first(): bool | ||
{ | ||
return true; | ||
} | ||
} | ||
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; | ||
} | ||
|
||
if (! $this->hasSingleStrictReturn($node)) { | ||
return null; | ||
} | ||
|
||
$node->returnType = new Identifier('bool'); | ||
return $node; | ||
} | ||
|
||
private function isStrictBoolExpr(Expr $expr): bool | ||
{ | ||
// detect strict type here :) | ||
if ($expr instanceof Empty_) { | ||
return true; | ||
} | ||
|
||
if ($expr instanceof BooleanAnd) { | ||
return true; | ||
} | ||
|
||
if ($expr instanceof BooleanOr) { | ||
return true; | ||
} | ||
|
||
if ($expr instanceof Equal) { | ||
return true; | ||
} | ||
|
||
if ($expr instanceof NotEqual) { | ||
return true; | ||
} | ||
|
||
if ($expr instanceof Identical) { | ||
return true; | ||
} | ||
|
||
if ($expr instanceof NotIdentical) { | ||
return true; | ||
} | ||
|
||
return $expr instanceof ConstFetch && in_array($expr->name->toLowerString(), ['true', 'false'], true); | ||
} | ||
|
||
private function hasSingleStrictReturn(ClassMethod $classMethod): bool | ||
{ | ||
if ($classMethod->stmts === null) { | ||
return false; | ||
} | ||
|
||
foreach ($classMethod->stmts as $stmt) { | ||
if (! $stmt instanceof Return_) { | ||
continue; | ||
} | ||
|
||
// we need exact expr return | ||
if (! $stmt->expr instanceof Expr) { | ||
return false; | ||
} | ||
|
||
if ($this->isStrictBoolExpr($stmt->expr)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |