PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/barryvdh/laravel-ide-helper/src/Method.php

https://gitlab.com/4gdevs/online-class-record-system
PHP | 313 lines | 168 code | 36 blank | 109 comment | 14 complexity | c0f9e604d1eb083ef671fd1f69236b65 MD5 | raw file
  1. <?php
  2. /**
  3. * Laravel IDE Helper Generator
  4. *
  5. * @author Barry vd. Heuvel <barryvdh@gmail.com>
  6. * @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
  7. * @license http://www.opensource.org/licenses/mit-license.php MIT
  8. * @link https://github.com/barryvdh/laravel-ide-helper
  9. */
  10. namespace Barryvdh\LaravelIdeHelper;
  11. use phpDocumentor\Reflection\DocBlock;
  12. use phpDocumentor\Reflection\DocBlock\Context;
  13. use phpDocumentor\Reflection\DocBlock\Tag;
  14. use phpDocumentor\Reflection\DocBlock\Tag\ReturnTag;
  15. use phpDocumentor\Reflection\DocBlock\Tag\ParamTag;
  16. use phpDocumentor\Reflection\DocBlock\Serializer as DocBlockSerializer;
  17. class Method
  18. {
  19. /** @var \phpDocumentor\Reflection\DocBlock */
  20. protected $phpdoc;
  21. /** @var \ReflectionMethod */
  22. protected $method;
  23. protected $output = '';
  24. protected $name;
  25. protected $namespace;
  26. protected $params = array();
  27. protected $params_with_default = array();
  28. protected $interfaces = array();
  29. protected $return = null;
  30. /**
  31. * @param \ReflectionMethod $method
  32. * @param string $alias
  33. * @param string $class
  34. * @param string|null $methodName
  35. * @param array $interfaces
  36. */
  37. public function __construct(\ReflectionMethod $method, $alias, $class, $methodName = null, $interfaces = array())
  38. {
  39. $this->method = $method;
  40. $this->interfaces = $interfaces;
  41. $this->name = $methodName ?: $method->name;
  42. $this->namespace = $method->getDeclaringClass()->getNamespaceName();
  43. //Create a DocBlock and serializer instance
  44. $this->phpdoc = new DocBlock($method, new Context($this->namespace));
  45. //Normalize the description and inherit the docs from parents/interfaces
  46. try {
  47. $this->normalizeParams($this->phpdoc);
  48. $this->normalizeReturn($this->phpdoc);
  49. $this->normalizeDescription($this->phpdoc);
  50. } catch (\Exception $e) {}
  51. //Get the parameters, including formatted default values
  52. $this->getParameters($method);
  53. //Make the method static
  54. $this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc));
  55. //Reference the 'real' function in the declaringclass
  56. $declaringClass = $method->getDeclaringClass();
  57. $this->declaringClassName = '\\' . ltrim($declaringClass->name, '\\');
  58. $this->root = '\\' . ltrim($class->getName(), '\\');
  59. }
  60. /**
  61. * Get the class wherein the function resides
  62. *
  63. * @return string
  64. */
  65. public function getDeclaringClass()
  66. {
  67. return $this->declaringClassName;
  68. }
  69. /**
  70. * Return the class from which this function would be called
  71. *
  72. * @return string
  73. */
  74. public function getRoot()
  75. {
  76. return $this->root;
  77. }
  78. /**
  79. * Get the docblock for this method
  80. *
  81. * @param string $prefix
  82. * @return mixed
  83. */
  84. public function getDocComment($prefix = "\t\t")
  85. {
  86. $serializer = new DocBlockSerializer(1, $prefix);
  87. return $serializer->getDocComment($this->phpdoc);
  88. }
  89. /**
  90. * Get the method name
  91. *
  92. * @return string
  93. */
  94. public function getName()
  95. {
  96. return $this->name;
  97. }
  98. /**
  99. * Get the parameters for this method
  100. *
  101. * @param bool $implode Wether to implode the array or not
  102. * @return string
  103. */
  104. public function getParams($implode = true)
  105. {
  106. return $implode ? implode(', ', $this->params) : $this->params;
  107. }
  108. /**
  109. * Get the parameters for this method including default values
  110. *
  111. * @param bool $implode Wether to implode the array or not
  112. * @return string
  113. */
  114. public function getParamsWithDefault($implode = true)
  115. {
  116. return $implode ? implode(', ', $this->params_with_default) : $this->params_with_default;
  117. }
  118. /**
  119. * Get the description and get the inherited docs.
  120. *
  121. * @param DocBlock $phpdoc
  122. */
  123. protected function normalizeDescription(DocBlock $phpdoc)
  124. {
  125. //Get the short + long description from the DocBlock
  126. $description = $phpdoc->getText();
  127. //Loop through parents/interfaces, to fill in {@inheritdoc}
  128. if (strpos($description, '{@inheritdoc}') !== false) {
  129. $inheritdoc = $this->getInheritDoc($this->method);
  130. $inheritDescription = $inheritdoc->getText();
  131. $description = str_replace('{@inheritdoc}', $inheritDescription, $description);
  132. $phpdoc->setText($description);
  133. $this->normalizeParams($inheritdoc);
  134. $this->normalizeReturn($inheritdoc);
  135. //Add the tags that are inherited
  136. $inheritTags = $inheritdoc->getTags();
  137. if ($inheritTags) {
  138. /** @var Tag $tag */
  139. foreach ($inheritTags as $tag) {
  140. $tag->setDocBlock();
  141. $phpdoc->appendTag($tag);
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Normalize the parameters
  148. *
  149. * @param DocBlock $phpdoc
  150. */
  151. protected function normalizeParams(DocBlock $phpdoc)
  152. {
  153. //Get the return type and adjust them for beter autocomplete
  154. $paramTags = $phpdoc->getTagsByName('param');
  155. if ($paramTags) {
  156. /** @var ParamTag $tag */
  157. foreach($paramTags as $tag){
  158. // Convert the keywords
  159. $content = $this->convertKeywords($tag->getContent());
  160. $tag->setContent($content);
  161. // Get the expanded type and re-set the content
  162. $content = $tag->getType() . ' ' . $tag->getVariableName() . ' ' . $tag->getDescription();
  163. $tag->setContent(trim($content));
  164. }
  165. }
  166. }
  167. /**
  168. * Normalize the return tag (make full namespace, replace interfaces)
  169. *
  170. * @param DocBlock $phpdoc
  171. */
  172. protected function normalizeReturn(DocBlock $phpdoc)
  173. {
  174. //Get the return type and adjust them for beter autocomplete
  175. $returnTags = $phpdoc->getTagsByName('return');
  176. if ($returnTags) {
  177. /** @var ReturnTag $tag */
  178. $tag = reset($returnTags);
  179. // Get the expanded type
  180. $returnValue = $tag->getType();
  181. // Replace the interfaces
  182. foreach($this->interfaces as $interface => $real){
  183. $returnValue = str_replace($interface, $real, $returnValue);
  184. }
  185. // Set the changed content
  186. $tag->setContent($returnValue . ' ' . $tag->getDescription());
  187. $this->return = $returnValue;
  188. }else{
  189. $this->return = null;
  190. }
  191. }
  192. /**
  193. * Convert keywwords that are incorrect.
  194. *
  195. * @param string $string
  196. * @return string
  197. */
  198. protected function convertKeywords($string)
  199. {
  200. $string = str_replace('\Closure', 'Closure', $string);
  201. $string = str_replace('Closure', '\Closure', $string);
  202. $string = str_replace('dynamic', 'mixed', $string);
  203. return $string;
  204. }
  205. /**
  206. * Should the function return a value?
  207. *
  208. * @return bool
  209. */
  210. public function shouldReturn()
  211. {
  212. if($this->return !== "void" && $this->method->name !== "__construct"){
  213. return true;
  214. }
  215. return false;
  216. }
  217. /**
  218. * Get the parameters and format them correctly
  219. *
  220. * @param $method
  221. * @return array
  222. */
  223. public function getParameters($method)
  224. {
  225. //Loop through the default values for paremeters, and make the correct output string
  226. $params = array();
  227. $paramsWithDefault = array();
  228. foreach ($method->getParameters() as $param) {
  229. $paramStr = '$' . $param->getName();
  230. $params[] = $paramStr;
  231. if ($param->isOptional()) {
  232. $default = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null;
  233. if (is_bool($default)) {
  234. $default = $default ? 'true' : 'false';
  235. } elseif (is_array($default)) {
  236. $default = 'array()';
  237. } elseif (is_null($default)) {
  238. $default = 'null';
  239. } elseif (is_int($default)) {
  240. //$default = $default;
  241. } elseif (is_resource($default)) {
  242. //skip to not fail
  243. } else {
  244. $default = "'" . trim($default) . "'";
  245. }
  246. $paramStr .= " = $default";
  247. }
  248. $paramsWithDefault[] = $paramStr;
  249. }
  250. $this->params = $params;
  251. $this->params_with_default = $paramsWithDefault;
  252. }
  253. /**
  254. * @param \ReflectionMethod $reflectionMethod
  255. * @return DocBlock
  256. */
  257. protected function getInheritDoc($reflectionMethod)
  258. {
  259. $parentClass = $reflectionMethod->getDeclaringClass()->getParentClass();
  260. //Get either a parent or the interface
  261. if ($parentClass) {
  262. $method = $parentClass->getMethod($reflectionMethod->getName());
  263. } else {
  264. $method = $reflectionMethod->getPrototype();
  265. }
  266. if ($method) {
  267. $namespace = $method->getDeclaringClass()->getNamespaceName();
  268. $phpdoc = new DocBlock($method, new Context($namespace));
  269. if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) {
  270. //Not at the end yet, try another parent/interface..
  271. return $this->getInheritDoc($method);
  272. } else {
  273. return $phpdoc;
  274. }
  275. }
  276. }
  277. }