Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions src/Hooks/CoversTargetHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
41 changes: 32 additions & 9 deletions src/Hooks/DataProviderNarrowingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
*
Expand Down
25 changes: 19 additions & 6 deletions src/Hooks/TestCaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
41 changes: 41 additions & 0 deletions tests/acceptance/CoversTargets.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions tests/acceptance/Initializers.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down