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

Handle UnusedClass false positive #325

Merged
merged 2 commits into from
Nov 10, 2023
Merged
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
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
Loading