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