|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\PHPUnit; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Error; |
| 7 | +use PHPStan\Analyser\IgnoreErrorExtension; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Rules\PHPUnit\DataProviderHelper; |
| 10 | +use PHPStan\Rules\PHPUnit\TestMethodsHelper; |
| 11 | + |
| 12 | +final class DataProviderReturnTypeIgnoreExtension implements IgnoreErrorExtension |
| 13 | +{ |
| 14 | + |
| 15 | + private TestMethodsHelper $testMethodsHelper; |
| 16 | + |
| 17 | + private DataProviderHelper $dataProviderHelper; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + TestMethodsHelper $testMethodsHelper, |
| 21 | + DataProviderHelper $dataProviderHelper |
| 22 | + ) |
| 23 | + { |
| 24 | + $this->testMethodsHelper = $testMethodsHelper; |
| 25 | + $this->dataProviderHelper = $dataProviderHelper; |
| 26 | + } |
| 27 | + |
| 28 | + public function shouldIgnore(Error $error, Node $node, Scope $scope): bool |
| 29 | + { |
| 30 | + if ($error->getIdentifier() !== 'missingType.iterableValue') { |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + if (!$scope->isInClass()) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + $classReflection = $scope->getClassReflection(); |
| 38 | + |
| 39 | + $methodReflection = $scope->getFunction(); |
| 40 | + if ($methodReflection === null) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + $testMethods = $this->testMethodsHelper->getTestMethods($classReflection, $scope); |
| 45 | + foreach ($testMethods as $testMethod) { |
| 46 | + foreach ($this->dataProviderHelper->getDataProviderMethods($scope, $testMethod, $classReflection) as [, $providerMethodName]) { |
| 47 | + if ($providerMethodName === $methodReflection->getName()) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments