Skip to content
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
16 changes: 15 additions & 1 deletion src/Type/ContainerDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public function getTypeFromMethodCall(
}
}

// A dynamic service ID has no constant strings; unioning zero
// types would produce `never`.
if ($types === []) {
return $returnType;
}

return TypeCombinator::union(...$types);
} elseif ($methodName === 'get') {
$args = $methodCall->getArgs();
Expand All @@ -89,12 +95,20 @@ public function getTypeFromMethodCall(

$argType = $scope->getType($args[0]->value);

foreach ($argType->getConstantStrings() as $constantStringType) {
$constantStrings = $argType->getConstantStrings();
foreach ($constantStrings as $constantStringType) {
$serviceId = $constantStringType->getValue();
$service = $this->serviceMap->getService($serviceId);
$types[] = $service !== null ? $service->getType() : $returnType;
}

// A dynamic service ID has no constant strings; fall back to the
// declared return type so the union cannot collapse to `never`,
// while keeping any null added for NULL_ON_INVALID_REFERENCE.
if ($constantStrings === []) {
$types[] = $returnType;
}

return TypeCombinator::union(...$types);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/src/Rules/EntityStoragePropertyAssignmentRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public function testRule(): void
]
);
}

public function testBug1012(): void
{
$this->analyse([__DIR__ . '/data/bug-1012-proxy.php'], []);
}
}
33 changes: 33 additions & 0 deletions tests/src/Rules/data/bug-1012-proxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace mglaman\PHPStanDrupal\Tests\Rules\data;

use Symfony\Component\DependencyInjection\ContainerInterface;

// No error: core-generated proxy classes fetch the decorated service with a
// dynamic service ID (issue #1012).
class GeneratedProxyLazyLoadingService
{
protected ContainerInterface $container;

protected string $drupalProxyOriginalServiceId;

protected object $service;

public function __construct(ContainerInterface $container, string $drupal_proxy_original_service_id)
{
$this->container = $container;
$this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id;
}

protected function lazyLoadItself(): object
{
if (!isset($this->service)) {
$this->service = $this->container->get($this->drupalProxyOriginalServiceId);
}

return $this->service;
}
}
1 change: 1 addition & 0 deletions tests/src/Type/DrupalContainerDynamicReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static function dataFileAsserts(): iterable
yield from self::gatherAssertTypes(__DIR__ . '/data/drupal-service-static.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/drupal-class-resolver.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-563.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-1012.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/container-optional.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/synthetic.php');
}
Expand Down
15 changes: 15 additions & 0 deletions tests/src/Type/data/bug-1012.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Bug1012;

use Symfony\Component\DependencyInjection\ContainerInterface;
use function PHPStan\Testing\assertType;

function foo(string $service_id): void {
$container = \Drupal::getContainer();
assert($container !== null);

assertType('object', $container->get($service_id));
assertType('object|null', $container->get($service_id, ContainerInterface::NULL_ON_INVALID_REFERENCE));
assertType('bool', $container->has($service_id));
}
Loading