|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\PHPUnit; |
| 4 | + |
| 5 | +use PHPStan\Analyser\Scope; |
| 6 | +use PHPStan\PhpDoc\ResolvedPhpDocBlock; |
| 7 | +use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
| 8 | +use PHPStan\Reflection\MissingMethodFromReflectionException; |
| 9 | +use PHPStan\Rules\RuleError; |
| 10 | +use PHPStan\Rules\RuleErrorBuilder; |
| 11 | +use function array_merge; |
| 12 | +use function preg_match; |
| 13 | +use function sprintf; |
| 14 | + |
| 15 | +class DataProviderHelper |
| 16 | +{ |
| 17 | + |
| 18 | + /** |
| 19 | + * @return array<PhpDocTagNode> |
| 20 | + */ |
| 21 | + public function getDataProviderAnnotations(?ResolvedPhpDocBlock $phpDoc): array |
| 22 | + { |
| 23 | + if ($phpDoc === null) { |
| 24 | + return []; |
| 25 | + } |
| 26 | + |
| 27 | + $phpDocNodes = $phpDoc->getPhpDocNodes(); |
| 28 | + |
| 29 | + $annotations = []; |
| 30 | + |
| 31 | + foreach ($phpDocNodes as $docNode) { |
| 32 | + $annotations = array_merge( |
| 33 | + $annotations, |
| 34 | + $docNode->getTagsByName('@dataProvider') |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + return $annotations; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return RuleError[] errors |
| 43 | + */ |
| 44 | + public function processDataProvider( |
| 45 | + Scope $scope, |
| 46 | + PhpDocTagNode $phpDocTag, |
| 47 | + bool $checkFunctionNameCase |
| 48 | + ): array |
| 49 | + { |
| 50 | + $dataProviderName = $this->getDataProviderName($phpDocTag); |
| 51 | + if ($dataProviderName === null) { |
| 52 | + // Missing name is already handled in NoMissingSpaceInMethodAnnotationRule |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + $classReflection = $scope->getClassReflection(); |
| 57 | + if ($classReflection === null) { |
| 58 | + // Should not happen |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + $dataProviderMethodReflection = $classReflection->getNativeMethod($dataProviderName); |
| 64 | + } catch (MissingMethodFromReflectionException $missingMethodFromReflectionException) { |
| 65 | + $error = RuleErrorBuilder::message(sprintf( |
| 66 | + '@dataProvider %s related method not found.', |
| 67 | + $dataProviderName |
| 68 | + ))->build(); |
| 69 | + |
| 70 | + return [$error]; |
| 71 | + } |
| 72 | + |
| 73 | + $errors = []; |
| 74 | + |
| 75 | + if ($checkFunctionNameCase && $dataProviderName !== $dataProviderMethodReflection->getName()) { |
| 76 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 77 | + '@dataProvider %s related method is used with incorrect case: %s.', |
| 78 | + $dataProviderName, |
| 79 | + $dataProviderMethodReflection->getName() |
| 80 | + ))->build(); |
| 81 | + } |
| 82 | + |
| 83 | + if (!$dataProviderMethodReflection->isPublic()) { |
| 84 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 85 | + '@dataProvider %s related method must be public.', |
| 86 | + $dataProviderName |
| 87 | + ))->build(); |
| 88 | + } |
| 89 | + |
| 90 | + return $errors; |
| 91 | + } |
| 92 | + |
| 93 | + private function getDataProviderName(PhpDocTagNode $phpDocTag): ?string |
| 94 | + { |
| 95 | + if (preg_match('/^[^ \t]+/', (string) $phpDocTag->value, $matches) !== 1) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + return $matches[0]; |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments