Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add-support-for-attributes #146

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
83 changes: 47 additions & 36 deletions src/Hooks/TestCaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;
use Psalm\Plugin\EventHandler\Event\AfterCodebasePopulatedEvent;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\MethodStorage;
use Psalm\Type;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Union;
Expand Down Expand Up @@ -88,8 +89,12 @@ public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
$file_path = $statements_source->getFilePath();
$file_storage = $codebase->file_storage_provider->get($file_path);

foreach ($class_node->getMethods() as $method) {
$specials = self::getSpecials($method);
foreach ($event->getStorage()->methods as $method_name_lc => $method_storage) {
$method_node = $class_node->getMethod($method_name_lc);
if ($method_node === null) {
continue;
}
$specials = self::getSpecials($method_storage, $method_node);
if (!isset($specials['dataProvider'])) {
continue;
}
Expand Down Expand Up @@ -152,7 +157,7 @@ public static function afterStatementAnalysis(AfterClassLikeAnalysisEvent $event
continue;
}

$specials = self::getSpecials($stmt_method);
$specials = self::getSpecials($method_storage, $stmt_method);

$is_test = 0 === strpos($method_name_lc, 'test') || isset($specials['test']);
if (!$is_test) {
Expand Down Expand Up @@ -519,62 +524,68 @@ private static function hasInitializers(ClassLikeStorage $storage, ClassLike $st
return true;
}

foreach ($storage->methods as $method => $_) {
$stmt_method = $stmt->getMethod($method);
foreach ($storage->methods as $method_name_lc => $method_storage) {
$stmt_method = $stmt->getMethod($method_name_lc);
if (!$stmt_method) {
continue;
}
if (self::isBeforeInitializer($stmt_method)) {
if (self::isBeforeInitializer($method_storage, $stmt_method)) {
return true;
}
}
return false;
}

private static function isBeforeInitializer(ClassMethod $method): bool
private static function isBeforeInitializer(MethodStorage $method_storage, ClassMethod $method): bool
{
$specials = self::getSpecials($method);
$specials = self::getSpecials($method_storage, $method);
return isset($specials['before']);
}

/** @return array<string, array<int,string>> */
private static function getSpecials(ClassMethod $method): array
/**
* @return array<string, list<string>>
*/
private static function getSpecials(MethodStorage $method_storage, ClassMethod $method): array
{
$specials = [];

foreach ($method_storage->attributes as $attribute_storage) {
if ($attribute_storage->fq_class_name === 'PHPUnit\Framework\Attributes\DataProvider') {
$first_arg_type = $attribute_storage->args[0]->type;
if (!$first_arg_type instanceof Union) {
continue;
}
$specials['dataProvider'] ??= [];
$specials['dataProvider'][] = $first_arg_type->getSingleStringLiteral()->value;
} elseif ($attribute_storage->fq_class_name === 'PHPUnit\Framework\Attributes\Before') {
$specials['before'] ??= [];
$specials['before'][] = '';
} elseif ($attribute_storage->fq_class_name === 'PHPUnit\Framework\Attributes\Test') {
$specials['test'] ??= [];
$specials['test'][] = '';
}
}

$docblock = $method->getDocComment();
if (!$docblock) {
return [];
if ($docblock === null) {
return $specials;
}

try {
$parsed_comment = self::getParsedComment($docblock);
$parsed_docblock = DocComment::parsePreservingLength($docblock);
$tags = $parsed_docblock->tags;
} catch (DocblockParseException $e) {
return [];
$tags = [];
}

if (!isset($parsed_comment['specials'])) {
return [];
foreach ($tags as $name => $lines) {
foreach ($lines as $line) {
$specials[$name] ??= [];
$specials[$name][] = \trim($line);
}
}

return array_map(
function (array $lines) {
return array_map('trim', $lines);
},
$parsed_comment['specials']
);
}

/** @return array{description:string, specials:array<string,array<int,string>>} */
private static function getParsedComment(Doc $comment): array
{
$parsed_docblock = DocComment::parsePreservingLength($comment);

$description = $parsed_docblock->description;
$specials = $parsed_docblock->tags;

return [
'description' => $description,
'specials' => $specials,
];
return $specials;
}

private static function queueClassLikeForScanning(
Expand Down
15 changes: 15 additions & 0 deletions tests/acceptance/TestCase.feature
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,21 @@ Feature: TestCase
public function testSomething($int) {
$this->assertEquals(1, $int);
}

/**
* @psalm-suppress UnusedMethod
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provide')]
public function test2(mixed $int): void {
$this->assertEquals(1, $int);
}
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| UndefinedMethod | Provider method NS\MyTestCase::provide is not defined |
| UndefinedMethod | Provider method NS\MyTestCase::provide is not defined |
And I see no other errors

Scenario: Invalid iterable data provider is reported
Expand Down Expand Up @@ -535,13 +544,19 @@ Feature: TestCase
public function testSomething(int $int) {
$this->assertEquals(1, $int);
}

/**
* @return void
* @test
*/
public function somethingElse(int $int) {
$this->assertEquals(1, $int);
}

#[\PHPUnit\Framework\Attributes\Test]
public function anotherOne(int $int): void {
$this->assertEquals(1, $int);
}
}
"""
When I run Psalm with dead code detection
Expand Down