-
Notifications
You must be signed in to change notification settings - Fork 50
Ignore missingType.iterableValue for data-providers #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c52c41b
Ignore missingType.iterableValue for data-providers
staabm b419a74
Delete data-provider-iterable-value.php
staabm a37f114
conditional tag
staabm 3ce0a23
added test
staabm be6fe1e
fix
staabm c4a826d
Update DataProviderReturnTypeIgnoreExtensionTest.php
staabm 6e321dd
move is-test-case check into TestMethodsHelper
staabm a9225a6
fix
staabm 8b83767
Update DataProviderReturnTypeIgnoreExtension.php
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/Type/PHPUnit/DataProviderReturnTypeIgnoreExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\PHPUnit; | ||
|
|
||
| use PhpParser\Node; | ||
| use PHPStan\Analyser\Error; | ||
| use PHPStan\Analyser\IgnoreErrorExtension; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Node\InClassMethodNode; | ||
| use PHPStan\Rules\PHPUnit\DataProviderHelper; | ||
| use PHPStan\Rules\PHPUnit\TestMethodsHelper; | ||
|
|
||
| final class DataProviderReturnTypeIgnoreExtension implements IgnoreErrorExtension | ||
| { | ||
|
|
||
| private TestMethodsHelper $testMethodsHelper; | ||
|
|
||
| private DataProviderHelper $dataProviderHelper; | ||
|
|
||
| public function __construct( | ||
| TestMethodsHelper $testMethodsHelper, | ||
| DataProviderHelper $dataProviderHelper | ||
| ) | ||
| { | ||
| $this->testMethodsHelper = $testMethodsHelper; | ||
| $this->dataProviderHelper = $dataProviderHelper; | ||
| } | ||
|
|
||
| public function shouldIgnore(Error $error, Node $node, Scope $scope): bool | ||
| { | ||
| if (! $node instanceof InClassMethodNode) { // @phpstan-ignore phpstanApi.instanceofAssumption | ||
| return false; | ||
| } | ||
|
|
||
| if ($error->getIdentifier() !== 'missingType.iterableValue') { | ||
| return false; | ||
| } | ||
|
|
||
| $classReflection = $node->getClassReflection(); | ||
| $methodReflection = $node->getMethodReflection(); | ||
| $testMethods = $this->testMethodsHelper->getTestMethods($classReflection, $scope); | ||
| foreach ($testMethods as $testMethod) { | ||
| foreach ($this->dataProviderHelper->getDataProviderMethods($scope, $testMethod, $classReflection) as [, $providerMethodName]) { | ||
| if ($providerMethodName === $methodReflection->getName()) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| } | ||
38 changes: 38 additions & 0 deletions
38
tests/Type/PHPUnit/DataProviderReturnTypeIgnoreExtensionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\PHPUnit; | ||
|
|
||
| use PHPStan\Rules\Methods\MissingMethodReturnTypehintRule; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<MissingMethodReturnTypehintRule> | ||
| */ | ||
| class DataProviderReturnTypeIgnoreExtensionTest extends RuleTestCase { | ||
| protected function getRule(): Rule | ||
| { | ||
| /** @phpstan-ignore phpstanApi.classConstant */ | ||
| $rule = self::getContainer()->getByType(MissingMethodReturnTypehintRule::class); | ||
|
|
||
| return $rule; | ||
| } | ||
|
|
||
| public function testRule(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/data-provider-iterable-value.php'], [ | ||
| [ | ||
| 'Method DataProviderIterableValueTest\Foo::notADataProvider() return type has no value type specified in iterable type iterable.', | ||
| 32, | ||
| 'See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type' | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| static public function getAdditionalConfigFiles(): array | ||
| { | ||
| return [ | ||
| __DIR__ . '/data/data-provider-iterable-value.neon' | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| parameters: | ||
| phpunit: | ||
| checkDataProviderData: true | ||
|
|
||
| includes: | ||
| - ../../../../extension.neon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| namespace DataProviderIterableValueTest; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class Foo extends TestCase { | ||
| /** | ||
| * @dataProvider dataProvider | ||
| * @dataProvider dataProvider2 | ||
| */ | ||
| public function testFoo():void { | ||
|
|
||
| } | ||
|
|
||
| public function dataProvider(): iterable { | ||
| return [ | ||
| [1, 2], | ||
| [3, 4], | ||
| [5, 6], | ||
| ]; | ||
| } | ||
|
|
||
| public function dataProvider2(): iterable { | ||
| $i = rand(0, 10); | ||
|
|
||
| return [ | ||
| [$i, 2], | ||
| ]; | ||
| } | ||
|
|
||
| public function notADataProvider(): iterable { | ||
| return [ | ||
| [1, 2], | ||
| [3, 4], | ||
| [5, 6], | ||
| ]; | ||
| } | ||
| } |
Oops, something went wrong.
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.
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.
This assumes an implementation detail that missingType.iterableValue is reported by a rule that hooks onto InClassMethodNode.
You can remove this instanceof and just ask for
$scope->isInClass(),$scope->getFunction()etc.Uh oh!
There was an error while loading. Please reload this page.
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.
got this code from the docs :).
doc-fix in phpstan/phpstan#13726
thank you.