/** * @return array */ private function createRulesFromPhpdoc() { $rules = []; // process phpdoc form $tags = $this->docBlock->getTags('form'); if ($tags) { $formClass = $tags[0]->getContent(); $rules = (new $formClass())->rules(); } // process phpdoc params $tags = $this->docBlock->getTags('param'); foreach ($tags as $tag) { $rules[] = [trim($tag->getVariableName(), '$'), $tag->getTypes()[0], 'query']; } return $rules; }
/** * fromReflection() - Build a docblock generator object from a reflection object * * @param ReflectionDocblock $reflectionDocblock * @return DocblockGenerator */ public static function fromReflection(DocBlockReflection $reflectionDocblock) { $docblock = new self(); $docblock->setSourceContent($reflectionDocblock->getContents()); $docblock->setSourceDirty(false); $docblock->setShortDescription($reflectionDocblock->getShortDescription()); $docblock->setLongDescription($reflectionDocblock->getLongDescription()); foreach ($reflectionDocblock->getTags() as $tag) { $docblock->setTag(Docblock\Tag::fromReflection($tag)); } return $docblock; }
/** * Build a DocBlock generator object from a reflection object * * @param DocBlockReflection $reflectionDocBlock * @return DocBlockGenerator */ public static function fromReflection(DocBlockReflection $reflectionDocBlock) { $docBlock = new static(); $docBlock->setSourceContent($reflectionDocBlock->getContents()); $docBlock->setSourceDirty(false); $docBlock->setShortDescription($reflectionDocBlock->getShortDescription()); $docBlock->setLongDescription($reflectionDocBlock->getLongDescription()); foreach ($reflectionDocBlock->getTags() as $tag) { $docBlock->setTag(self::getTagManager()->createTagFromReflection($tag)); } return $docBlock; }
/** * Use code reflection to create method signatures * * Determines the method help/description text from the function DocBlock * comment. Determines method signatures using a combination of * ReflectionFunction and parsing of DocBlock @param and @return values. * * @throws Exception\RuntimeException * @return array */ protected function reflect() { $function = $this->reflection; $paramCount = $function->getNumberOfParameters(); $parameters = $function->getParameters(); $scanner = new DocBlockReflection($function->getDocComment() ?: '/***/'); $helpText = $scanner->getLongDescription(); /* @var \Zend\Code\Reflection\DocBlock\Tag\ParamTag[] $paramTags */ $paramTags = $scanner->getTags('param'); /* @var \Zend\Code\Reflection\DocBlock\Tag\ReturnTag $returnTag */ $returnTag = $scanner->getTag('return'); if (empty($helpText)) { $helpText = $scanner->getShortDescription(); if (empty($helpText)) { $helpText = $function->getName(); } } $this->setDescription($helpText); if ($returnTag) { $return = array(); $returnDesc = $returnTag->getDescription(); foreach ($returnTag->getTypes() as $type) { $return[] = $type; } } else { $return = array('void'); $returnDesc = ''; } $paramTypesTmp = array(); $paramDesc = array(); if (empty($paramTags)) { foreach ($parameters as $param) { $paramTypesTmp[] = array($param->isArray() ? 'array' : 'mixed'); $paramDesc[] = ''; } } else { $paramDesc = array(); foreach ($paramTags as $paramTag) { $paramTypesTmp[] = $paramTag->getTypes(); $paramDesc[] = $paramTag->getDescription() ?: ''; } } // Get all param types as arrays $nParamTypesTmp = count($paramTypesTmp); if ($nParamTypesTmp < $paramCount) { $start = $paramCount - $nParamTypesTmp; for ($i = $start; $i < $paramCount; ++$i) { $paramTypesTmp[$i] = array('mixed'); $paramDesc[$i] = ''; } } elseif ($nParamTypesTmp != $paramCount) { throw new Exception\RuntimeException('Variable number of arguments is not supported for services (except optional parameters). ' . 'Number of function arguments must correspond to actual number of arguments described in a docblock.'); } $paramTypes = array(); foreach ($paramTypesTmp as $i => $param) { if ($parameters[$i]->isOptional()) { array_unshift($param, null); } $paramTypes[] = $param; } $this->buildSignatures($return, $returnDesc, $paramTypes, $paramDesc); }
/** * Extracts the DocBlock (in parts) from the source of the stored routine. */ private function getDocBlockPartsSource() { // Get the DocBlock for the source. $tmp = PHP_EOL; foreach ($this->routineSourceCodeLines as $line) { $n = preg_match('/create\\s+(procedure|function)\\s+([a-zA-Z0-9_]+)/i', $line); if ($n) { break; } $tmp .= $line; $tmp .= PHP_EOL; } $phpdoc = new DocBlockReflection($tmp); // Get the short description. $this->docBlockPartsSource['sort_description'] = $phpdoc->getShortDescription(); // Get the long description. $this->docBlockPartsSource['long_description'] = $phpdoc->getLongDescription(); // Get the description for each parameter of the stored routine. foreach ($phpdoc->getTags() as $key => $tag) { if ($tag->getName() == 'param') { /* @var $tag ParamTag */ $this->docBlockPartsSource['parameters'][$key] = ['name' => $tag->getTypes()[0], 'description' => $tag->getDescription()]; } } }
/** * @param \Zend\Code\Reflection\DocBlockReflection|false $docBlock * @return \Praxigento\Core\Reflection\Data\Method */ public function _processMethodDocBlock($docBlock) { /** @var \Praxigento\Core\Reflection\Data\Method $result */ $result = $this->_manObj->create(\Praxigento\Core\Reflection\Data\Method::class); if ($docBlock) { $returnAnnotations = $docBlock->getTags('return'); if (!empty($returnAnnotations)) { /** @var \Zend\Code\Reflection\DocBlock\Tag\ReturnTag $returnTag */ $returnTag = current($returnAnnotations); $types = $returnTag->getTypes(); $returnType = current($types); $nullable = in_array('null', $types); $desc = $returnTag->getDescription(); $result->setType($returnType); $result->setIsRequired(!$nullable); $result->setDescription($desc); } } return $result; }
public function testFunctionDocBlockTags() { $docblock = ' /** * Method ShortDescription * * @param int $one Description for one * @param int[] Description for two * @param string|null $three Description for three * which spans multiple lines * @return int[]|null Description * @throws Exception */ '; $docblockReflection = new DocBlockReflection($docblock); $paramTags = $docblockReflection->getTags('param'); $this->assertEquals(5, count($docblockReflection->getTags())); $this->assertEquals(3, count($paramTags)); $this->assertEquals(1, count($docblockReflection->getTags('return'))); $this->assertEquals(1, count($docblockReflection->getTags('throws'))); $returnTag = $docblockReflection->getTag('return'); $this->assertInstanceOf('Zend\\Code\\Reflection\\DocBlock\\Tag\\ReturnTag', $returnTag); $this->assertEquals('int[]', $returnTag->getType()); $this->assertEquals(array('int[]', 'null'), $returnTag->getTypes()); $this->assertEquals('Description', $returnTag->getDescription()); $throwsTag = $docblockReflection->getTag('throws'); $this->assertInstanceOf('Zend\\Code\\Reflection\\DocBlock\\Tag\\ThrowsTag', $throwsTag); $this->assertEquals('Exception', $throwsTag->getType()); $paramTag = $paramTags[0]; $this->assertInstanceOf('Zend\\Code\\Reflection\\DocBlock\\Tag\\ParamTag', $paramTag); $this->assertEquals('int', $paramTag->getType()); $paramTag = $paramTags[1]; $this->assertInstanceOf('Zend\\Code\\Reflection\\DocBlock\\Tag\\ParamTag', $paramTag); $this->assertEquals('int[]', $paramTag->getType()); $paramTag = $paramTags[2]; $this->assertInstanceOf('Zend\\Code\\Reflection\\DocBlock\\Tag\\ParamTag', $paramTag); $this->assertEquals('string', $paramTag->getType()); $this->assertEquals(array('string', 'null'), $paramTag->getTypes()); }