ControllerMethodReflector.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Olivier Paroz <github@oparoz.com>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\AppFramework\Utility;
  33. use OCP\AppFramework\Utility\IControllerMethodReflector;
  34. /**
  35. * Reads and parses annotations from doc comments
  36. */
  37. class ControllerMethodReflector implements IControllerMethodReflector {
  38. public $annotations = [];
  39. private $types = [];
  40. private $parameters = [];
  41. private array $ranges = [];
  42. /**
  43. * @param object $object an object or classname
  44. * @param string $method the method which we want to inspect
  45. */
  46. public function reflect($object, string $method) {
  47. $reflection = new \ReflectionMethod($object, $method);
  48. $docs = $reflection->getDocComment();
  49. if ($docs !== false) {
  50. // extract everything prefixed by @ and first letter uppercase
  51. preg_match_all('/^\h+\*\h+@(?P<annotation>[A-Z]\w+)((?P<parameter>.*))?$/m', $docs, $matches);
  52. foreach ($matches['annotation'] as $key => $annotation) {
  53. $annotation = strtolower($annotation);
  54. $annotationValue = $matches['parameter'][$key];
  55. if (str_starts_with($annotationValue, '(') && str_ends_with($annotationValue, ')')) {
  56. $cutString = substr($annotationValue, 1, -1);
  57. $cutString = str_replace(' ', '', $cutString);
  58. $splitArray = explode(',', $cutString);
  59. foreach ($splitArray as $annotationValues) {
  60. [$key, $value] = explode('=', $annotationValues);
  61. $this->annotations[$annotation][$key] = $value;
  62. }
  63. continue;
  64. }
  65. $this->annotations[$annotation] = [$annotationValue];
  66. }
  67. // extract type parameter information
  68. preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches);
  69. $this->types = array_combine($matches['var'], $matches['type']);
  70. preg_match_all('/@psalm-param\h+(?P<type>\w+)<(?P<rangeMin>(-?\d+|min)),\h*(?P<rangeMax>(-?\d+|max))>\h+\$(?P<var>\w+)/', $docs, $matches);
  71. foreach ($matches['var'] as $index => $varName) {
  72. if ($matches['type'][$index] !== 'int') {
  73. // only int ranges are possible at the moment
  74. // @see https://psalm.dev/docs/annotating_code/type_syntax/scalar_types
  75. continue;
  76. }
  77. $this->ranges[$varName] = [
  78. 'min' => $matches['rangeMin'][$index] === 'min' ? PHP_INT_MIN : (int)$matches['rangeMin'][$index],
  79. 'max' => $matches['rangeMax'][$index] === 'max' ? PHP_INT_MAX : (int)$matches['rangeMax'][$index],
  80. ];
  81. }
  82. }
  83. foreach ($reflection->getParameters() as $param) {
  84. // extract type information from PHP 7 scalar types and prefer them over phpdoc annotations
  85. $type = $param->getType();
  86. if ($type instanceof \ReflectionNamedType) {
  87. $this->types[$param->getName()] = $type->getName();
  88. }
  89. $default = null;
  90. if ($param->isOptional()) {
  91. $default = $param->getDefaultValue();
  92. }
  93. $this->parameters[$param->name] = $default;
  94. }
  95. }
  96. /**
  97. * Inspects the PHPDoc parameters for types
  98. * @param string $parameter the parameter whose type comments should be
  99. * parsed
  100. * @return string|null type in the type parameters (@param int $something)
  101. * would return int or null if not existing
  102. */
  103. public function getType(string $parameter) {
  104. if (array_key_exists($parameter, $this->types)) {
  105. return $this->types[$parameter];
  106. }
  107. return null;
  108. }
  109. public function getRange(string $parameter): ?array {
  110. if (array_key_exists($parameter, $this->ranges)) {
  111. return $this->ranges[$parameter];
  112. }
  113. return null;
  114. }
  115. /**
  116. * @return array the arguments of the method with key => default value
  117. */
  118. public function getParameters(): array {
  119. return $this->parameters;
  120. }
  121. /**
  122. * Check if a method contains an annotation
  123. * @param string $name the name of the annotation
  124. * @return bool true if the annotation is found
  125. */
  126. public function hasAnnotation(string $name): bool {
  127. $name = strtolower($name);
  128. return array_key_exists($name, $this->annotations);
  129. }
  130. /**
  131. * Get optional annotation parameter by key
  132. *
  133. * @param string $name the name of the annotation
  134. * @param string $key the string of the annotation
  135. * @return string
  136. */
  137. public function getAnnotationParameter(string $name, string $key): string {
  138. $name = strtolower($name);
  139. if (isset($this->annotations[$name][$key])) {
  140. return $this->annotations[$name][$key];
  141. }
  142. return '';
  143. }
  144. }