From 74b363449f693caf322ea2a55d879c4140e66a73 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Tue, 13 Jul 2021 11:43:26 +0200 Subject: [PATCH] feat: suppress well-known unused classes/methods --- src/Handlers/SuppressHandler.php | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Handlers/SuppressHandler.php b/src/Handlers/SuppressHandler.php index 8ef8491..51ef01c 100644 --- a/src/Handlers/SuppressHandler.php +++ b/src/Handlers/SuppressHandler.php @@ -7,10 +7,29 @@ use Illuminate\Foundation\Http\FormRequest; use Illuminate\Notifications\Notification; use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface; use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent; +use Psalm\Storage\MethodStorage; +use function array_key_exists; use function in_array; class SuppressHandler implements AfterClassLikeVisitInterface { + private const UNUSED_CLASSES = [ + "App\Console\Kernel", + "App\Exceptions\Handler", + "App\Http\Controllers\Controller", + "App\Http\Kernel", + "App\Http\Middleware\Authenticate", + "App\Http\Middleware\TrustHosts", + "App\Providers\AppServiceProvider", + "App\Providers\AuthServiceProvider", + "App\Providers\BroadcastServiceProvider", + "App\Providers\EventServiceProvider", + ]; + + private const UNUSED_METHODS = [ + "App\Http\Middleware\RedirectIfAuthenticated" => ["handle"], + ]; + public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event) { $storage = $event->getStorage(); @@ -36,5 +55,20 @@ class SuppressHandler implements AfterClassLikeVisitInterface if (in_array(Notification::class, $storage->parent_classes) && !in_array('PropertyNotSetInConstructor', $storage->suppressed_issues)) { $storage->suppressed_issues[] = 'PropertyNotSetInConstructor'; } + + // Suppress UnusedClass on well-known classes. + if (in_array($storage->name, self::UNUSED_CLASSES)) { + $storage->suppressed_issues[] = 'UnusedClass'; + } + + // Suppress PossiblyUnusedMethod on well-known methods. + if (array_key_exists($storage->name, self::UNUSED_METHODS)) { + foreach (self::UNUSED_METHODS[$storage->name] as $method_name) { + $method = $storage->methods[$method_name] ?? null; + if ($method instanceof MethodStorage) { + $method->suppressed_issues[] = 'PossiblyUnusedMethod'; + } + } + } } }