Repository navigation
Support #[DataProviderClosure] in the type checking rule - #370
Conversation
… 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>
|
//cc @SanderMuller |
#[DataProviderClosure] in the type checking rule
SanderMuller
left a comment
There was a problem hiding this comment.
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.
-
validateArgumentCountas 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
falsehere and accepts the data set. The helper only knows the literalfalse. If it yields the expression and the rule resolves it with the scope, the error goes away:$validateArgumentCount === null || !$scope->getType($validateArgumentCount)->isFalse()->yes()
-
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
returnto 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:
testDataProviderClosurefails with thesrcchange reverted and passes with it.- The flag handling matches PHPUnit 13.4: it warns about extra values only when
validateArgumentCountis 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
TestMethodsHelperusesgetImmediateMethods(). - 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.
Cover a validateArgumentCount value which is maybe false (bool) and a falsy non-false value (null): both keep the argument count validation enabled.
|
thank you! |
closes #291