From 272ae1b3f58dd3c7a49834bef405b21de0584682 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Wed, 22 Jan 2025 09:37:20 +0100 Subject: [PATCH] Symfony: fix DIC filepath for phpstan-symfony 2.0.2 --- src/Provider/SymfonyUsageProvider.php | 22 ++++++++++++++++++---- tests/Rule/DeadCodeRuleTest.php | 14 ++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Provider/SymfonyUsageProvider.php b/src/Provider/SymfonyUsageProvider.php index 3d3046f..b4f3db2 100644 --- a/src/Provider/SymfonyUsageProvider.php +++ b/src/Provider/SymfonyUsageProvider.php @@ -10,11 +10,12 @@ use PhpParser\Node\Stmt\Return_; use PHPStan\Analyser\Scope; use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound; +use PHPStan\DependencyInjection\Container; +use PHPStan\DependencyInjection\ParameterNotFoundException; use PHPStan\Node\InClassNode; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ExtendedMethodReflection; use PHPStan\Reflection\MethodReflection; -use PHPStan\Symfony\Configuration as PHPStanSymfonyConfiguration; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; use ReflectionAttribute; @@ -63,16 +64,17 @@ class SymfonyUsageProvider implements MemberUsageProvider private array $dicConstants = []; public function __construct( - ?PHPStanSymfonyConfiguration $symfonyConfiguration, + Container $container, ?bool $enabled, ?string $configDir ) { $this->enabled = $enabled ?? $this->isSymfonyInstalled(); $resolvedConfigDir = $configDir ?? $this->autodetectConfigDir(); + $containerXmlPath = $this->getContainerXmlPath($container); - if ($this->enabled && $symfonyConfiguration !== null && $symfonyConfiguration->getContainerXmlPath() !== null) { // @phpstan-ignore phpstanApi.method - $this->fillDicClasses($symfonyConfiguration->getContainerXmlPath()); // @phpstan-ignore phpstanApi.method + if ($this->enabled && $containerXmlPath !== null) { + $this->fillDicClasses($containerXmlPath); } if ($this->enabled && $resolvedConfigDir !== null) { @@ -500,4 +502,16 @@ private function getConstantUsages(ClassReflection $classReflection): array return $usages; } + private function getContainerXmlPath(Container $container): ?string + { + try { + /** @var array{containerXmlPath: string|null} $symfonyConfig */ + $symfonyConfig = $container->getParameter('symfony'); + + return $symfonyConfig['containerXmlPath']; + } catch (ParameterNotFoundException $e) { + return null; + } + } + } diff --git a/tests/Rule/DeadCodeRuleTest.php b/tests/Rule/DeadCodeRuleTest.php index 5a9aab9..22fe3aa 100644 --- a/tests/Rule/DeadCodeRuleTest.php +++ b/tests/Rule/DeadCodeRuleTest.php @@ -11,7 +11,6 @@ use PHPStan\PhpDocParser\Lexer\Lexer; use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\Reflection\ReflectionProvider; -use PHPStan\Symfony\Configuration; use ReflectionMethod; use ShipMonk\PHPStan\DeadCode\Collector\ClassDefinitionCollector; use ShipMonk\PHPStan\DeadCode\Collector\ConstantFetchCollector; @@ -403,7 +402,7 @@ public function shouldMarkMethodAsUsed(ReflectionMethod $method): bool true, ), new SymfonyUsageProvider( - new Configuration(['containerXmlPath' => __DIR__ . '/data/providers/symfony/services.xml']), // @phpstan-ignore phpstanApi.constructor + $this->createContainerMockWithSymfonyConfig(), true, __DIR__ . '/data/providers/symfony/', ), @@ -477,4 +476,15 @@ public function gatherAnalyserErrors(array $files): array return $result; } + private function createContainerMockWithSymfonyConfig(): Container + { + $mock = $this->createMock(Container::class); + + $mock->expects(self::once()) + ->method('getParameter') + ->willReturn(['containerXmlPath' => __DIR__ . '/data/providers/symfony/services.xml']); + + return $mock; + } + }