PageRenderTime 22ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Front_End/vendor/phpdocumentor/reflection-docblock/src/DocBlock/Tags/Method.php

https://gitlab.com/Sigpot/AirSpot
PHP | 220 lines | 150 code | 25 blank | 45 comment | 10 complexity | 4be8abf3404b2c39f18da5517368cceb MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of phpDocumentor.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
  9. * @license http://www.opensource.org/licenses/mit-license.php MIT
  10. * @link http://phpdoc.org
  11. */
  12. namespace phpDocumentor\Reflection\DocBlock\Tags;
  13. use phpDocumentor\Reflection\DocBlock\Description;
  14. use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
  15. use phpDocumentor\Reflection\Type;
  16. use phpDocumentor\Reflection\TypeResolver;
  17. use phpDocumentor\Reflection\Types\Context as TypeContext;
  18. use phpDocumentor\Reflection\Types\Void_;
  19. use Webmozart\Assert\Assert;
  20. /**
  21. * Reflection class for an {@}method in a Docblock.
  22. */
  23. final class Method extends BaseTag implements Factory\StaticMethod
  24. {
  25. protected $name = 'method';
  26. /** @var string */
  27. private $methodName = '';
  28. /** @var string[] */
  29. private $arguments = [];
  30. /** @var bool */
  31. private $isStatic = false;
  32. /** @var Type */
  33. private $returnType;
  34. public function __construct(
  35. $methodName,
  36. array $arguments = [],
  37. Type $returnType = null,
  38. $static = false,
  39. Description $description = null
  40. ) {
  41. Assert::stringNotEmpty($methodName);
  42. Assert::boolean($static);
  43. if ($returnType === null) {
  44. $returnType = new Void_();
  45. }
  46. $this->methodName = $methodName;
  47. $this->arguments = $this->filterArguments($arguments);
  48. $this->returnType = $returnType;
  49. $this->isStatic = $static;
  50. $this->description = $description;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public static function create(
  56. $body,
  57. TypeResolver $typeResolver = null,
  58. DescriptionFactory $descriptionFactory = null,
  59. TypeContext $context = null
  60. ) {
  61. Assert::stringNotEmpty($body);
  62. Assert::allNotNull([ $typeResolver, $descriptionFactory ]);
  63. // 1. none or more whitespace
  64. // 2. optionally the keyword "static" followed by whitespace
  65. // 3. optionally a word with underscores followed by whitespace : as
  66. // type for the return value
  67. // 4. then optionally a word with underscores followed by () and
  68. // whitespace : as method name as used by phpDocumentor
  69. // 5. then a word with underscores, followed by ( and any character
  70. // until a ) and whitespace : as method name with signature
  71. // 6. any remaining text : as description
  72. if (!preg_match(
  73. '/^
  74. # Static keyword
  75. # Declares a static method ONLY if type is also present
  76. (?:
  77. (static)
  78. \s+
  79. )?
  80. # Return type
  81. (?:
  82. (
  83. (?:[\w\|_\\\\]+)
  84. # array notation
  85. (?:\[\])*
  86. )?
  87. \s+
  88. )?
  89. # Legacy method name (not captured)
  90. (?:
  91. [\w_]+\(\)\s+
  92. )?
  93. # Method name
  94. ([\w\|_\\\\]+)
  95. # Arguments
  96. (?:
  97. \(([^\)]*)\)
  98. )?
  99. \s*
  100. # Description
  101. (.*)
  102. $/sux',
  103. $body,
  104. $matches
  105. )) {
  106. return null;
  107. }
  108. list(, $static, $returnType, $methodName, $arguments, $description) = $matches;
  109. $static = $static === 'static';
  110. $returnType = $typeResolver->resolve($returnType, $context);
  111. $description = $descriptionFactory->create($description, $context);
  112. if ('' !== $arguments) {
  113. $arguments = explode(',', $arguments);
  114. foreach($arguments as &$argument) {
  115. $argument = explode(' ', trim($argument));
  116. if ($argument[0][0] === '$') {
  117. $argumentName = substr($argument[0], 1);
  118. $argumentType = new Void_();
  119. } else {
  120. $argumentType = $typeResolver->resolve($argument[0], $context);
  121. $argumentName = '';
  122. if (isset($argument[1])) {
  123. $argumentName = substr($argument[1], 1);
  124. }
  125. }
  126. $argument = [ 'name' => $argumentName, 'type' => $argumentType];
  127. }
  128. } else {
  129. $arguments = [];
  130. }
  131. return new static($methodName, $arguments, $returnType, $static, $description);
  132. }
  133. /**
  134. * Retrieves the method name.
  135. *
  136. * @return string
  137. */
  138. public function getMethodName()
  139. {
  140. return $this->methodName;
  141. }
  142. /**
  143. * @return string[]
  144. */
  145. public function getArguments()
  146. {
  147. return $this->arguments;
  148. }
  149. /**
  150. * Checks whether the method tag describes a static method or not.
  151. *
  152. * @return bool TRUE if the method declaration is for a static method, FALSE otherwise.
  153. */
  154. public function isStatic()
  155. {
  156. return $this->isStatic;
  157. }
  158. /**
  159. * @return Type
  160. */
  161. public function getReturnType()
  162. {
  163. return $this->returnType;
  164. }
  165. public function __toString()
  166. {
  167. $arguments = [];
  168. foreach ($this->arguments as $argument) {
  169. $arguments[] = $argument['type'] . ' $' . $argument['name'];
  170. }
  171. return ($this->isStatic() ? 'static ' : '')
  172. . (string)$this->returnType . ' '
  173. . $this->methodName
  174. . '(' . implode(', ', $arguments) . ')'
  175. . ($this->description ? ' ' . $this->description->render() : '');
  176. }
  177. private function filterArguments($arguments)
  178. {
  179. foreach ($arguments as &$argument) {
  180. if (is_string($argument)) {
  181. $argument = [ 'name' => $argument ];
  182. }
  183. if (! isset($argument['type'])) {
  184. $argument['type'] = new Void_();
  185. }
  186. $keys = array_keys($argument);
  187. if ($keys !== [ 'name', 'type' ]) {
  188. throw new \InvalidArgumentException(
  189. 'Arguments can only have the "name" and "type" fields, found: ' . var_export($keys, true)
  190. );
  191. }
  192. }
  193. return $arguments;
  194. }
  195. }