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 3 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
2 changes: 2 additions & 0 deletions composer.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"phpstan/phpstan": "^1.4"
},
"conflict": {
"psr/container": "1.1.2",
"symfony/framework-bundle": "<3.0"
},
"require-dev": {
Expand All @@ -28,6 +29,7 @@
"phpunit/phpunit": "^9.5",
"symfony/config": "^4.2 || ^5.0",
"symfony/console": "^4.0 || ^5.0",
"symfony/dependency-injection": "^4.0 || ^5.0",
"symfony/form": "^4.0 || ^5.0",
"symfony/framework-bundle": "^4.4 || ^5.0",
"symfony/http-foundation": "^5.1",
Expand Down
32 changes: 30 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,11 +8,14 @@
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 Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use function in_array;

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

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

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

public function getClass(): string
Expand Down Expand Up @@ -70,7 +82,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 +109,20 @@ private function getHasTypeFromMethodCall(
return $returnType;
}

private function determineServiceClass(ServiceDefinition $service): ?string
{
return $this->parameterBag->resolveValue($service->getClass());
}

private function createParameterBag(ParameterMap $symfonyParameterMap): ParameterBag
{
$parameters = [];

foreach ($symfonyParameterMap->getParameters() as $parameterDefinition) {
$parameters[$parameterDefinition->getKey()] = $parameterDefinition->getValue();
}

return new ParameterBag($parameters);
}

}
3 changes: 3 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,8 @@

<services>
<service id="foo" class="Foo" public="true"></service>
<service id="parameterised_foo" class="%app.class%"></service>
<service id="parameterised_bar" class="%app.class%\Bar"></service>
<service id="synthetic" class="Synthetic" public="true" synthetic="true" />
</services>
</container>
2 changes: 2 additions & 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,8 @@ final class ExampleAbstractController extends AbstractController
public function services(): void
{
assertType('Foo', $this->get('foo'));
assertType('Foo', $this->get('parameterised_foo'));
assertType('Foo\Bar', $this->get('parameterised_bar'));
assertType('Synthetic', $this->get('synthetic'));
assertType('object', $this->get('bar'));
assertType('object', $this->get(doFoo()));
Expand Down
2 changes: 2 additions & 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,8 @@ final class ExampleController extends Controller
public function services(): void
{
assertType('Foo', $this->get('foo'));
assertType('Foo', $this->get('parameterised_foo'));
assertType('Foo\Bar', $this->get('parameterised_bar'));
assertType('Synthetic', $this->get('synthetic'));
assertType('object', $this->get('bar'));
assertType('object', $this->get(doFoo()));
Expand Down

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