Skip to content
Open
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
c4597bf
Implement DataProviderDataRule
staabm Oct 13, 2025
ccff992
report correct line
staabm Oct 13, 2025
0975030
Added DataProviderDataRuleTest
staabm Oct 13, 2025
5c6445a
implement CompositeRule
staabm Oct 13, 2025
ff6843e
cleanup
staabm Oct 13, 2025
1dd180c
Implement reflection based dataprovider detection
staabm Oct 13, 2025
cff1f79
cs
staabm Oct 13, 2025
0ab211d
simplify
staabm Oct 13, 2025
7f7b38b
fix
staabm Oct 13, 2025
d74aeec
Refactor
staabm Oct 14, 2025
09b226b
test annotations
staabm Oct 14, 2025
f9c8af7
support yield
staabm Oct 14, 2025
d3a2b9b
support yield from
staabm Oct 14, 2025
f42bd8a
fix
staabm Oct 14, 2025
3318b73
Support `test` annotation and attribute
staabm Oct 14, 2025
f715fbf
extract PHPUnitVersionDetector
staabm Oct 14, 2025
67a40d6
fix php 7.4
staabm Oct 14, 2025
6f4dcc9
cs
staabm Oct 14, 2025
20d1408
bleeding edge only
staabm Oct 14, 2025
5cb8c63
test too many/few arguments
staabm Oct 14, 2025
72d5655
report too many/too few with when provider re-used
staabm Oct 14, 2025
1453c20
cleanup
staabm Oct 14, 2025
1506a06
Update extension.neon
staabm Oct 14, 2025
9b69121
use types
staabm Oct 14, 2025
459eb1d
support named args
staabm Oct 14, 2025
5fcf01d
fix invalid call on null
staabm Oct 14, 2025
a978aa8
fix invalid call on null in yield
staabm Oct 14, 2025
a1dad84
fix type error
staabm Oct 14, 2025
1d3db54
new TypeExpr(new ObjectType())
staabm Oct 14, 2025
7417fbc
test constant array return from delegated method
staabm Oct 14, 2025
74b67cf
use CompositeRule from phpstan-src
staabm Oct 14, 2025
5cb7312
fix PHP 7.4
staabm Oct 14, 2025
9ee26b1
Update data-provider-data.php
staabm Oct 14, 2025
4cb9404
test more variadics
staabm Oct 14, 2025
34a0a0e
more variadic tests
staabm Oct 14, 2025
bb855f0
fix
staabm Oct 14, 2025
a56169b
Update DataProviderDataRuleTest.php
staabm Oct 14, 2025
e31a234
Update DataProviderDataRule.php
staabm Oct 14, 2025
1532c26
support iterables
staabm Oct 14, 2025
68e65e9
test ArrayIterator
staabm Oct 14, 2025
30e27db
cleanup
staabm Oct 15, 2025
aafaf8f
simplify
staabm Oct 15, 2025
b6f11fe
Report wrongly typed generic arrays
staabm Oct 15, 2025
72dc9e0
simplify
staabm Oct 15, 2025
ccb7b04
Fix SA error
ondrejmirtes Oct 16, 2025
8491dd7
Fix CS
ondrejmirtes Oct 16, 2025
aea7e42
abstract test-class
staabm Oct 17, 2025
a1e5a19
factor out buildArrayTypesFromNode()
staabm Oct 17, 2025
7f9dbf2
test more const array variants
staabm Oct 17, 2025
c689efe
special case for providers only containing static data, so we get mor…
staabm Oct 17, 2025
d6c11be
apply static data only special case also for "yield from"
staabm Oct 17, 2025
21740cb
moved cheapest check to the front
staabm Oct 17, 2025
41acc04
Failing test - args trimming logic needs some work
ondrejmirtes Oct 17, 2025
807a35f
Not a failing test, just checking :)
ondrejmirtes Oct 17, 2025
3fc4e0e
separated php8 only tests
staabm Oct 17, 2025
9a0d039
separate named args expectations
staabm Oct 17, 2025
daf978e
separated more named args tests
staabm Oct 17, 2025
534694d
fix arg trimming
staabm Oct 18, 2025
58461d7
more fixes
staabm Oct 18, 2025
b36a404
cs
staabm Oct 18, 2025
f083e63
Manual mutation testing ep. 1
ondrejmirtes Oct 18, 2025
42ad04e
Manual mutation testing ep. 2
ondrejmirtes Oct 18, 2025
0731026
break
ondrejmirtes Oct 18, 2025
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.4 || ^8.0",
"phpstan/phpstan": "^2.1.18"
"phpstan/phpstan": "^2.1.32"
},
"conflict": {
"phpunit/phpunit": "<7.0"
Expand Down
7 changes: 7 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ services:
class: PHPStan\Rules\PHPUnit\CoversHelper
-
class: PHPStan\Rules\PHPUnit\AnnotationHelper

-
class: PHPStan\Rules\PHPUnit\PHPUnitVersionDetector

-
class: PHPStan\Rules\PHPUnit\TestMethodsHelper
factory: @PHPStan\Rules\PHPUnit\TestMethodsHelperFactory::create()
-
class: PHPStan\Rules\PHPUnit\TestMethodsHelperFactory

-
class: PHPStan\Rules\PHPUnit\DataProviderHelper
factory: @PHPStan\Rules\PHPUnit\DataProviderHelperFactory::create()
Expand Down
6 changes: 6 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ conditionalTags:
PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule:
phpstan.rules.rule: [%strictRulesInstalled%, %featureToggles.bleedingEdge%]

PHPStan\Rules\PHPUnit\DataProviderDataRule:
phpstan.rules.rule: %featureToggles.bleedingEdge%

services:
-
class: PHPStan\Rules\PHPUnit\DataProviderDeclarationRule
Expand All @@ -24,3 +27,6 @@ services:

-
class: PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule

-
class: PHPStan\Rules\PHPUnit\DataProviderDataRule
237 changes: 237 additions & 0 deletions src/Rules/PHPUnit/DataProviderDataRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PHPUnit;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Rules\Rule;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPUnit\Framework\TestCase;
use function array_slice;
use function count;
use function max;
use function min;

/**
* @implements Rule<Node>
*/
class DataProviderDataRule implements Rule
{

private TestMethodsHelper $testMethodsHelper;

private DataProviderHelper $dataProviderHelper;

public function __construct(
TestMethodsHelper $testMethodsHelper,
DataProviderHelper $dataProviderHelper
)
{
$this->testMethodsHelper = $testMethodsHelper;
$this->dataProviderHelper = $dataProviderHelper;
}

public function getNodeType(): string
{
return Node::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (
!$node instanceof Node\Stmt\Return_
&& !$node instanceof Node\Expr\Yield_
&& !$node instanceof Node\Expr\YieldFrom
) {
return [];
}

if ($scope->getFunction() === null) {
return [];
}
if ($scope->isInAnonymousFunction()) {
return [];
}

$arraysTypes = $this->buildArrayTypesFromNode($node, $scope);
if ($arraysTypes === []) {
return [];
}

$method = $scope->getFunction();
$classReflection = $scope->getClassReflection();
if (
$classReflection === null
|| !$classReflection->is(TestCase::class)
) {
return [];
}
Comment on lines +63 to +70
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this condition be done before buildArrayTypesFromNode to be faster ?

We might also prefer to check

if (count($testsWithProvider) === 0) {
     return [];
}

before the buildArrayTypesFromNode call, it will avoid to inspect methods which are not dataprovider and improves performances.

Copy link
Contributor Author

@staabm staabm Oct 17, 2025

Choose a reason for hiding this comment

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

I am not sure whether AST traversing + type retrieval is faster or slower then runtime reflection.
do you have a test at hand which to prove your thesis about what is faster?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure whether AST traversing + type retrieval is faster or slower then runtime reflection.
do you have a cast at hand which to prove your thesis about what is faster?

Not at all, it might be a wrong intuition.
I thought type manipulation was something slow, with possible union/intersection etc.


$testsWithProvider = [];
$testMethods = $this->testMethodsHelper->getTestMethods($classReflection, $scope);
foreach ($testMethods as $testMethod) {
foreach ($this->dataProviderHelper->getDataProviderMethods($scope, $testMethod, $classReflection) as [, $providerMethodName]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think it would be worth having a cache on getDataProviderMethods (and maybe getTestMethods ?) ?

Cause we will compute this for every possible return of a TestCase class, and the result will always be the same

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am fine/willing to add a cache in case we find a case where it turns into a measurable improvement

if ($providerMethodName === $method->getName()) {
$testsWithProvider[] = $testMethod;
continue 2;
}
}
}

if (count($testsWithProvider) === 0) {
return [];
}

$maxNumberOfParameters = 0;
$trimArgs = count($testsWithProvider) > 1;
Copy link
Member

Choose a reason for hiding this comment

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

I think in correct implementation, we don't need this condition at all. A foreach over methods that just iterates once should be enough to set everything correctly.

foreach ($testsWithProvider as $testMethod) {
$maxNumberOfParameters = max($maxNumberOfParameters, $testMethod->getNumberOfParameters());
}

foreach ($testsWithProvider as $testMethod) {
$numberOfParameters = $testMethod->getNumberOfParameters();

foreach ($arraysTypes as [$startLine, $arraysType]) {
$args = $this->arrayItemsToArgs($arraysType, $numberOfParameters);
if ($args === null) {
continue;
}

if ($trimArgs && $maxNumberOfParameters !== $numberOfParameters) {
$args = array_slice($args, 0, min($numberOfParameters, $maxNumberOfParameters));
}

$scope->invokeNodeCallback(new Node\Expr\MethodCall(
new TypeExpr(new ObjectType($classReflection->getName())),
$testMethod->getName(),
$args,
['startLine' => $startLine],
));
}
}

return [];
}

/**
* @return array<Node\Arg>
*/
private function arrayItemsToArgs(Type $array, int $numberOfParameters): ?array
{
$args = [];

$constArrays = $array->getConstantArrays();
if ($constArrays !== [] && count($constArrays) === 1) {
$keyTypes = $constArrays[0]->getKeyTypes();
$valueTypes = $constArrays[0]->getValueTypes();
} elseif ($array->isArray()->yes()) {
$keyTypes = [];
$valueTypes = [];
for ($i = 0; $i < $numberOfParameters; ++$i) {
$keyTypes[$i] = $array->getIterableKeyType();
$valueTypes[$i] = $array->getIterableValueType();
}
} else {
return null;
}

foreach ($valueTypes as $i => $valueType) {
$key = $keyTypes[$i]->getConstantStrings();
if (count($key) > 1) {
return null;
}

if (count($key) === 0) {
$arg = new Node\Arg(new TypeExpr($valueType));
$args[] = $arg;
continue;

}

$arg = new Node\Arg(
new TypeExpr($valueType),
false,
false,
[],
new Node\Identifier($key[0]->getValue()),
);
$args[] = $arg;
}

return $args;
}

/**
* @param Node\Stmt\Return_|Node\Expr\Yield_|Node\Expr\YieldFrom $node
*
* @return list<list{int, Type}>
*/
private function buildArrayTypesFromNode(Node $node, Scope $scope): array
{
$arraysTypes = [];

// special case for providers only containing static data, so we get more precise error lines
if (
($node instanceof Node\Stmt\Return_ && $node->expr instanceof Node\Expr\Array_)
|| ($node instanceof Node\Expr\YieldFrom && $node->expr instanceof Node\Expr\Array_)
) {
foreach ($node->expr->items as $item) {
if (!$item->value instanceof Node\Expr\Array_) {
$arraysTypes = [];
break;
}

$constArrays = $scope->getType($item->value)->getConstantArrays();
if ($constArrays === []) {
$arraysTypes = [];
break;
}

foreach ($constArrays as $constArray) {
$arraysTypes[] = [$item->value->getStartLine(), $constArray];
}
}

if ($arraysTypes !== []) {
return $arraysTypes;
}
}

// general case with less precise error message lines
if ($node instanceof Node\Stmt\Return_ || $node instanceof Node\Expr\YieldFrom) {
if ($node->expr === null) {
return [];
}

$exprType = $scope->getType($node->expr);
$exprConstArrays = $exprType->getConstantArrays();
foreach ($exprConstArrays as $constArray) {
foreach ($constArray->getValueTypes() as $valueType) {
foreach ($valueType->getConstantArrays() as $constValueArray) {
$arraysTypes[] = [$node->getStartLine(), $constValueArray];
}
}
}

if ($arraysTypes === []) {
foreach ($exprType->getIterableValueType()->getArrays() as $arrayType) {
$arraysTypes[] = [$node->getStartLine(), $arrayType];
}
}
} elseif ($node instanceof Node\Expr\Yield_) {
if ($node->value === null) {
return [];
}

$exprType = $scope->getType($node->value);
foreach ($exprType->getConstantArrays() as $constValueArray) {
$arraysTypes[] = [$node->getStartLine(), $constValueArray];
}
}

return $arraysTypes;
}

}
94 changes: 94 additions & 0 deletions src/Rules/PHPUnit/TestMethodsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PHPUnit;

use PHPStan\Analyser\Scope;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\FileTypeMapper;
use ReflectionMethod;
use function str_starts_with;
use function strtolower;

final class TestMethodsHelper
{

private FileTypeMapper $fileTypeMapper;

private bool $phpunit10OrNewer;

public function __construct(
FileTypeMapper $fileTypeMapper,
bool $phpunit10OrNewer
)
{
$this->fileTypeMapper = $fileTypeMapper;
$this->phpunit10OrNewer = $phpunit10OrNewer;
}

/**
* @return array<ReflectionMethod>
Copy link
Member

Choose a reason for hiding this comment

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

This is a weird type. I'd prefer our own ExtendedMethodReflection to be returned. You can get ClassReflection::getNativeMethod() once you make sure this is a test method from the current $reflectionMethod (to save time from instantiating all methods which is expensive).

Copy link
Member

Choose a reason for hiding this comment

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

I noticed this because I was looking for how you're handling variadic methods and the $testMethod->getNumberOfParameters() method call seemed unfamiliar to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

from ExtendedMethodReflection I cannot getStartLine, which I need in DataProviderHelper

Copy link
Member

Choose a reason for hiding this comment

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

You mean there isn't getStartLine on AttributeReflection? I don't see getStartLine being called on ReflectionMethod.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

see

$startLine = $node->getStartLine();

*/
public function getTestMethods(ClassReflection $classReflection, Scope $scope): array
{
$testMethods = [];
foreach ($classReflection->getNativeReflection()->getMethods() as $reflectionMethod) {
if (!$reflectionMethod->isPublic()) {
continue;
}

if (str_starts_with(strtolower($reflectionMethod->getName()), 'test')) {
$testMethods[] = $reflectionMethod;
continue;
}

$docComment = $reflectionMethod->getDocComment();
if ($docComment !== false) {
$methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$scope->getFile(),
$classReflection->getName(),
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
$reflectionMethod->getName(),
$docComment,
);

if ($this->hasTestAnnotation($methodPhpDoc)) {
$testMethods[] = $reflectionMethod;
continue;
}
}

if (!$this->phpunit10OrNewer) {
continue;
}

$testAttributes = $reflectionMethod->getAttributes('PHPUnit\Framework\Attributes\Test'); // @phpstan-ignore argument.type
if ($testAttributes === []) {
continue;
}

$testMethods[] = $reflectionMethod;
}

return $testMethods;
}

private function hasTestAnnotation(?ResolvedPhpDocBlock $phpDoc): bool
{
if ($phpDoc === null) {
return false;
}

$phpDocNodes = $phpDoc->getPhpDocNodes();

foreach ($phpDocNodes as $docNode) {
$tags = $docNode->getTagsByName('@test');
if ($tags !== []) {
return true;
}
}

return false;
}

}
Loading
Loading