|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\PHPUnit; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Node\MethodReturnStatementsNode; |
| 8 | +use PHPStan\Rules\Rule; |
| 9 | +use PHPStan\Rules\RuleErrorBuilder; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use function sprintf; |
| 12 | +use function strcasecmp; |
| 13 | + |
| 14 | +/** |
| 15 | + * @implements Rule<MethodReturnStatementsNode> |
| 16 | + */ |
| 17 | +class EmptyDataProviderRule implements Rule |
| 18 | +{ |
| 19 | + |
| 20 | + private TestMethodsHelper $testMethodsHelper; |
| 21 | + |
| 22 | + private DataProviderHelper $dataProviderHelper; |
| 23 | + |
| 24 | + private PHPUnitVersion $PHPUnitVersion; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + TestMethodsHelper $testMethodsHelper, |
| 28 | + DataProviderHelper $dataProviderHelper, |
| 29 | + PHPUnitVersion $PHPUnitVersion |
| 30 | + ) |
| 31 | + { |
| 32 | + $this->testMethodsHelper = $testMethodsHelper; |
| 33 | + $this->dataProviderHelper = $dataProviderHelper; |
| 34 | + $this->PHPUnitVersion = $PHPUnitVersion; |
| 35 | + } |
| 36 | + |
| 37 | + public function getNodeType(): string |
| 38 | + { |
| 39 | + return MethodReturnStatementsNode::class; |
| 40 | + } |
| 41 | + |
| 42 | + public function processNode(Node $node, Scope $scope): array |
| 43 | + { |
| 44 | + if ($this->PHPUnitVersion->rejectsEmptyDataProviders()->no()) { |
| 45 | + return []; |
| 46 | + } |
| 47 | + |
| 48 | + if (!$node->getClassReflection()->is(TestCase::class)) { |
| 49 | + return []; |
| 50 | + } |
| 51 | + |
| 52 | + // A generator yields its data sets one at a time, so whether it provides |
| 53 | + // any depends on control flow this rule cannot decide. |
| 54 | + if ($node->isGenerator()) { |
| 55 | + return []; |
| 56 | + } |
| 57 | + |
| 58 | + $returnStatements = $node->getReturnStatements(); |
| 59 | + if ($returnStatements === []) { |
| 60 | + return []; |
| 61 | + } |
| 62 | + |
| 63 | + foreach ($returnStatements as $returnStatement) { |
| 64 | + $returnExpr = $returnStatement->getReturnNode()->expr; |
| 65 | + if ($returnExpr === null) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + $returnType = $returnStatement->getScope()->getType($returnExpr); |
| 70 | + if (!$returnType->isIterable()->yes() || !$returnType->isIterableAtLeastOnce()->no()) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (!$this->isDataProvider($node, $scope)) { |
| 76 | + return []; |
| 77 | + } |
| 78 | + |
| 79 | + return [ |
| 80 | + RuleErrorBuilder::message(sprintf( |
| 81 | + 'Data provider method %s() provides no data sets, which is an error in PHPUnit 10 and newer.', |
| 82 | + $node->getMethodName(), |
| 83 | + )) |
| 84 | + ->identifier('phpunit.dataProviderEmpty') |
| 85 | + ->build(), |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + private function isDataProvider(MethodReturnStatementsNode $node, Scope $scope): bool |
| 90 | + { |
| 91 | + $classReflection = $node->getClassReflection(); |
| 92 | + |
| 93 | + foreach ($this->testMethodsHelper->getTestMethods($classReflection, $scope) as $testMethod) { |
| 94 | + foreach ($this->dataProviderHelper->getDataProviderMethods($scope, $testMethod, $classReflection) as [$providerClassReflection, $providerMethodName]) { |
| 95 | + // A provider declared in another class is checked when that class is analysed. |
| 96 | + if ($providerClassReflection === null || $providerClassReflection->getName() !== $classReflection->getName()) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + if (strcasecmp($providerMethodName, $node->getMethodName()) === 0) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments