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