-
Notifications
You must be signed in to change notification settings - Fork 63
[code-quality] Add AddReturnTypeToDependedRector #528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
28 changes: 28 additions & 0 deletions
28
...Quality/Rector/Class_/AddReturnTypeToDependedRector/AddReturnTypeToDependedRectorTest.php
This file contains hidden or 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\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class AddReturnTypeToDependedRectorTest 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'; | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
...Quality/Rector/Class_/AddReturnTypeToDependedRector/Fixture/skip_non_test_methods.php.inc
This file contains hidden or 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,18 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class SkipNonTestMethods extends TestCase | ||
| { | ||
| public function nonTestMethod() | ||
| { | ||
| return new \stdClass(); | ||
| } | ||
|
|
||
| private function testNotMethod() | ||
| { | ||
| return new \stdClass(); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
.../CodeQuality/Rector/Class_/AddReturnTypeToDependedRector/Fixture/skip_silent_void.php.inc
This file contains hidden or 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\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class SkipSilentVoid extends TestCase | ||
| { | ||
| public function test() | ||
| { | ||
| if (rand(0, 1)) { | ||
| return; | ||
| } | ||
|
|
||
| return new \stdClass(); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
...s-tests/CodeQuality/Rector/Class_/AddReturnTypeToDependedRector/Fixture/some_test.php.inc
This file contains hidden or 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\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class SomeFileTest extends TestCase | ||
| { | ||
| public function test() | ||
| { | ||
| return new \stdClass(); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class SomeFileTest extends TestCase | ||
| { | ||
| public function test(): \stdClass | ||
| { | ||
| return new \stdClass(); | ||
| } | ||
| } | ||
|
|
||
| ?> |
35 changes: 35 additions & 0 deletions
35
...CodeQuality/Rector/Class_/AddReturnTypeToDependedRector/Fixture/variable_assigned.php.inc
This file contains hidden or 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\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class VaribleAssigned extends TestCase | ||
| { | ||
| public function test() | ||
| { | ||
| $someValue = rand(0, 1); | ||
|
|
||
| return $someValue; | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class VaribleAssigned extends TestCase | ||
| { | ||
| public function test(): int | ||
| { | ||
| $someValue = rand(0, 1); | ||
|
|
||
| return $someValue; | ||
| } | ||
| } | ||
|
|
||
| ?> |
9 changes: 9 additions & 0 deletions
9
...-tests/CodeQuality/Rector/Class_/AddReturnTypeToDependedRector/config/configured_rule.php
This file contains hidden or 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,9 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\PHPUnit\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([AddReturnTypeToDependedRector::class]); |
150 changes: 150 additions & 0 deletions
150
rules/CodeQuality/Rector/Class_/AddReturnTypeToDependedRector.php
This file contains hidden or 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,150 @@ | ||||||||
| <?php | ||||||||
|
|
||||||||
| declare(strict_types=1); | ||||||||
|
|
||||||||
| namespace Rector\PHPUnit\CodeQuality\Rector\Class_; | ||||||||
|
|
||||||||
| use PhpParser\Node; | ||||||||
| use PhpParser\Node\Expr; | ||||||||
| use PhpParser\Node\Stmt\Class_; | ||||||||
| use PhpParser\Node\Stmt\ClassMethod; | ||||||||
| use Rector\PhpParser\Node\BetterNodeFinder; | ||||||||
| use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||||||||
| use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||||||||
| use Rector\Rector\AbstractRector; | ||||||||
| use Rector\StaticTypeMapper\StaticTypeMapper; | ||||||||
| use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer; | ||||||||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||||||||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||||||||
|
|
||||||||
| /** | ||||||||
| * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddReturnTypeToDependedRector\AddReturnTypeToDependedRectorTest | ||||||||
| */ | ||||||||
| final class AddReturnTypeToDependedRector extends AbstractRector | ||||||||
| { | ||||||||
| public function __construct( | ||||||||
| private readonly TestsNodeAnalyzer $testsNodeAnalyzer, | ||||||||
| private readonly ReturnAnalyzer $returnAnalyzer, | ||||||||
| private readonly StaticTypeMapper $staticTypeMapper, | ||||||||
| private readonly BetterNodeFinder $betterNodeFinder | ||||||||
| ) { | ||||||||
| } | ||||||||
|
|
||||||||
| public function getRuleDefinition(): RuleDefinition | ||||||||
| { | ||||||||
| return new RuleDefinition( | ||||||||
| 'Add return type declaration to a test method that returns type', | ||||||||
| [ | ||||||||
| new CodeSample( | ||||||||
| <<<'CODE_SAMPLE' | ||||||||
| use PHPUnit\Framework\TestCase; | ||||||||
|
|
||||||||
| final class SomeTest extends TestCase | ||||||||
| { | ||||||||
| public function test() | ||||||||
| { | ||||||||
| $value = new \stdClass(); | ||||||||
|
|
||||||||
| return $value; | ||||||||
| } | ||||||||
| } | ||||||||
| CODE_SAMPLE | ||||||||
|
|
||||||||
| , | ||||||||
| <<<'CODE_SAMPLE' | ||||||||
| use PHPUnit\Framework\TestCase; | ||||||||
|
|
||||||||
| final class SomeTest extends TestCase | ||||||||
| { | ||||||||
| public function test(): \stdClass | ||||||||
| { | ||||||||
| $value = new \stdClass(); | ||||||||
|
|
||||||||
| return $value; | ||||||||
| } | ||||||||
| } | ||||||||
| CODE_SAMPLE | ||||||||
| ), | ||||||||
| ] | ||||||||
| ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * @return array<class-string<Node>> | ||||||||
| */ | ||||||||
| public function getNodeTypes(): array | ||||||||
| { | ||||||||
| return [Class_::class]; | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * @param Class_ $node | ||||||||
| */ | ||||||||
| public function refactor(Node $node): ?Node | ||||||||
| { | ||||||||
| if (! $this->testsNodeAnalyzer->isInTestClass($node)) { | ||||||||
| return null; | ||||||||
| } | ||||||||
|
|
||||||||
| $hasChanged = false; | ||||||||
|
|
||||||||
| foreach ($node->getMethods() as $classMethod) { | ||||||||
| if (! $classMethod->isPublic()) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| if (! $this->testsNodeAnalyzer->isTestClassMethod($classMethod)) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| if (! $this->haveAllReturnsExpr($classMethod)) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
Comment on lines
+100
to
+102
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be removed.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #529 |
||||||||
|
|
||||||||
| // already known return type | ||||||||
| if ($classMethod->returnType instanceof Node) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| $returns = $this->betterNodeFinder->findReturnsScoped($classMethod); | ||||||||
| if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($classMethod, $returns)) { | ||||||||
| return null; | ||||||||
| } | ||||||||
|
|
||||||||
| if (count($returns) !== 1) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| $soleReturnExpr = $returns[0]->expr; | ||||||||
|
|
||||||||
| // does return a type? | ||||||||
| $returnedExprType = $this->getType($soleReturnExpr); | ||||||||
| $returnType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnedExprType, TypeKind::RETURN); | ||||||||
|
|
||||||||
| if (! $returnType instanceof Node) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| $classMethod->returnType = $returnType; | ||||||||
| $hasChanged = true; | ||||||||
| } | ||||||||
|
|
||||||||
| if ($hasChanged === false) { | ||||||||
| return null; | ||||||||
| } | ||||||||
|
|
||||||||
| return $node; | ||||||||
| } | ||||||||
|
|
||||||||
| private function haveAllReturnsExpr(ClassMethod $classMethod): bool | ||||||||
| { | ||||||||
| $returns = $this->betterNodeFinder->findReturnsScoped($classMethod); | ||||||||
| foreach ($returns as $return) { | ||||||||
| if (! $return->expr instanceof Expr) { | ||||||||
| return false; | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| return true; | ||||||||
| } | ||||||||
| } | ||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.