GeezerVison is showing github.com — Report a data provider that provides no data sets by SanderMuller · Pull Request #328 · phpstan/phpstan-phpunit
Conversation
EmptyDataProviderRule reports a data provider method whose every return statement returns an iterable that is provably empty. PHPUnit 10 and newer errors on this. PHPUnit 9 skipped the test.
Registered behind bleedingEdge, like DataProviderDataRule. It reports unless the detected PHPUnit major is 9 or lower, so a project where the version cannot be detected still gets the check.
Scope limits, each deliberate:
- Generators are skipped. Whether a generator yields at all depends on control flow. Where it is decidable, core already reports
deadCode.unreachable on the dead yield.
- The
phpstan-doctrine example in the issue is a generator guarded by PHP_VERSION_ID, which PHPStan models as int<70400, 80599>. No static check can decide it, and this rule does not report it.
- A provider declared in another class is checked when that class is analysed, not from the test method.
- With an undetectable PHPUnit version,
DataProviderHelper does not read #[DataProvider], so only annotation-declared providers are covered. That is existing helper behaviour, and the test encodes it.
Infection on the changed lines generates 3 mutants. All 3 are killed by the test framework, at 100% covered MSI.
Closes #251
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
PHPUnit 10 and newer rejects a data provider that provides nothing.
PHPUnit 9 skipped the test instead.
EmptyDataProviderRule reports a provider method whose every return
statement returns an iterable that is provably empty. It skips
generators, because whether a generator yields at all depends on control
flow the rule cannot decide.
Closes phpstan#251
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
|
/**
|
|
|
* @implements Rule<MethodReturnStatementsNode>
|
|
|
*/
|
|
|
class EmptyDataProviderRule implements Rule
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember DataProviderDataRule can be slow on big projects. it might make sense to explore in a separate PR, whether implementing the empty-data case inside of DataProviderDataRule (in case this makes things faster)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
staabm
staabm left review comments
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.