-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add a getAttributes to IControllerMethodReflector #37279
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
Closed
nickvergessen
wants to merge
1
commit into
master
from
techdebt/noid/bruteforce-protection-attribute
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -33,12 +33,15 @@ | |||||
| */ | ||||||
| namespace OC\AppFramework\Utility; | ||||||
|
|
||||||
| use Attribute; | ||||||
| use OCP\AppFramework\Utility\IControllerMethodReflector; | ||||||
|
|
||||||
| /** | ||||||
| * Reads and parses annotations from doc comments | ||||||
| */ | ||||||
| class ControllerMethodReflector implements IControllerMethodReflector { | ||||||
|
|
||||||
| protected ?\ReflectionMethod $reflection; | ||||||
|
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. To be more specific because exist other types of reflections:
Suggested change
|
||||||
| public $annotations = []; | ||||||
| private $types = []; | ||||||
| private $parameters = []; | ||||||
|
|
@@ -48,8 +51,8 @@ class ControllerMethodReflector implements IControllerMethodReflector { | |||||
| * @param string $method the method which we want to inspect | ||||||
| */ | ||||||
| public function reflect($object, string $method) { | ||||||
| $reflection = new \ReflectionMethod($object, $method); | ||||||
| $docs = $reflection->getDocComment(); | ||||||
| $this->reflection = new \ReflectionMethod($object, $method); | ||||||
| $docs = $this->reflection->getDocComment(); | ||||||
|
|
||||||
| if ($docs !== false) { | ||||||
| // extract everything prefixed by @ and first letter uppercase | ||||||
|
|
@@ -76,7 +79,7 @@ public function reflect($object, string $method) { | |||||
| $this->types = array_combine($matches['var'], $matches['type']); | ||||||
| } | ||||||
|
|
||||||
| foreach ($reflection->getParameters() as $param) { | ||||||
| foreach ($this->reflection->getParameters() as $param) { | ||||||
| // extract type information from PHP 7 scalar types and prefer them over phpdoc annotations | ||||||
| $type = $param->getType(); | ||||||
| if ($type instanceof \ReflectionNamedType) { | ||||||
|
|
@@ -138,4 +141,18 @@ public function getAnnotationParameter(string $name, string $key): string { | |||||
|
|
||||||
| return ''; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @template T of Attribute | ||||||
|
Contributor
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. Attribute classes do not extend Attribute class, which is final. So you cannot do that. |
||||||
| * @param class-string<T> $class | ||||||
| * @return T[] | ||||||
| */ | ||||||
| public function getAttributes(string $class): array { | ||||||
| $attributes = $this->reflection->getAttributes($class); | ||||||
| $return = []; | ||||||
| foreach ($attributes as $attribute) { | ||||||
| $return[] = $attribute->newInstance(); | ||||||
| } | ||||||
| return $return; | ||||||
| } | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / Psalm
InvalidArgument