Skip to content
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

Symfony: fix DIC filepath for phpstan-symfony 2.0.2 #138

Merged
merged 1 commit into from
Jan 22, 2025
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
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;
}

}
Loading