Skip to content

Commit

Permalink
Handle UnusedClass false positive (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
seferov authored Nov 10, 2023
1 parent 517205b commit 2a3f81e
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Handler/ContainerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand Down Expand Up @@ -165,6 +167,23 @@ 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());

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(
Expand Down

0 comments on commit 2a3f81e

Please sign in to comment.