- Notifications
You must be signed in to change notification settings - Fork 50
Implement DataProviderDataRule #238
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
Changes from 1 commit
c4597bf ccff992 0975030 5c6445a ff6843e 1dd180c cff1f79 0ab211d 7f7b38b d74aeec 09b226b f9c8af7 d3a2b9b f42bd8a 3318b73 f715fbf 67a40d6 6f4dcc9 20d1408 5cb8c63 72d5655 1453c20 1506a06 9b69121 459eb1d 5fcf01d a978aa8 a1dad84 1d3db54 7417fbc 74b67cf 5cb7312 9ee26b1 4cb9404 34a0a0e bb855f0 a56169b e31a234 1532c26 68e65e9 30e27db aafaf8f b6f11fe 72dc9e0 ccb7b04 8491dd7 aea7e42 a1e5a19 7f9dbf2 c689efe d6c11be 21740cb 41acc04 807a35f 3fc4e0e 9a0d039 daf978e 534694d 58461d7 b36a404 f083e63 42ad04e 0731026 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
test annotation and attribute - Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| | @@ -2,7 +2,9 @@ | |||
| | ||||
| namespace PHPStan\Rules\PHPUnit; | ||||
| | ||||
| use PHPStan\Analyser\Scope; | ||||
| use PHPStan\Parser\Parser; | ||||
| use PHPStan\PhpDoc\ResolvedPhpDocBlock; | ||||
| use PHPStan\Reflection\ClassReflection; | ||||
| use PHPStan\Reflection\ReflectionProvider; | ||||
| use PHPStan\Type\FileTypeMapper; | ||||
| | @@ -33,18 +35,43 @@ public function __construct( | |||
| /** | ||||
| * @return array<ReflectionMethod> | ||||
| Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a weird type. I'd prefer our own ExtendedMethodReflection to be returned. You can get Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed this because I was looking for how you're handling variadic methods and the Contributor Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean there isn't Contributor Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see
| ||||
| */ | ||||
| public function getTestMethods(ClassReflection $class): array | ||||
| public function getTestMethods(ClassReflection $classReflection, Scope $scope): array | ||||
| { | ||||
| $testMethods = []; | ||||
| foreach ($class->getNativeReflection()->getMethods() as $reflectionMethod) { | ||||
| foreach ($classReflection->getNativeReflection()->getMethods() as $reflectionMethod) { | ||||
| if (!$reflectionMethod->isPublic()) { | ||||
| continue; | ||||
| } | ||||
| | ||||
| if (str_starts_with(strtolower($reflectionMethod->getName()), 'test')) { | ||||
| $testMethods[] = $reflectionMethod; | ||||
| continue; | ||||
| } | ||||
| | ||||
| $docComment = $reflectionMethod->getDocComment(); | ||||
| if ($docComment !== false) { | ||||
| $methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( | ||||
| $scope->getFile(), | ||||
| $classReflection->getName(), | ||||
| $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, | ||||
| $reflectionMethod->getName(), | ||||
| $docComment, | ||||
| ); | ||||
| | ||||
| if ($this->hasTestAnnotation($methodPhpDoc)) { | ||||
| $testMethods[] = $reflectionMethod; | ||||
| continue; | ||||
| } | ||||
| } | ||||
| | ||||
| // todo: detect tests with @test annotation | ||||
| | ||||
| $testAttributes = $reflectionMethod->getAttributes('PHPUnit\Framework\Attribute\Test'); | ||||
| // XXX | ||||
| //if (!$this->phpunit10OrNewer) { | ||||
| // return; | ||||
| //} | ||||
| | ||||
| $testAttributes = $reflectionMethod->getAttributes('PHPUnit\Framework\Attributes\Test'); | ||||
| if ($testAttributes === []) { | ||||
| continue; | ||||
| } | ||||
| | @@ -55,4 +82,22 @@ public function getTestMethods(ClassReflection $class): array | |||
| return $testMethods; | ||||
| } | ||||
| | ||||
| private function hasTestAnnotation(?ResolvedPhpDocBlock $phpDoc): bool | ||||
| { | ||||
| if ($phpDoc === null) { | ||||
| return false; | ||||
| } | ||||
| | ||||
| $phpDocNodes = $phpDoc->getPhpDocNodes(); | ||||
| | ||||
| foreach ($phpDocNodes as $docNode) { | ||||
| $tags = $docNode->getTagsByName('@test'); | ||||
| if ($tags !== []) { | ||||
| return true; | ||||
| } | ||||
| } | ||||
| | ||||
| return false; | ||||
| } | ||||
| | ||||
| } | ||||
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.
Do you think it would be worth having a cache on
getDataProviderMethods(and maybe getTestMethods ?) ?Cause we will compute this for every possible return of a TestCase class, and the result will always be the same
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.
I am fine/willing to add a cache in case we find a case where it turns into a measurable improvement