Skip to content
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

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Copy link
Owner

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 ?

Copy link
Contributor

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.

$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) {
Copy link
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the type is ConstantStringType then you can ask for $argType->getValue()

$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;
Expand Down
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
}
]
15 changes: 14 additions & 1 deletion tests/LaminasIntegration/data/pluginMethodDynamicReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Laminas\Router\SimpleRouteStack;
use Laminas\View\Renderer\PhpRenderer;
use Laminas\View\Helper\AbstractHelper;

final class pluginMethodDynamicReturn
{
Expand All @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes we would like to catch here the raw value of $name variable, which is 'url' from few lines above, can we?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thank you

}

}