-
-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Plugin method should accept string variable as argument #11
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,13 @@ | |
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeUtils; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
abstract class AbstractPluginMethodDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
|
@@ -36,18 +39,25 @@ final public function getTypeFromMethodCall( | |
MethodCall $methodCall, | ||
Scope $scope | ||
): Type { | ||
$pluginManager = $this->serviceManagerLoader->getServiceLocator($this->getPluginManagerName()); | ||
$argType = $scope->getType($methodCall->args[0]->value); | ||
if (! $argType instanceof ConstantStringType) { | ||
throw new \PHPStan\ShouldNotHappenException(\sprintf('Argument passed to %s::%s should be a string, %s given', | ||
$methodReflection->getDeclaringClass()->getName(), | ||
$methodReflection->getName(), | ||
$argType->describe(VerbosityLevel::typeOnly()) | ||
)); | ||
$argType = $scope->getType($methodCall->args[0]->value); | ||
$strings = TypeUtils::getConstantStrings($argType); | ||
$plugin = count($strings) === 1 ? $strings[0]->getValue() : null; | ||
|
||
if ($plugin) { | ||
$pluginManager = $this->serviceManagerLoader->getServiceLocator($this->getPluginManagerName()); | ||
return new ObjectType(\get_class($pluginManager->get($plugin))); | ||
} | ||
$pluginClass = \get_class($pluginManager->get($argType->getValue())); | ||
|
||
return new ObjectType($pluginClass); | ||
if ($argType instanceof StringType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way in this case to get the string raw value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the type is |
||
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->args, $methodReflection->getVariants())->getReturnType(); | ||
return $defaultReturnType; | ||
} | ||
|
||
throw new \PHPStan\ShouldNotHappenException(\sprintf('Argument passed to %s::%s should be a string, %s given', | ||
$methodReflection->getDeclaringClass()->getName(), | ||
$methodReflection->getName(), | ||
$argType->describe(VerbosityLevel::value()) | ||
)); | ||
} | ||
|
||
abstract protected function getPluginManagerName(): string; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
[ | ||
{ | ||
"message": "Call to an undefined method Laminas\\View\\Helper\\Url::foobar().", | ||
"line": 21, | ||
"line": 22, | ||
"ignorable": true | ||
}, | ||
{ | ||
"message": "Call to an undefined method Laminas\\View\\Helper\\AbstractHelper::foobar().", | ||
"line": 28, | ||
"ignorable": true | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
use Laminas\Router\SimpleRouteStack; | ||
use Laminas\View\Renderer\PhpRenderer; | ||
use Laminas\View\Helper\AbstractHelper; | ||
|
||
final class pluginMethodDynamicReturn | ||
{ | ||
|
@@ -14,10 +15,22 @@ final class pluginMethodDynamicReturn | |
*/ | ||
private $phpRenderer; | ||
|
||
public function getDynamicType(): void | ||
public function getDynamicTypeFromStaticString(): void | ||
{ | ||
$urlHelper = $this->phpRenderer->plugin('url'); | ||
$urlHelper->setRouter(new SimpleRouteStack()); | ||
$urlHelper->foobar(); | ||
} | ||
|
||
public function callGetDynamicTypeFromVariable(): void | ||
{ | ||
$urlHelper = $this->getDynamicTypeFromStringVariable('url'); | ||
$urlHelper->foobar(); | ||
} | ||
|
||
public function getDynamicTypeFromStringVariable(string $name): AbstractHelper | ||
{ | ||
return $this->phpRenderer->plugin($name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ondrejmirtes we would like to catch here the raw value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can see the literal values only in the same method body, not across method boundaries. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok thank you |
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@neilime why not simply
$argType instanceof ConstantStringType
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually better. It accounts for
'foo'|'bar'
etc.