Skip to content

Ignore the docblock of a method that has any PHPUnit attribute - #263

Merged
alies-dev merged 2 commits into
psalm:masterfrom
alies-dev:fix/docblock-next-to-attribute
Nov 10, 2026
Merged

alies-dev merged 2 commits into
psalm:masterfrom
alies-dev:fix/docblock-next-to-attribute

Conversation

@alies-dev

@alies-dev alies-dev commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

PHPUnit ignores the whole docblock of a method that has any PHPUnit attribute, but the plugin still honours its @before, @test and @dataProvider next to an attribute that is not a provider, such as #[Group].

Before

final class MyTestCase extends TestCase
{
    private int $cents; // no issue

    /**
     * @before
     */
    #[Group('money')]
    public function initCents(): void { // PHPUnit never runs it
        $this->cents = 100;
    }

    public function testCents(): void {
        $this->assertSame(100, $this->cents);
    }
}
final class MyTestCase extends TestCase {
    /**
     * @dataProvider provideCents
     */
    #[Group('money')] // UndefinedMethod: Provider method NS\MyTestCase::provideCents is not defined
    public function testCents(int $cents = 100): void {
        $this->assertSame(100, $cents);
    }
}

After

    private int $cents; // MissingConstructor: NS\MyTestCase has an uninitialized property NS\MyTestCase::$cents, but no constructor
    #[Group('money')] // no issue

With dead code detection, a provider named only by such a docblock and a method marked only by such an @test are now reported as unused.

Like PHPUnit's ParserChain. The rule now lives in docblockSpecials(), so @before, @test, @dataProvider and @Depends all follow it.
docblockSpecials() is empty for a method with any PHPUnit attribute, so the null result that selected the docblock providers is no longer needed.
@alies-dev
alies-dev merged commit aecefef into psalm:master Nov 10, 2026
8 checks passed
@alies-dev
alies-dev deleted the fix/docblock-next-to-attribute branch November 10, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant