Skip to content

Support #[DataProviderClosure] in the type checking rule - #370

Open
staabm wants to merge 2 commits into
phpstan:2.1.xfrom
staabm:closure
Open

staabm wants to merge 2 commits into
phpstan:2.1.xfrom
staabm:closure

Conversation

@staabm

@staabm staabm commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

closes #291

staabm and others added 2 commits November 29, 2026 09:23
… class

While analysing a trait in the context of the using class, the scope file
is the class file. Lines of a trait closure were therefore compared with
closures of test methods in the class file, reporting errors against an
unrelated test method.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@staabm
staabm marked this pull request as ready for review November 29, 2026 08:42
@staabm

staabm commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

//cc @SanderMuller

@staabm staabm changed the title Support DataProviderClosure in the type checking rule Support #[DataProviderClosure] in the type checking rule Nov 29, 2026

@SanderMuller SanderMuller 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.

I checked 4b1fade against 2.1.x (f161708) with PHPStan 2.3.7 on PHP 8.6. The approach works. I found two false positives in edge cases, each with a small fix that I tried locally.

  1. validateArgumentCount as a constant. For this test, the rule reports "invoked with 3 parameters, 2 required":

    const NO_VALIDATION = false;
    
    #[DataProviderClosure(static function (): array {
    	return [[1, 2, 3]];
    }, self::NO_VALIDATION)]
    public function testConstantFlag(int $a, int $b): void

    PHPUnit reads the flag as false here and accepts the data set. The helper only knows the literal false. If it yields the expression and the rule resolves it with the scope, the error goes away:

    $validateArgumentCount === null || !$scope->getType($validateArgumentCount)->isFalse()->yes()
  2. Two closures on one line:

    #[DataProviderClosure(static function (): array { return [[1, 2]]; }), DataProviderClosure(static function (): array { return [[1, 2, 3]]; }, false)]
    public function testSameLine(int $a, int $b): void

    The rule matches the second return to the first closure, so it uses the wrong flag and reports the same error. This is unusual code. Matching on file positions instead of lines fixes it, because both ASTs keep the positions:

    $node->getStartFilePos() < $closure->getStartFilePos()
    || $node->getStartFilePos() > $closure->getEndFilePos()

With both changes, the test suite still passes.

A possible follow-up, not for this PR: PHP 8.5 also accepts a first-class callable in the attribute, for example #[DataProviderClosure(self::provide(...))]. The rule skips it because the argument is not a Closure node, so the data of provide() is not checked.

What I checked that is fine:

  • testDataProviderClosure fails with the src change reverted and passes with it.
  • The flag handling matches PHPUnit 13.4: it warns about extra values only when validateArgumentCount is true and the test method is not variadic.
  • Arrow functions are not a gap. PHP rejects them in constant expressions.
  • Test methods inherited from a parent class in another file do not take part in the line match, because TestMethodsHelper uses getImmediateMethods().
  • The test suite (146 tests), PHPStan, phpcs and lint pass locally. CI is green.

Performance: I used 300 test classes in one file, each with a method provider and a closure provider, with bleedingEdge on. The median of 3 runs was 1.26 s on 2.1.x and 1.26 s with this PR. With the closure check forced on (PHPUnit 9.6 is installed here), it was 1.21 s. The runs spread from 1.21 to 1.29 s, so I see no difference.

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.

Support DataProviderClosure in the type checking rule

3 participants