You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

113 lines
3.0 KiB

  1. <?php
  2. namespace dokuwiki\Remote\OpenApiDoc;
  3. use ReflectionFunction;
  4. use ReflectionMethod;
  5. class DocBlockMethod extends DocBlock
  6. {
  7. /**
  8. * Parse the given docblock
  9. *
  10. * The docblock can be of a method, class or property.
  11. *
  12. * @param ReflectionMethod|ReflectionFunction $reflector
  13. */
  14. public function __construct($reflector)
  15. {
  16. parent::__construct($reflector);
  17. $this->refineParam();
  18. $this->refineReturn();
  19. }
  20. /** @inheritdoc */
  21. protected function getContext()
  22. {
  23. if ($this->reflector instanceof ReflectionFunction) {
  24. return null;
  25. }
  26. return parent::getContext();
  27. }
  28. /**
  29. * Convenience method to access the method parameters
  30. *
  31. * @return array
  32. */
  33. public function getParameters()
  34. {
  35. return $this->getTag('param');
  36. }
  37. /**
  38. * Convenience method to access the method return
  39. *
  40. * @return array
  41. */
  42. public function getReturn()
  43. {
  44. return $this->getTag('return');
  45. }
  46. /**
  47. * Parse the param tag into its components
  48. *
  49. * @return void
  50. */
  51. protected function refineParam()
  52. {
  53. $result = [];
  54. // prefill from reflection
  55. foreach ($this->reflector->getParameters() as $parameter) {
  56. $refType = $parameter->getType();
  57. $result[$parameter->getName()] = [
  58. 'type' => new Type($refType ? $refType->getName() : 'string', $this->getContext()),
  59. 'optional' => $parameter->isOptional(),
  60. 'description' => '',
  61. ];
  62. if ($parameter->isDefaultValueAvailable()) {
  63. $result[$parameter->getName()]['default'] = $parameter->getDefaultValue();
  64. }
  65. }
  66. // refine from doc tags
  67. foreach ($this->tags['param'] ?? [] as $param) {
  68. [$type, $name, $description] = array_map('trim', sexplode(' ', $param, 3, ''));
  69. if ($name === '' || $name[0] !== '$') continue;
  70. $name = substr($name, 1);
  71. if (!isset($result[$name])) continue; // reflection says this param does not exist
  72. $result[$name]['type'] = new Type($type, $this->getContext());
  73. $result[$name]['description'] = $description;
  74. }
  75. $this->tags['param'] = $result;
  76. }
  77. /**
  78. * Parse the return tag into its components
  79. *
  80. * @return void
  81. */
  82. protected function refineReturn()
  83. {
  84. // prefill from reflection
  85. $refType = $this->reflector->getReturnType();
  86. $result = [
  87. 'type' => new Type($refType ? $refType->getName() : 'void', $this->getContext()),
  88. 'description' => '',
  89. ];
  90. // refine from doc tag
  91. foreach ($this->tags['return'] ?? [] as $return) {
  92. [$type, $description] = array_map('trim', sexplode(' ', $return, 2, ''));
  93. $result['type'] = new Type($type, $this->getContext());
  94. $result['description'] = $description;
  95. }
  96. $this->tags['return'] = $result;
  97. }
  98. }