Skip to content

Commit

Permalink
Symfony: fix DIC filepath for phpstan-symfony 2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Jan 22, 2025
1 parent 478ecfa commit 272ae1b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/Provider/SymfonyUsageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}

}
14 changes: 12 additions & 2 deletions tests/Rule/DeadCodeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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/',
),
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 272ae1b

Please sign in to comment.