-
-
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.
[StrictTypes] Add MethodCall/StaticCall to ExclusiveNativeCallLikeRet…
…urnMatcher (#2646)
- Loading branch information
1 parent
f9916a1
commit fba410b
Showing
13 changed files
with
138 additions
and
81 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
16 changes: 16 additions & 0 deletions
16
...tor/ClassMethod/ReturnTypeFromStrictNativeCallRector/Fixture/skip_custom_function.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,16 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRector\Fixture; | ||
|
||
function custom_function(): string | ||
{ | ||
return 'yes'; | ||
} | ||
|
||
final class SkipCustomFunction | ||
{ | ||
public function run() | ||
{ | ||
return custom_function(); | ||
} | ||
} |
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
.../ClassMethod/ReturnTypeFromStrictNativeCallRector/Fixture/some_native_method_call.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\ReturnTypeFromStrictNativeCallRector\Fixture; | ||
|
||
final class SomeNativeMethodCall | ||
{ | ||
public function run(\SplFileInfo $splFileInfo) | ||
{ | ||
return $splFileInfo->isDir(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRector\Fixture; | ||
|
||
final class SomeNativeMethodCall | ||
{ | ||
public function run(\SplFileInfo $splFileInfo): bool | ||
{ | ||
return $splFileInfo->isDir(); | ||
} | ||
} | ||
|
||
?> |
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
65 changes: 65 additions & 0 deletions
65
rules/TypeDeclaration/NodeAnalyzer/ReturnFilter/ExclusiveNativeCallLikeReturnMatcher.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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\NodeAnalyzer\ReturnFilter; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Stmt\Return_; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\MethodReflection; | ||
use Rector\Core\Reflection\ReflectionResolver; | ||
|
||
final class ExclusiveNativeCallLikeReturnMatcher | ||
{ | ||
public function __construct( | ||
private readonly ReflectionResolver $reflectionResolver, | ||
) { | ||
} | ||
|
||
/** | ||
* @param Return_[] $returns | ||
* @return array<StaticCall|FuncCall|MethodCall>|null | ||
*/ | ||
public function match(array $returns): array|null | ||
{ | ||
$callLikes = []; | ||
|
||
foreach ($returns as $return) { | ||
// we need exact expr return | ||
$returnExpr = $return->expr; | ||
if (! $returnExpr instanceof StaticCall && ! $returnExpr instanceof MethodCall && ! $returnExpr instanceof FuncCall) { | ||
return null; | ||
} | ||
|
||
$functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($returnExpr); | ||
|
||
if (! $functionLikeReflection instanceof FunctionReflection && ! $functionLikeReflection instanceof MethodReflection) { | ||
return null; | ||
} | ||
|
||
// is native func call? | ||
if (! $this->isNativeCallLike($functionLikeReflection)) { | ||
return null; | ||
} | ||
|
||
$callLikes[] = $returnExpr; | ||
} | ||
|
||
return $callLikes; | ||
} | ||
|
||
private function isNativeCallLike(MethodReflection|FunctionReflection $functionLikeReflection): bool | ||
{ | ||
if ($functionLikeReflection instanceof FunctionReflection) { | ||
return $functionLikeReflection->isBuiltin(); | ||
} | ||
|
||
// is native method call? | ||
$classReflection = $functionLikeReflection->getDeclaringClass(); | ||
return $classReflection->isBuiltin(); | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
rules/TypeDeclaration/NodeAnalyzer/ReturnFilter/ExclusiveNativeFuncCallReturnMatcher.php
This file was deleted.
Oops, something went wrong.
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