-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add MethodCall to ExclusiveNativeCallLikeReturnMatcher
- Loading branch information
1 parent
91ce295
commit 34be2ee
Showing
5 changed files
with
80 additions
and
67 deletions.
There are no files selected for viewing
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