Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support for parameterised service class #254

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

Merged
ondrejmirtes merged 5 commits into phpstan:master from Wirone:class-param-resolution
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Support for parameterised service class
Fixes #226 
  • Loading branch information
Grzegorz Korba authored and Wirone committed Feb 9, 2022
commit a0da7dc17ffe9ea99ddefc5ba0b352d58e55f645
33 changes: 31 additions & 2 deletions src/Type/Symfony/ServiceDynamicReturnTypeExtension.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\Symfony\Configuration;
use PHPStan\Symfony\ParameterMap;
use PHPStan\Symfony\ServiceDefinition;
use PHPStan\Symfony\ServiceMap;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function in_array;
use function is_scalar;
use function strpos;
use function trim;

final class ServiceDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
Expand All @@ -27,11 +32,20 @@ final class ServiceDynamicReturnTypeExtension implements DynamicMethodReturnType
/** @var ServiceMap */
private $serviceMap;

public function __construct(string $className, Configuration $configuration, ServiceMap $symfonyServiceMap)
/** @var ParameterMap */
private $parameterMap;

public function __construct(
string $className,
Configuration $configuration,
ServiceMap $symfonyServiceMap,
ParameterMap $symfonyParameterMap
)
{
$this->className = $className;
$this->constantHassers = $configuration->hasConstantHassers();
$this->serviceMap = $symfonyServiceMap;
$this->parameterMap = $symfonyParameterMap;
}

public function getClass(): string
Expand Down Expand Up @@ -70,7 +84,7 @@ private function getGetTypeFromMethodCall(
if ($serviceId !== null) {
$service = $this->serviceMap->getService($serviceId);
if ($service !== null && (!$service->isSynthetic() || $service->getClass() !== null)) {
return new ObjectType($service->getClass() ?? $serviceId);
return new ObjectType($this->determineServiceClass($service) ?? $serviceId);
}
}

Expand All @@ -97,4 +111,19 @@ private function getHasTypeFromMethodCall(
return $returnType;
}

private function determineServiceClass(ServiceDefinition $service): ?string
{
$class = $service->getClass();

if ($class !== null && strpos($class, '%') === 0) {
$param = $this->parameterMap->getParameter(trim($class, '%'));

if ($param !== null && is_scalar($param->getValue())) {
return (string) $param->getValue();
}
}

return $class;
}

}
2 changes: 2 additions & 0 deletions tests/Type/Symfony/container.xml
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="app.class">Foo</parameter>
<parameter key="app.string">abcdef</parameter>
<parameter key="app.int">123</parameter>
<parameter key="app.int_as_string" type="string">123</parameter>
Expand Down Expand Up @@ -51,6 +52,7 @@

<services>
<service id="foo" class="Foo" public="true"></service>
<service id="parameterised_foo" class="%app.class%"></service>
<service id="synthetic" class="Synthetic" public="true" synthetic="true" />
</services>
</container>
1 change: 1 addition & 0 deletions tests/Type/Symfony/data/ExampleAbstractController.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class ExampleAbstractController extends AbstractController
public function services(): void
{
assertType('Foo', $this->get('foo'));
assertType('Foo', $this->get('parameterised_foo'));
assertType('Synthetic', $this->get('synthetic'));
assertType('object', $this->get('bar'));
assertType('object', $this->get(doFoo()));
Expand Down
1 change: 1 addition & 0 deletions tests/Type/Symfony/data/ExampleController.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class ExampleController extends Controller
public function services(): void
{
assertType('Foo', $this->get('foo'));
assertType('Foo', $this->get('parameterised_foo'));
assertType('Synthetic', $this->get('synthetic'));
assertType('object', $this->get('bar'));
assertType('object', $this->get(doFoo()));
Expand Down

AltStyle によって変換されたページ (->オリジナル) /