Skip to content

Commit 1935d9b

Browse files
authored
Symfony: fix DIC filepath for phpstan-symfony 2.0.2 (#138)
1 parent 478ecfa commit 1935d9b

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/Provider/SymfonyUsageProvider.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
use PhpParser\Node\Stmt\Return_;
1111
use PHPStan\Analyser\Scope;
1212
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
13+
use PHPStan\DependencyInjection\Container;
14+
use PHPStan\DependencyInjection\ParameterNotFoundException;
1315
use PHPStan\Node\InClassNode;
1416
use PHPStan\Reflection\ClassReflection;
1517
use PHPStan\Reflection\ExtendedMethodReflection;
1618
use PHPStan\Reflection\MethodReflection;
17-
use PHPStan\Symfony\Configuration as PHPStanSymfonyConfiguration;
1819
use RecursiveDirectoryIterator;
1920
use RecursiveIteratorIterator;
2021
use ReflectionAttribute;
@@ -63,16 +64,17 @@ class SymfonyUsageProvider implements MemberUsageProvider
6364
private array $dicConstants = [];
6465

6566
public function __construct(
66-
?PHPStanSymfonyConfiguration $symfonyConfiguration,
67+
Container $container,
6768
?bool $enabled,
6869
?string $configDir
6970
)
7071
{
7172
$this->enabled = $enabled ?? $this->isSymfonyInstalled();
7273
$resolvedConfigDir = $configDir ?? $this->autodetectConfigDir();
74+
$containerXmlPath = $this->getContainerXmlPath($container);
7375

74-
if ($this->enabled && $symfonyConfiguration !== null && $symfonyConfiguration->getContainerXmlPath() !== null) { // @phpstan-ignore phpstanApi.method
75-
$this->fillDicClasses($symfonyConfiguration->getContainerXmlPath()); // @phpstan-ignore phpstanApi.method
76+
if ($this->enabled && $containerXmlPath !== null) {
77+
$this->fillDicClasses($containerXmlPath);
7678
}
7779

7880
if ($this->enabled && $resolvedConfigDir !== null) {
@@ -500,4 +502,16 @@ private function getConstantUsages(ClassReflection $classReflection): array
500502
return $usages;
501503
}
502504

505+
private function getContainerXmlPath(Container $container): ?string
506+
{
507+
try {
508+
/** @var array{containerXmlPath: string|null} $symfonyConfig */
509+
$symfonyConfig = $container->getParameter('symfony');
510+
511+
return $symfonyConfig['containerXmlPath'];
512+
} catch (ParameterNotFoundException $e) {
513+
return null;
514+
}
515+
}
516+
503517
}

tests/Rule/DeadCodeRuleTest.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PHPStan\PhpDocParser\Lexer\Lexer;
1212
use PHPStan\PhpDocParser\Parser\PhpDocParser;
1313
use PHPStan\Reflection\ReflectionProvider;
14-
use PHPStan\Symfony\Configuration;
1514
use ReflectionMethod;
1615
use ShipMonk\PHPStan\DeadCode\Collector\ClassDefinitionCollector;
1716
use ShipMonk\PHPStan\DeadCode\Collector\ConstantFetchCollector;
@@ -403,7 +402,7 @@ public function shouldMarkMethodAsUsed(ReflectionMethod $method): bool
403402
true,
404403
),
405404
new SymfonyUsageProvider(
406-
new Configuration(['containerXmlPath' => __DIR__ . '/data/providers/symfony/services.xml']), // @phpstan-ignore phpstanApi.constructor
405+
$this->createContainerMockWithSymfonyConfig(),
407406
true,
408407
__DIR__ . '/data/providers/symfony/',
409408
),
@@ -477,4 +476,15 @@ public function gatherAnalyserErrors(array $files): array
477476
return $result;
478477
}
479478

479+
private function createContainerMockWithSymfonyConfig(): Container
480+
{
481+
$mock = $this->createMock(Container::class);
482+
483+
$mock->expects(self::once())
484+
->method('getParameter')
485+
->willReturn(['containerXmlPath' => __DIR__ . '/data/providers/symfony/services.xml']);
486+
487+
return $mock;
488+
}
489+
480490
}

0 commit comments

Comments
 (0)