diff --git a/lib/private/AppFramework/Middleware/MiddlewareUtils.php b/lib/private/AppFramework/Middleware/MiddlewareUtils.php index 94ac2d1f44c..de5abf6c246 100644 --- a/lib/private/AppFramework/Middleware/MiddlewareUtils.php +++ b/lib/private/AppFramework/Middleware/MiddlewareUtils.php @@ -31,19 +31,10 @@ class MiddlewareUtils { * @param ReflectionMethod $reflectionMethod * @param ?string $annotationName * @param class-string $attributeClass - * @return boolean + * @deprecated 34.0.0 call directly on the reflector */ public function hasAnnotationOrAttribute(ReflectionMethod $reflectionMethod, ?string $annotationName, string $attributeClass): bool { - if (!empty($reflectionMethod->getAttributes($attributeClass))) { - return true; - } - - if ($annotationName && $this->reflector->hasAnnotation($annotationName)) { - $this->logger->debug($reflectionMethod->getDeclaringClass()->getName() . '::' . $reflectionMethod->getName() . ' uses the @' . $annotationName . ' annotation and should use the #[' . $attributeClass . '] attribute instead'); - return true; - } - - return false; + return $this->reflector->hasAnnotationOrAttribute($annotationName, $attributeClass); } /** diff --git a/lib/private/AppFramework/Utility/ControllerMethodReflector.php b/lib/private/AppFramework/Utility/ControllerMethodReflector.php index b63be92dc3b..1360ca2737c 100644 --- a/lib/private/AppFramework/Utility/ControllerMethodReflector.php +++ b/lib/private/AppFramework/Utility/ControllerMethodReflector.php @@ -9,28 +9,35 @@ declare(strict_types=1); namespace OC\AppFramework\Utility; use OCP\AppFramework\Utility\IControllerMethodReflector; +use Psr\Log\LoggerInterface; /** * Reads and parses annotations from doc comments */ class ControllerMethodReflector implements IControllerMethodReflector { - public $annotations = []; - private $types = []; - private $parameters = []; + public array $annotations = []; + private array $types = []; + private array $parameters = []; private array $ranges = []; private int $startLine = 0; private string $file = ''; + private ?\ReflectionMethod $reflectionMethod = null; + + public function __construct( + private readonly LoggerInterface $logger, + ) { + } /** * @param object $object an object or classname * @param string $method the method which we want to inspect */ public function reflect($object, string $method) { - $reflection = new \ReflectionMethod($object, $method); - $this->startLine = $reflection->getStartLine(); - $this->file = $reflection->getFileName(); + $this->reflectionMethod = new \ReflectionMethod($object, $method); + $this->startLine = $this->reflectionMethod->getStartLine(); + $this->file = $this->reflectionMethod->getFileName(); - $docs = $reflection->getDocComment(); + $docs = $this->reflectionMethod->getDocComment(); if ($docs !== false) { // extract everything prefixed by @ and first letter uppercase @@ -69,7 +76,7 @@ class ControllerMethodReflector implements IControllerMethodReflector { } } - foreach ($reflection->getParameters() as $param) { + foreach ($this->reflectionMethod->getParameters() as $param) { // extract type information from PHP 7 scalar types and prefer them over phpdoc annotations $type = $param->getType(); if ($type instanceof \ReflectionNamedType) { @@ -114,6 +121,24 @@ class ControllerMethodReflector implements IControllerMethodReflector { return $this->parameters; } + /** + * @template T + * + * @param class-string $attributeClass + */ + public function hasAnnotationOrAttribute(?string $annotationName, string $attributeClass): bool { + if (!empty($this->reflectionMethod->getAttributes($attributeClass))) { + return true; + } + + if ($annotationName && $this->hasAnnotation($annotationName)) { + $this->logger->debug($this->reflectionMethod->getDeclaringClass()->getName() . '::' . $this->reflectionMethod->getName() . ' uses the @' . $annotationName . ' annotation and should use the #[' . $attributeClass . '] attribute instead'); + return true; + } + + return false; + } + /** * Check if a method contains an annotation * @param string $name the name of the annotation diff --git a/lib/public/AppFramework/Utility/IControllerMethodReflector.php b/lib/public/AppFramework/Utility/IControllerMethodReflector.php index 95d7fbebb56..480b4a3576c 100644 --- a/lib/public/AppFramework/Utility/IControllerMethodReflector.php +++ b/lib/public/AppFramework/Utility/IControllerMethodReflector.php @@ -56,4 +56,15 @@ interface IControllerMethodReflector { * @see https://help.nextcloud.com/t/how-should-we-use-php8-attributes/104278 */ public function hasAnnotation(string $name): bool; + + /** + * @template T + * + * Check if a method contains an annotation or an attribute. + * Log a debug line if the annotation is used. + * + * @param class-string $attributeClass + * @since 34.0.0 + */ + public function hasAnnotationOrAttribute(?string $annotationName, string $attributeClass): bool; }