Skip to content

Commit cfbc483

Browse files
authored
[container] don't report service constructors as PossiblyUnusedMethod (#326)
1 parent 2a3f81e commit cfbc483

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/Handler/ContainerHandler.php

+27-6
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface;
1212
use Psalm\Plugin\EventHandler\AfterCodebasePopulatedInterface;
1313
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
14+
use Psalm\Plugin\EventHandler\BeforeAddIssueInterface;
1415
use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;
1516
use Psalm\Plugin\EventHandler\Event\AfterCodebasePopulatedEvent;
1617
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
18+
use Psalm\Plugin\EventHandler\Event\BeforeAddIssueEvent;
1719
use Psalm\SymfonyPsalmPlugin\Issue\NamingConventionViolation;
1820
use Psalm\SymfonyPsalmPlugin\Issue\PrivateService;
1921
use Psalm\SymfonyPsalmPlugin\Issue\ServiceNotFound;
@@ -22,7 +24,7 @@
2224
use Psalm\Type\Union;
2325
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2426

25-
class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLikeVisitInterface, AfterCodebasePopulatedInterface
27+
class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLikeVisitInterface, AfterCodebasePopulatedInterface, BeforeAddIssueInterface
2628
{
2729
private const GET_CLASSLIKES = [
2830
'Psr\Container\ContainerInterface',
@@ -38,9 +40,18 @@ class ContainerHandler implements AfterMethodCallAnalysisInterface, AfterClassLi
3840
*/
3941
private static $containerMeta;
4042

43+
/**
44+
* @var array<string> collection of cower-cased class names that are present in the container
45+
*/
46+
private static array $containerClassNames = [];
47+
4148
public static function init(ContainerMeta $containerMeta): void
4249
{
4350
self::$containerMeta = $containerMeta;
51+
52+
self::$containerClassNames = array_map(function (string $className): string {
53+
return strtolower($className);
54+
}, self::$containerMeta->getClassNames());
4455
}
4556

4657
public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
@@ -173,17 +184,27 @@ public static function afterCodebasePopulated(AfterCodebasePopulatedEvent $event
173184
return;
174185
}
175186

176-
$containerClassNames = array_map(function (string $className): string {
177-
return strtolower($className);
178-
}, self::$containerMeta->getClassNames());
179-
180187
foreach ($event->getCodebase()->classlike_storage_provider->getAll() as $name => $storage) {
181-
if (in_array($name, $containerClassNames, true)) {
188+
if (in_array($name, self::$containerClassNames, true)) {
182189
$storage->suppressed_issues[] = 'UnusedClass';
183190
}
184191
}
185192
}
186193

194+
public static function beforeAddIssue(BeforeAddIssueEvent $event): ?bool
195+
{
196+
$data = $event->getIssue()->toIssueData('error');
197+
if ('PossiblyUnusedMethod' === $data->type
198+
&& '__construct' === $data->selected_text
199+
&& null !== $data->dupe_key
200+
&& in_array(preg_replace('/::__construct$/', '', $data->dupe_key), self::$containerClassNames, true)) {
201+
// Don't report service constructors as PossiblyUnusedMethod
202+
return false;
203+
}
204+
205+
return null;
206+
}
207+
187208
public static function isContainerMethod(string $declaringMethodId, string $methodName): bool
188209
{
189210
return in_array(

0 commit comments

Comments
 (0)