Repository navigation
Conversation
|
//cc @SanderMuller |
SanderMuller
left a comment
There was a problem hiding this comment.
I checked this against 2.1.x (8c16263) with PHPStan 2.3.7. I found one regression, for a data provider that comes from a trait.
When the trait is in the same file, 2.1.x adds static to the trait method. With this PR the error is still reported, but the fix changes nothing. getDeclaringClass() returns the class that uses the trait, and getMethod() on that class node does not see trait methods. Fixture:
<?php declare(strict_types=1);
namespace ProbeTraitSame;
trait ProvidesData
{
public function provide(): iterable
{
yield [];
}
}
class FooTest extends \PHPUnit\Framework\TestCase
{
use ProvidesData;
/**
* @dataProvider provide
*/
public function testFoo(): void
{
}
}A trait in another file gets no fix on 2.1.x or on this PR. The native method knows the trait's file and name, so one change covers both cases:
// a method from a trait is declared in the trait, which can be in another file
$nativeMethod = $dataProviderMethodReflection->getDeclaringClass()->getNativeReflection()->getMethod($dataProviderMethodReflection->getName())->getBetterReflection();
$declaringClassFileName = $nativeMethod->getFileName();
if ($declaringClassFileName !== null) {
$stmts = $this->parser->parseFile($declaringClassFileName);
$methodNode = $this->findMethodNode(
$stmts,
$nativeMethod->getDeclaringClass()->getName(),
$dataProviderMethodReflection->getName(),
);With that change, both trait cases get the fix and the tests in this PR still pass. processDataProvider() gets the method with getNativeMethod(), so the native lookup does not throw.
What I checked that is fine:
- The three new tests fail with the
srcchange reverted and pass with it. - The PR also fixes two cases that were wrong on 2.1.x. One is
#[DataProviderExternal]that points to a class in the same file that has a same-named method. The other is two classes with the same short name in braced namespaces. On 2.1.x both addedstaticto the first method in the file. - The
$fileargument offixNode()is in PHPStan 2.3.6, which is the minimum incomposer.json. - The test suite (138 tests), PHPStan, phpcs and lint pass locally. CI is green.
Performance: 300 test classes with a non-static provider in one file, 300 fixable errors on both sides. Median of 3 runs: 4,307 ms on 2.1.x and 4,365 ms with this PR. The 2.1.x runs alone spread from 3,965 to 5,699 ms, so I see no difference.
commit 1:
The auto-fix for
@dataProvider %s related method must be static in PHPUnit 10 and newer.used to pick the first method with a matching name anywhere in the file.If two classes in the same file each had a method with that name, it could add
staticto the wrong class's method and leave the real data provider unchanged.commit 2:
When a data provider is declared in a parent class in another file, the "must be static" auto-fix applied to the test file instead. PHPStan finds the node to fix by its position, so it either changed nothing or added
staticto an unrelated method in the test file that happened to sit at the same position.