Skip to content

Check CoversClass, CoversTrait, CoversMethod and CoversFunction attributes - #358

Open
theluckystrike wants to merge 1 commit into
phpstan:2.1.xfrom
theluckystrike:covers-attributes
Open

theluckystrike wants to merge 1 commit into
phpstan:2.1.xfrom
theluckystrike:covers-attributes

Conversation

@theluckystrike

Copy link
Copy Markdown

Closes #289.

ClassCoversExistsRule now also checks the PHPUnit coverage attributes on test classes:

  • #[CoversClass(X::class)]: X must exist and must not be an interface (phpunit.coversClass, phpunit.coversInterface)
  • #[CoversTrait(T::class)]: T must exist and be a trait (phpunit.coversTrait)
  • #[CoversMethod(X::class, 'm')]: X must exist and have method m (phpunit.coversMethod)
  • #[CoversFunction('f')]: f must exist (phpunit.coversFunction)

The attributes are read with getAttributesByName() from the BetterReflection class, like ClassAttributeRequiresPhpVersionRule does, so arguments come in already evaluated. Named arguments (className: etc.) work too. Errors are reported on the class line, the same place as the @covers errors.

I did not change ClassMethodCoversExistsRule. In PHPUnit these attributes only target classes (Attribute::TARGET_CLASS), so there is nothing to read on methods. If you want the method rule to report a Covers attribute placed on a method, I can add that.

Two points for review: phpunit.coversTrait and phpunit.coversFunction are new identifiers, and I kept the messages close to the @covers ones. Happy to rename them.

The new testAttributes case in ClassCoversExistsRuleTest uses data/class-coverage-attributes.php with valid and invalid uses and a non-TestCase class. It fails on 2.1.x and passes with the change. phpunit, phpstan, parallel-lint and phpcs with build-cs pass locally.

@staabm

staabm commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

//cc @SanderMuller

@staabm staabm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • please make the new errors only show up when bleeding-edge is enabled
  • each error should point to the line of the attribute
  • #[CoversTrait] is never tested with a class that doesn't exist, so removing the hasClass() part of the condition goes unnoticed
  • #[CoversClass] with a trait isn't reported. PHPUnit 11.x deprecates this in favour of #[CoversTrait], and 12 rejects it.

@SanderMuller

Copy link
Copy Markdown
Contributor

I checked the four points against 8d42830 and ran the targets through PHPUnit 11.5.58 and 12.5.45, with coverage on.

  1. Bleeding edge: ClassCoversExistsRule is not gated itself, so the new checks need a flag. AttributeVersionRequirementHelper gets one as its bleedingEdge argument in extension.neon.
  2. The #[CoversTrait] gap is real. With hasClass() removed from that condition, ClassCoversExistsRuleTest still passes. A missing trait would then reach getClass(), which throws ClassNotFoundException for a class that does not exist.
  3. All six errors in the test are on line 37, the first attribute, because the class node starts at its attributes.
  4. #[CoversClass] with a trait: PHPUnit 12.5.45 warns "Class App\T is not a valid target for code coverage". PHPUnit 11.5.58 gives nothing, not even with --display-phpunit-deprecations. So the check depends on the PHPUnit version. PHPUnitVersion could gate it, like supportsDataProviderAttribute() does.

PHPUnit 12 warns on more targets that the PR accepts:

  • #[CoversMethod(C::class, 'inParent')], a method C inherits from its parent
  • #[CoversMethod(C::class, 'inTrait')], a method C gets from a trait
  • a leading backslash: #[CoversClass('\App\C')], #[CoversTrait('\App\T')], #[CoversFunction('\App\f')]

hasMethod() and hasClass() accept all of them. PHPUnit 11 accepts them too. It only warned on a magic method, and the PR reports that one already. An enum in #[CoversClass], #[CoversMethod(T::class, 'inTrait')] and a lowercase function name are fine in both versions.

#[CoversClass(UNDEFINED_CONST)] makes getArguments() throw. The run then reports phpstan.reflection and "Result is incomplete because of severe errors", and the file's other errors are lost. #[RequiresPhp(UNDEFINED_CONST)] already does the same with bleeding edge, so the PR copies an existing pattern. Without bleeding edge, ClassCoversExistsRule would bring it to stable users.

Reading the arguments from the attribute nodes of InClassNode::getOriginalNode() with $scope->getType() would avoid that, and it would also give each error its attribute line. I have not tried that.

Performance: 2,000 generated test classes with four Covers attributes each, three interleaved rounds. User CPU was 4.70 s on 2.1.x and 4.72 s on the PR (medians), with runs between 4.54 and 4.84 s. That is no measurable difference.

The PR's gate passes locally: 99 tests, phpstan, phpcs. testAttributes fails without the change. I did not run parallel-lint, and I did not check the PHPUnit 10 behaviour.

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.

ClassCoversExistsRule and ClassMethodCoversExistsRule do not support attributes

3 participants