ControllerMethodReflector.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\AppFramework\Utility;
  9. use OCP\AppFramework\Utility\IControllerMethodReflector;
  10. /**
  11. * Reads and parses annotations from doc comments
  12. */
  13. class ControllerMethodReflector implements IControllerMethodReflector {
  14. public $annotations = [];
  15. private $types = [];
  16. private $parameters = [];
  17. private array $ranges = [];
  18. /**
  19. * @param object $object an object or classname
  20. * @param string $method the method which we want to inspect
  21. */
  22. public function reflect($object, string $method) {
  23. $reflection = new \ReflectionMethod($object, $method);
  24. $docs = $reflection->getDocComment();
  25. if ($docs !== false) {
  26. // extract everything prefixed by @ and first letter uppercase
  27. preg_match_all('/^\h+\*\h+@(?P<annotation>[A-Z]\w+)((?P<parameter>.*))?$/m', $docs, $matches);
  28. foreach ($matches['annotation'] as $key => $annotation) {
  29. $annotation = strtolower($annotation);
  30. $annotationValue = $matches['parameter'][$key];
  31. if (str_starts_with($annotationValue, '(') && str_ends_with($annotationValue, ')')) {
  32. $cutString = substr($annotationValue, 1, -1);
  33. $cutString = str_replace(' ', '', $cutString);
  34. $splitArray = explode(',', $cutString);
  35. foreach ($splitArray as $annotationValues) {
  36. [$key, $value] = explode('=', $annotationValues);
  37. $this->annotations[$annotation][$key] = $value;
  38. }
  39. continue;
  40. }
  41. $this->annotations[$annotation] = [$annotationValue];
  42. }
  43. // extract type parameter information
  44. preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches);
  45. $this->types = array_combine($matches['var'], $matches['type']);
  46. preg_match_all('/@psalm-param\h+(?P<type>\w+)<(?P<rangeMin>(-?\d+|min)),\h*(?P<rangeMax>(-?\d+|max))>\h+\$(?P<var>\w+)/', $docs, $matches);
  47. foreach ($matches['var'] as $index => $varName) {
  48. if ($matches['type'][$index] !== 'int') {
  49. // only int ranges are possible at the moment
  50. // @see https://psalm.dev/docs/annotating_code/type_syntax/scalar_types
  51. continue;
  52. }
  53. $this->ranges[$varName] = [
  54. 'min' => $matches['rangeMin'][$index] === 'min' ? PHP_INT_MIN : (int)$matches['rangeMin'][$index],
  55. 'max' => $matches['rangeMax'][$index] === 'max' ? PHP_INT_MAX : (int)$matches['rangeMax'][$index],
  56. ];
  57. }
  58. }
  59. foreach ($reflection->getParameters() as $param) {
  60. // extract type information from PHP 7 scalar types and prefer them over phpdoc annotations
  61. $type = $param->getType();
  62. if ($type instanceof \ReflectionNamedType) {
  63. $this->types[$param->getName()] = $type->getName();
  64. }
  65. $default = null;
  66. if ($param->isOptional()) {
  67. $default = $param->getDefaultValue();
  68. }
  69. $this->parameters[$param->name] = $default;
  70. }
  71. }
  72. /**
  73. * Inspects the PHPDoc parameters for types
  74. * @param string $parameter the parameter whose type comments should be
  75. * parsed
  76. * @return string|null type in the type parameters (@param int $something)
  77. * would return int or null if not existing
  78. */
  79. public function getType(string $parameter) {
  80. if (array_key_exists($parameter, $this->types)) {
  81. return $this->types[$parameter];
  82. }
  83. return null;
  84. }
  85. public function getRange(string $parameter): ?array {
  86. if (array_key_exists($parameter, $this->ranges)) {
  87. return $this->ranges[$parameter];
  88. }
  89. return null;
  90. }
  91. /**
  92. * @return array the arguments of the method with key => default value
  93. */
  94. public function getParameters(): array {
  95. return $this->parameters;
  96. }
  97. /**
  98. * Check if a method contains an annotation
  99. * @param string $name the name of the annotation
  100. * @return bool true if the annotation is found
  101. */
  102. public function hasAnnotation(string $name): bool {
  103. $name = strtolower($name);
  104. return array_key_exists($name, $this->annotations);
  105. }
  106. /**
  107. * Get optional annotation parameter by key
  108. *
  109. * @param string $name the name of the annotation
  110. * @param string $key the string of the annotation
  111. * @return string
  112. */
  113. public function getAnnotationParameter(string $name, string $key): string {
  114. $name = strtolower($name);
  115. if (isset($this->annotations[$name][$key])) {
  116. return $this->annotations[$name][$key];
  117. }
  118. return '';
  119. }
  120. }