-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Add ReturnTypeFromStrictNativeFuncCallRector (#2570)
- Loading branch information
1 parent
e424660
commit 237f255
Showing
22 changed files
with
564 additions
and
175 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
2 changes: 1 addition & 1 deletion
2
...ixture/skip_multi_returns_no_root.php.inc → ...ixture/skip_multi_returns_no_root.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
2 changes: 1 addition & 1 deletion
2
...Rector/Fixture/skip_nested_return.php.inc → ...Rector/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
2 changes: 1 addition & 1 deletion
2
...ip_root_return_with_nested_return.php.inc → ...ip_root_return_with_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
2 changes: 1 addition & 1 deletion
2
...prRector/Fixture/skip_void_return.php.inc → ...prRector/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
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
31 changes: 31 additions & 0 deletions
31
.../Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector/Fixture/some_closure.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\ReturnTypeFromStrictBoolReturnExprRector\Fixture; | ||
|
||
final class SomeClosure | ||
{ | ||
public function run() | ||
{ | ||
return function () { | ||
return 100 && true; | ||
}; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector\Fixture; | ||
|
||
final class SomeClosure | ||
{ | ||
public function run() | ||
{ | ||
return function (): bool { | ||
return 100 && true; | ||
}; | ||
} | ||
} | ||
|
||
?> |
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
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
27 changes: 27 additions & 0 deletions
27
...on/Rector/ClassMethod/ReturnTypeFromStrictNativeFuncCallRector/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,27 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeFuncCallRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
return strlen('value'); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeFuncCallRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run(): int | ||
{ | ||
return strlen('value'); | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...ReturnTypeFromStrictNativeFuncCallRector/ReturnTypeFromStrictNativeFuncCallRectorTest.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\ReturnTypeFromStrictNativeFuncCallRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ReturnTypeFromStrictNativeFuncCallRectorTest 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'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...on/Rector/ClassMethod/ReturnTypeFromStrictNativeFuncCallRector/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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
|
||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeFuncCallRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ReturnTypeFromStrictNativeFuncCallRector::class); | ||
}; |
52 changes: 52 additions & 0 deletions
52
rules/TypeDeclaration/NodeAnalyzer/ReturnFilter/ExclusiveNativeFuncCallReturnMatcher.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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\NodeAnalyzer\ReturnFilter; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Stmt\Return_; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use Rector\Core\Reflection\ReflectionResolver; | ||
|
||
final class ExclusiveNativeFuncCallReturnMatcher | ||
{ | ||
public function __construct( | ||
private readonly ReflectionResolver $reflectionResolver, | ||
) { | ||
} | ||
|
||
/** | ||
* @param Return_[] $returns | ||
* @return FuncCall[]|null | ||
*/ | ||
public function match(array $returns): array|null | ||
{ | ||
$funcCalls = []; | ||
|
||
foreach ($returns as $return) { | ||
// we need exact expr return | ||
if (! $return->expr instanceof Expr) { | ||
return null; | ||
} | ||
|
||
if (! $return->expr instanceof FuncCall) { | ||
return null; | ||
} | ||
|
||
$functionReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($return->expr); | ||
if (! $functionReflection instanceof FunctionReflection) { | ||
return null; | ||
} | ||
|
||
if (! $functionReflection->isBuiltin()) { | ||
return null; | ||
} | ||
|
||
$funcCalls[] = $return->expr; | ||
} | ||
|
||
return $funcCalls; | ||
} | ||
} |
Oops, something went wrong.