|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\PHPUnit; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Name; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Node\ClassMethod; |
| 9 | +use PHPStan\Parser\Parser; |
| 10 | +use PHPStan\PhpDoc\ResolvedPhpDocBlock; |
| 11 | +use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
| 12 | +use PHPStan\Reflection\ClassReflection; |
| 13 | +use PHPStan\Reflection\ReflectionProvider; |
| 14 | +use PHPStan\Rules\IdentifierRuleError; |
| 15 | +use PHPStan\Rules\RuleErrorBuilder; |
| 16 | +use PHPStan\Type\FileTypeMapper; |
| 17 | +use ReflectionMethod; |
| 18 | +use function array_merge; |
| 19 | +use function explode; |
| 20 | +use function sprintf; |
| 21 | +use function strpos; |
| 22 | + |
| 23 | +final class TestMethodsHelper |
| 24 | +{ |
| 25 | + private ReflectionProvider $reflectionProvider; |
| 26 | + |
| 27 | + private FileTypeMapper $fileTypeMapper; |
| 28 | + |
| 29 | + private Parser $parser; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + ReflectionProvider $reflectionProvider, |
| 33 | + FileTypeMapper $fileTypeMapper, |
| 34 | + Parser $parser |
| 35 | + ) |
| 36 | + { |
| 37 | + $this->reflectionProvider = $reflectionProvider; |
| 38 | + $this->fileTypeMapper = $fileTypeMapper; |
| 39 | + $this->parser = $parser; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @return array<ReflectionMethod> |
| 44 | + */ |
| 45 | + public function getTestMethods(ClassReflection $class): array |
| 46 | + { |
| 47 | + $testMethods = []; |
| 48 | + foreach($class->getNativeReflection()->getMethods() as $reflectionMethod) { |
| 49 | + if (str_starts_with(strtolower($reflectionMethod->getName()), 'test')) { |
| 50 | + $testMethods[] = $reflectionMethod; |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + // todo: detect tests with @test annotation |
| 55 | + |
| 56 | + $testAttributes = $reflectionMethod->getAttributes('PHPUnit\Framework\Attribute\Test'); |
| 57 | + if ($testAttributes !== []) { |
| 58 | + $testMethods[] = $reflectionMethod; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return $testMethods; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return iterable<array{string}> |
| 67 | + */ |
| 68 | + public function getDataProviderMethods( |
| 69 | + Scope $scope, |
| 70 | + ReflectionMethod $node, |
| 71 | + ClassReflection $classReflection |
| 72 | + ): iterable |
| 73 | + { |
| 74 | + /* |
| 75 | + $docComment = $node->getDocComment(); |
| 76 | + if ($docComment !== null) { |
| 77 | + $methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
| 78 | + $scope->getFile(), |
| 79 | + $classReflection->getName(), |
| 80 | + $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, |
| 81 | + $node->name->toString(), |
| 82 | + $docComment->getText(), |
| 83 | + ); |
| 84 | + foreach ($this->getDataProviderAnnotations($methodPhpDoc) as $annotation) { |
| 85 | + $dataProviderValue = $this->getDataProviderAnnotationValue($annotation); |
| 86 | + if ($dataProviderValue === null) { |
| 87 | + // Missing value is already handled in NoMissingSpaceInMethodAnnotationRule |
| 88 | + continue; |
| 89 | + } |
| 90 | +
|
| 91 | + $dataProviderMethod = $this->parseDataProviderAnnotationValue($scope, $dataProviderValue); |
| 92 | + $dataProviderMethod[] = $node->getStartLine(); |
| 93 | +
|
| 94 | + yield $dataProviderValue => $dataProviderMethod; |
| 95 | + } |
| 96 | + } |
| 97 | +
|
| 98 | + if (!$this->phpunit10OrNewer) { |
| 99 | + return; |
| 100 | + } |
| 101 | + */ |
| 102 | + |
| 103 | + foreach ($node->getAttributes('PHPUnit\Framework\Attributes\DataProvider') as $attr) { |
| 104 | + $args = $attr->getArguments(); |
| 105 | + if (count($args) !== 1) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + yield [$args[0]]; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments