Pop\Code\Generator\DocblockGenerator::parse PHP Метод

parse() публичный статический Метод

Static method to parse a docblock string and return a new docblock generator object.
public static parse ( string $docblock, string $forceIndent = null ) : DocblockGenerator
$docblock string
$forceIndent string
Результат DocblockGenerator
    public static function parse($docblock, $forceIndent = null)
    {
        if (strpos($docblock, '/*') === false || strpos($docblock, '*/') === false) {
            throw new Exception('The docblock is not in the correct format.');
        }
        $desc = null;
        $formattedDesc = null;
        $indent = null;
        $tags = null;
        // Parse the description, if any
        if (strpos($docblock, '@') !== false) {
            $desc = substr($docblock, 0, strpos($docblock, '@'));
            $desc = str_replace('/*', '', $desc);
            $desc = str_replace('*/', '', $desc);
            $desc = str_replace(PHP_EOL . ' * ', ' ', $desc);
            $desc = trim(str_replace('*', '', $desc));
            $descAry = explode("\n", $desc);
            $formattedDesc = null;
            foreach ($descAry as $line) {
                $formattedDesc .= ' ' . trim($line);
            }
            $formattedDesc = trim($formattedDesc);
        }
        // Get the indentation, if any, and create docblock object
        $indent = null === $forceIndent ? substr($docblock, 0, strpos($docblock, '/')) : $forceIndent;
        $newDocblock = new self($formattedDesc, $indent);
        // Get the tags, if any
        if (strpos($docblock, '@') !== false) {
            $tags = substr($docblock, strpos($docblock, '@'));
            $tags = substr($tags, 0, strpos($tags, '*/'));
            $tags = str_replace('*', '', $tags);
            $tagsAry = explode("\n", $tags);
            foreach ($tagsAry as $key => $value) {
                $value = trim(str_replace('@', '', $value));
                // Param tags
                if (stripos($value, 'param') !== false) {
                    $paramtag = trim(str_replace('param', '', $value));
                    $paramtype = trim(substr($paramtag, 0, strpos($paramtag, ' ')));
                    $varname = null;
                    $paramdesc = null;
                    if (strpos($paramtag, ' ') !== false) {
                        $varname = trim(substr($paramtag, strpos($paramtag, ' ')));
                        if (strpos($varname, ' ') !== false) {
                            $paramdesc = trim(substr($varname, strpos($varname, ' ')));
                        }
                    } else {
                        $paramtype = $paramtag;
                    }
                    $newDocblock->setParam($paramtype, $varname, $paramdesc);
                    // Else, return tags
                } else {
                    if (stripos($value, 'return') !== false) {
                        $returntag = trim(str_replace('return', '', $value));
                        if (strpos($returntag, ' ') !== false) {
                            $returntype = substr($returntag, 0, strpos($returntag, ' '));
                            $returndesc = trim(str_replace($returntype, '', $returntag));
                        } else {
                            $returntype = $returntag;
                            $returndesc = null;
                        }
                        $newDocblock->setReturn($returntype, $returndesc);
                        // Else, all other tags
                    } else {
                        $tagname = trim(substr($value, 0, strpos($value, ' ')));
                        $tagdesc = trim(str_replace($tagname, '', $value));
                        if (!empty($tagname) && !empty($tagdesc)) {
                            $newDocblock->setTag($tagname, $tagdesc);
                        } else {
                            unset($tagsAry[$key]);
                        }
                    }
                }
            }
        }
        return $newDocblock;
    }

Usage Example

Пример #1
0
 public function testDocblockParse()
 {
     $this->setExpectedException('Pop\\Code\\Generator\\Exception');
     $d = DocblockGenerator::parse('Bad doc block');
     $docBlock = "/*\n * @param \$var\n * @return array\n */";
     $d = DocblockGenerator::parse($docBlock);
     $this->assertEquals('array', $d->getReturn());
 }
All Usage Examples Of Pop\Code\Generator\DocblockGenerator::parse