-
-
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 AddArrowFunctionReturnTypeRector (#2933)
* [TypeDeclaration] Add AddArrowFunctionReturnTypeRector * [ci-review] Rector Rectify * [ci-review] Rector Rectify * final touch: clean up * final touch: clean up * final touch: test for return declaration exists * fix min php version Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
5cd60b2
commit 6d44ff7
Showing
9 changed files
with
224 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
32 changes: 32 additions & 0 deletions
32
...r/ArrowFunction/AddArrowFunctionReturnTypeRector/AddArrowFunctionReturnTypeRectorTest.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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AddArrowFunctionReturnTypeRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<array<string>> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/before_union_types.php'; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...Declaration/Rector/ArrowFunction/AddArrowFunctionReturnTypeRector/Fixture/fixture.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\Closure\AddClosureReturnTypeRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
fn () => rand(0, 1) ? []: null; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
fn (): ?array => rand(0, 1) ? []: null; | ||
} | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...wFunction/AddArrowFunctionReturnTypeRector/Fixture/skip_return_declaration_exists.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\Closure\AddClosureReturnTypeRector\Fixture; | ||
|
||
class SkipReturnDeclarationExists | ||
{ | ||
public function run() | ||
{ | ||
fn (): array => []; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ration/Rector/ArrowFunction/AddArrowFunctionReturnTypeRector/FixturePhp80/fixture.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\Closure\AddClosureReturnTypeRector\FixturePhp80; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
fn () => rand(0, 1) ? true : (rand(0, 1) ? [] : 0); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector\FixturePhp80; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
fn (): array|bool|int => rand(0, 1) ? true : (rand(0, 1) ? [] : 0); | ||
} | ||
} | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...tests/TypeDeclaration/Rector/ArrowFunction/AddArrowFunctionReturnTypeRector/Php80Test.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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class Php80Test extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<array<string>> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/FixturePhp80'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/union_types.php'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ation/Rector/ArrowFunction/AddArrowFunctionReturnTypeRector/config/before_union_types.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Core\ValueObject\PhpVersionFeature; | ||
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->phpVersion(PhpVersionFeature::UNION_TYPES - 1); | ||
$rectorConfig->rule(AddArrowFunctionReturnTypeRector::class); | ||
}; |
12 changes: 12 additions & 0 deletions
12
...eDeclaration/Rector/ArrowFunction/AddArrowFunctionReturnTypeRector/config/union_types.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Core\ValueObject\PhpVersionFeature; | ||
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->phpVersion(PhpVersionFeature::UNION_TYPES); | ||
$rectorConfig->rule(AddArrowFunctionReturnTypeRector::class); | ||
}; |
69 changes: 69 additions & 0 deletions
69
rules/TypeDeclaration/Rector/ArrowFunction/AddArrowFunctionReturnTypeRector.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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\Rector\ArrowFunction; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrowFunction; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\PhpVersionFeature; | ||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector\AddArrowFunctionReturnTypeRectorTest | ||
*/ | ||
final class AddArrowFunctionReturnTypeRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Add known return type to arrow function', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
fn () => []; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
fn (): array => []; | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ArrowFunction::class]; | ||
} | ||
|
||
/** | ||
* @param ArrowFunction $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node->returnType !== null) { | ||
return null; | ||
} | ||
|
||
$type = $this->getType($node->expr); | ||
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::RETURN); | ||
|
||
if ($returnTypeNode === null) { | ||
return null; | ||
} | ||
|
||
$node->returnType = $returnTypeNode; | ||
|
||
return $node; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::ARROW_FUNCTION; | ||
} | ||
} |