mirror of
https://github.com/nextcloud/server.git
synced 2026-06-10 09:13:19 -04:00
fix: Move hasAnnotationOrAttribute to the reflector
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
parent
a304a54775
commit
520878338f
3 changed files with 46 additions and 19 deletions
|
|
@ -31,19 +31,10 @@ class MiddlewareUtils {
|
|||
* @param ReflectionMethod $reflectionMethod
|
||||
* @param ?string $annotationName
|
||||
* @param class-string<T> $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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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<T> $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
|
||||
|
|
|
|||
|
|
@ -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<T> $attributeClass
|
||||
* @since 34.0.0
|
||||
*/
|
||||
public function hasAnnotationOrAttribute(?string $annotationName, string $attributeClass): bool;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue