-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
CacheInterface::get()
return type
- Loading branch information
Showing
3 changed files
with
73 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
48 changes: 48 additions & 0 deletions
48
src/Type/Symfony/CacheInterfaceGetDynamicReturnTypeExtension.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,48 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Symfony; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\GeneralizePrecision; | ||
use PHPStan\Type\Type; | ||
|
||
final class CacheInterfaceGetDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return 'Symfony\Contracts\Cache\CacheInterface'; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'get'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
{ | ||
if (!isset($methodCall->getArgs()[1])) { | ||
return null; | ||
} | ||
|
||
$callbackReturnType = $scope->getType($methodCall->getArgs()[1]->value); | ||
if ($callbackReturnType->isCallable()->yes()) { | ||
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( | ||
$scope, | ||
$methodCall->getArgs(), | ||
$callbackReturnType->getCallableParametersAcceptors($scope) | ||
); | ||
$returnType = $parametersAcceptor->getReturnType(); | ||
|
||
// generalize template parameters | ||
return $returnType->generalize(GeneralizePrecision::templateArgument()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
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