From e5c83c9092065571c495837a2f7337b5edbe2a88 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin <5278175+alies-dev@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:50:37 +0100 Subject: [PATCH 1/3] Find the initializers of a class without a getMethod() lookup per method getBeforeMethods() makes one pass over the methods of the class instead of a linear ClassLike::getMethod() per method, and hasInitializers() reads the before methods of each declaring class once. --- src/Hooks/TestCaseHandler.php | 25 +++++++++++++++++++------ tests/acceptance/Initializers.feature | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/Hooks/TestCaseHandler.php b/src/Hooks/TestCaseHandler.php index 94ca7c0..6ac903c 100644 --- a/src/Hooks/TestCaseHandler.php +++ b/src/Hooks/TestCaseHandler.php @@ -309,6 +309,7 @@ public static function isInPaths(string $file_path, array $paths): bool */ private static function hasInitializers(Codebase $codebase, ClassLikeStorage $storage): bool { + $before_by_class = []; foreach ($storage->declaring_method_ids as $name_lc => $method_id) { if ($name_lc === 'setup') { if (\strtolower($method_id->fq_class_name) !== 'phpunit\framework\testcase') { @@ -317,9 +318,14 @@ private static function hasInitializers(Codebase $codebase, ClassLikeStorage $st continue; } - $declaring = $codebase->classlike_storage_provider->get($method_id->fq_class_name); - $meta = (array) ($declaring->custom_metadata[__NAMESPACE__] ?? []); - if (isset($meta['before'][$method_id->method_name])) { + // Most methods come from a handful of declaring classes + if (!isset($before_by_class[$method_id->fq_class_name])) { + $declaring = $codebase->classlike_storage_provider->get($method_id->fq_class_name); + $meta = (array) ($declaring->custom_metadata[__NAMESPACE__] ?? []); + $before_by_class[$method_id->fq_class_name] = (array) ($meta['before'] ?? []); + } + + if (isset($before_by_class[$method_id->fq_class_name][$method_id->method_name])) { return true; } } @@ -1416,9 +1422,16 @@ public static function unionizeIterables(Codebase $codebase, Type\Union $iterabl private static function getBeforeMethods(ClassLikeStorage $storage, ClassLike $stmt, Aliases $aliases): array { $before = []; - foreach ($storage->methods as $method => $_) { - $stmt_method = $stmt->getMethod($method); - if ($stmt_method && self::isBeforeInitializer($stmt_method, $aliases)) { + $seen = []; + // Only the first method of a duplicated name counts, as for ClassLike::getMethod() + foreach ($stmt->getMethods() as $stmt_method) { + $method = $stmt_method->name->toLowerString(); + if (isset($seen[$method]) || !isset($storage->methods[$method])) { + continue; + } + + $seen[$method] = true; + if (self::isBeforeInitializer($stmt_method, $aliases)) { $before[$method] = true; } } diff --git a/tests/acceptance/Initializers.feature b/tests/acceptance/Initializers.feature index eb3be41..bae3713 100644 --- a/tests/acceptance/Initializers.feature +++ b/tests/acceptance/Initializers.feature @@ -64,6 +64,26 @@ Feature: MissingConstructor in stateful test cases When I run Psalm Then I see no errors + Scenario: A mixed-case @before method produces no MissingConstructor + Given I have the following code + """ + final class MyTestCase extends TestCase + { + private int $cents; + + /** @before */ + public function InitCents(): void { + $this->cents = 100; + } + + public function testCents(): void { + $this->assertSame(100, $this->cents); + } + } + """ + When I run Psalm + Then I see no errors + Scenario: Stateful test case without @before or setUp produces MissingConstructor Given I have the following code """ From 240e4b7c1b7e64df5fe4b282a895414aa6cb0152 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin <5278175+alies-dev@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:50:37 +0100 Subject: [PATCH 2/3] Look the analysed test up by its name before scanning every method of the class --- src/Hooks/DataProviderNarrowingHandler.php | 41 +++++++++++++++++----- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/src/Hooks/DataProviderNarrowingHandler.php b/src/Hooks/DataProviderNarrowingHandler.php index 614634d..6c1eadb 100644 --- a/src/Hooks/DataProviderNarrowingHandler.php +++ b/src/Hooks/DataProviderNarrowingHandler.php @@ -6,6 +6,7 @@ use Psalm\Codebase; use Psalm\Internal\Analyzer\MethodAnalyzer; +use Psalm\Internal\MethodIdentifier; use Psalm\PhpUnitPlugin\Dataset\DatasetBinding; use Psalm\Plugin\EventHandler\BeforeStatementAnalysisInterface; use Psalm\Plugin\EventHandler\Event\BeforeStatementAnalysisEvent; @@ -64,7 +65,7 @@ public static function beforeStatementAnalysis(BeforeStatementAnalysisEvent $eve return null; } - $method_id = (string) $method_analyzer->getMethodId(); + $method_id = $method_analyzer->getMethodId(); $marked = (bool) $metadata['test']; $class = $codebase->classlike_storage_provider->get($self); if (!self::isTest($codebase, $class, $storage, $method_id, $marked)) { @@ -142,18 +143,26 @@ private static function isTest( Codebase $codebase, ClassLikeStorage $class, MethodStorage $method, - string $method_id, + MethodIdentifier $method_id, bool $marked, ): bool { - $method_id = \strtolower($method_id); - foreach ($class->declaring_method_ids as $name => $declaring_id) { - if (\strtolower((string) $declaring_id) !== $method_id) { - continue; - } + $method_id_lc = \strtolower((string) $method_id); + + // The usual case: the class declares the method itself under the name it is analysed with + $own_name = $method_id->method_name; + $own_id = $class->declaring_method_ids[$own_name] ?? null; + if ( + $own_id !== null + && \strtolower((string) $own_id) === $method_id_lc + && self::isTestEntry($codebase, $class, $method, $own_name, $marked) + ) { + return true; + } + foreach ($class->declaring_method_ids as $name => $declaring_id) { if ( - ($marked || \str_starts_with(self::casedName($class, $method, $name), 'test')) - && TestCaseHandler::isPublicMethod($codebase, $class, $method, $name) + \strtolower((string) $declaring_id) === $method_id_lc + && self::isTestEntry($codebase, $class, $method, $name, $marked) ) { return true; } @@ -162,6 +171,20 @@ private static function isTest( return false; } + /** + * @param lowercase-string $name + */ + private static function isTestEntry( + Codebase $codebase, + ClassLikeStorage $class, + MethodStorage $method, + string $name, + bool $marked, + ): bool { + return ($marked || \str_starts_with(self::casedName($class, $method, $name), 'test')) + && TestCaseHandler::isPublicMethod($codebase, $class, $method, $name); + } + /** * @param lowercase-string $name * From d36f496dc8ef54c0c359d377d250f832f3b04dfc Mon Sep 17 00:00:00 2001 From: Alies Lapatsin <5278175+alies-dev@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:50:37 +0100 Subject: [PATCH 3/3] Read the tagged methods of each declaring class once when checking docblock covers targets --- src/Hooks/CoversTargetHandler.php | 14 +++++++-- tests/acceptance/CoversTargets.feature | 41 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/Hooks/CoversTargetHandler.php b/src/Hooks/CoversTargetHandler.php index aa13772..f2b9ede 100644 --- a/src/Hooks/CoversTargetHandler.php +++ b/src/Hooks/CoversTargetHandler.php @@ -261,9 +261,19 @@ private static function validateDocblockTargets( return; } + $declaring_classes = []; foreach ($storage->declaring_method_ids as $declaring_method_id) { - $declaring_storage = $codebase->classlike_storage_provider->get($declaring_method_id->fq_class_name); - $tags = self::metadata($declaring_storage)['methods'][$declaring_method_id->method_name] ?? []; + $declaring_class = $declaring_method_id->fq_class_name; + if (!isset($declaring_classes[$declaring_class])) { + $declaring_storage = $codebase->classlike_storage_provider->get($declaring_class); + $declaring_classes[$declaring_class] = [ + $declaring_storage, + self::metadata($declaring_storage)['methods'], + ]; + } + + [$declaring_storage, $declaring_methods] = $declaring_classes[$declaring_class]; + $tags = $declaring_methods[$declaring_method_id->method_name] ?? []; if ($tags === []) { continue; } diff --git a/tests/acceptance/CoversTargets.feature b/tests/acceptance/CoversTargets.feature index 29947a8..ffe0b54 100644 --- a/tests/acceptance/CoversTargets.feature +++ b/tests/acceptance/CoversTargets.feature @@ -537,3 +537,44 @@ Feature: Coverage targets """ When I run Psalm Then I see no errors + + Scenario: Every tagged method of each declaring class is validated, also next to untagged ones + Given I have the following code + """ + abstract class BaseTestCase extends TestCase { + /** + * @covers ::missingFirst + * @psalm-capabilities read-props + */ + public function testFirst(): void {} + + /** @psalm-capabilities read-props */ + public function testPlain(): void {} + + /** + * @uses ::missingSecond + * @psalm-capabilities read-props + */ + public function testSecond(): void {} + } + + abstract class MiddleTestCase extends BaseTestCase { + /** @psalm-capabilities read-props */ + public function testMiddle(): void {} + } + + final class ConcreteTestCase extends MiddleTestCase { + /** + * @covers ::missingOwn + * @psalm-capabilities read-props + */ + public function testOwn(): void {} + } + """ + When I run Psalm + Then I see these errors + | Type | Message | Line | + | UndefinedFunction | @covers target ::missingFirst is not defined | 7 | + | UndefinedFunction | @uses target ::missingSecond is not defined | 16 | + | UndefinedFunction | @covers target ::missingOwn is not defined | 29 | + And I see no other errors