From 8d7446eb48d48ee6bff01f9a41582b5cf5528464 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Sun, 6 Jun 2021 10:46:01 +0300 Subject: [PATCH] Upgrade Psalm interfaces Switch from legacy plugin hook interfaces to event handler interfaces --- composer.json | 2 +- src/Hooks/TestCaseHandler.php | 47 +++++++++++++++++------------------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index f4a9dda..5ff36fa 100755 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "ext-simplexml": "*", "composer/semver": "^1.4 || ^2.0 || ^3.0", "composer/package-versions-deprecated": "^1.10", - "vimeo/psalm": "dev-master || dev-4.x || ^4.0" + "vimeo/psalm": "dev-master || dev-4.x || ^4.5" }, "conflict": { "phpunit/phpunit": "<7.5" diff --git a/src/Hooks/TestCaseHandler.php b/src/Hooks/TestCaseHandler.php index 273868a..fb67d6a 100644 --- a/src/Hooks/TestCaseHandler.php +++ b/src/Hooks/TestCaseHandler.php @@ -10,14 +10,15 @@ use Psalm\Codebase; use Psalm\DocComment; use Psalm\Exception\DocblockParseException; -use Psalm\FileSource; use Psalm\IssueBuffer; use Psalm\Issue; use Psalm\PhpUnitPlugin\VersionUtils; -use Psalm\Plugin\Hook\AfterClassLikeAnalysisInterface; -use Psalm\Plugin\Hook\AfterClassLikeVisitInterface; -use Psalm\Plugin\Hook\AfterCodebasePopulatedInterface; -use Psalm\StatementsSource; +use Psalm\Plugin\EventHandler\AfterClassLikeAnalysisInterface; +use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface; +use Psalm\Plugin\EventHandler\AfterCodebasePopulatedInterface; +use Psalm\Plugin\EventHandler\Event\AfterClassLikeAnalysisEvent; +use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent; +use Psalm\Plugin\EventHandler\Event\AfterCodebasePopulatedEvent; use Psalm\Storage\ClassLikeStorage; use Psalm\Type; use Psalm\Type\Atomic\TNull; @@ -31,8 +32,10 @@ class TestCaseHandler implements /** * {@inheritDoc} */ - public static function afterCodebasePopulated(Codebase $codebase) + public static function afterCodebasePopulated(AfterCodebasePopulatedEvent $event) { + $codebase = $event->getCodebase(); + foreach ($codebase->classlike_storage_provider->getAll() as $name => $storage) { $meta = (array) ($storage->custom_metadata[__NAMESPACE__] ?? []); if ($codebase->classExtends($name, 'PHPUnit\Framework\TestCase') && ($meta['hasInitializers'] ?? false)) { @@ -68,15 +71,13 @@ private static function getDescendants(Codebase $codebase, string $name): array /** * {@inheritDoc} */ - public static function afterClassLikeVisit( - ClassLike $stmt, - ClassLikeStorage $storage, - FileSource $statements_source, - Codebase $codebase, - array &$file_replacements = [] - ) { - $class_node = $stmt; - $class_storage = $storage; + public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event) + { + $class_node = $event->getStmt(); + $class_storage = $event->getStorage(); + $statements_source = $event->getStatementsSource(); + $codebase = $event->getCodebase(); + if (self::hasInitializers($class_storage, $class_node)) { $class_storage->custom_metadata[__NAMESPACE__] = ['hasInitializers' => true]; } @@ -103,15 +104,13 @@ public static function afterClassLikeVisit( /** * {@inheritDoc} */ - public static function afterStatementAnalysis( - ClassLike $stmt, - ClassLikeStorage $classlike_storage, - StatementsSource $statements_source, - Codebase $codebase, - array &$file_replacements = [] - ) { - $class_node = $stmt; - $class_storage = $classlike_storage; + public static function afterStatementAnalysis(AfterClassLikeAnalysisEvent $event) + { + $class_node = $event->getStmt(); + $class_storage = $event->getClasslikeStorage(); + $codebase = $event->getCodebase(); + $statements_source = $event->getStatementsSource(); + if (!$codebase->classExtends($class_storage->name, 'PHPUnit\Framework\TestCase')) { return null; }