From 93cf6805e45e55c35214e90dec23d34360fdf680 Mon Sep 17 00:00:00 2001 From: Farhad Safarov Date: Fri, 10 Nov 2023 09:10:57 +0300 Subject: [PATCH 1/2] Handle UnusedClass false positive --- src/Handler/ContainerHandler.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Handler/ContainerHandler.php b/src/Handler/ContainerHandler.php index a4ce5a8..b49acbc 100644 --- a/src/Handler/ContainerHandler.php +++ b/src/Handler/ContainerHandler.php @@ -9,8 +9,10 @@ use Psalm\CodeLocation; use Psalm\IssueBuffer; use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface; +use Psalm\Plugin\EventHandler\AfterCodebasePopulatedInterface; use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface; use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent; +use Psalm\Plugin\EventHandler\Event\AfterCodebasePopulatedEvent; use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent; use Psalm\SymfonyPsalmPlugin\Issue\NamingConventionViolation; use Psalm\SymfonyPsalmPlugin\Issue\PrivateService; @@ -20,7 +22,7 @@ use Psalm\Type\Union; use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; -class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLikeVisitInterface +class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLikeVisitInterface, AfterCodebasePopulatedInterface { private const GET_CLASSLIKES = [ 'Psr\Container\ContainerInterface', @@ -165,6 +167,19 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event) } } + public static function afterCodebasePopulated(AfterCodebasePopulatedEvent $event): void + { + $containerClassNames = array_map(function (string $className): string { + return strtolower($className); + }, self::$containerMeta->getClassNames()); + + foreach ($event->getCodebase()->classlike_storage_provider->getAll() as $name => $storage) { + if (in_array($name, $containerClassNames, true)) { + $storage->suppressed_issues[] = 'UnusedClass'; + } + } + } + public static function isContainerMethod(string $declaringMethodId, string $methodName): bool { return in_array( From 9a9a90c4d80c79866d3e041535fd09b844c57543 Mon Sep 17 00:00:00 2001 From: Farhad Safarov Date: Fri, 10 Nov 2023 10:07:29 +0300 Subject: [PATCH 2/2] skip if container xml is not provided --- src/Handler/ContainerHandler.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Handler/ContainerHandler.php b/src/Handler/ContainerHandler.php index b49acbc..cdc19d0 100644 --- a/src/Handler/ContainerHandler.php +++ b/src/Handler/ContainerHandler.php @@ -169,6 +169,10 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event) public static function afterCodebasePopulated(AfterCodebasePopulatedEvent $event): void { + if (null === self::$containerMeta) { + return; + } + $containerClassNames = array_map(function (string $className): string { return strtolower($className); }, self::$containerMeta->getClassNames());