/** * Factory method responsible for instantiating the correct sub type. * * @param string $tag_line The text for this tag, including description. * @param DocBlock $docblock The DocBlock which this tag belongs to. * @param Location $location Location of the tag. * * @throws \InvalidArgumentException if an invalid tag line was presented. * * @return static A new tag object. */ public static final function createInstance($tag_line, DocBlock $docblock = null, Location $location = null) { if (!preg_match('/^@(' . self::REGEX_TAGNAME . ')(?:\\s*([^\\s].*)|$)?/us', $tag_line, $matches)) { throw new \InvalidArgumentException('Invalid tag_line detected: ' . $tag_line); } $handler = __CLASS__; if (isset(self::$tagHandlerMappings[$matches[1]])) { $handler = self::$tagHandlerMappings[$matches[1]]; } elseif (isset($docblock)) { $tagName = (string) new Type\Collection(array($matches[1]), $docblock->getContext()); if (isset(self::$tagHandlerMappings[$tagName])) { $handler = self::$tagHandlerMappings[$tagName]; } } return new $handler($matches[1], isset($matches[2]) ? $matches[2] : '', $docblock, $location); }
/** * @param string $class * @return string */ protected function createPhpDocs($class) { $reflection = new \ReflectionClass($class); $namespace = $reflection->getNamespaceName(); $classname = $reflection->getShortName(); $originalDoc = $reflection->getDocComment(); if ($this->reset) { $phpdoc = new DocBlock('', new Context($namespace)); } else { $phpdoc = new DocBlock($reflection, new Context($namespace)); } if (!$phpdoc->getText()) { $phpdoc->setText($class); } $properties = array(); $methods = array(); foreach ($phpdoc->getTags() as $tag) { $name = $tag->getName(); if ($name == "property" || $name == "property-read" || $name == "property-write") { $properties[] = $tag->getVariableName(); } elseif ($name == "method") { $methods[] = $tag->getMethodName(); } } foreach ($this->properties as $name => $property) { $name = "\${$name}"; if (in_array($name, $properties)) { continue; } if ($property['read'] && $property['write']) { $attr = 'property'; } elseif ($property['write']) { $attr = 'property-write'; } else { $attr = 'property-read'; } if ($this->hasCamelCaseModelProperties()) { $name = Str::camel($name); } $tagLine = trim("@{$attr} {$property['type']} {$name} {$property['comment']}"); $tag = Tag::createInstance($tagLine, $phpdoc); $phpdoc->appendTag($tag); } foreach ($this->methods as $name => $method) { if (in_array($name, $methods)) { continue; } $arguments = implode(', ', $method['arguments']); $tag = Tag::createInstance("@method static {$method['type']} {$name}({$arguments})", $phpdoc); $phpdoc->appendTag($tag); } if ($this->write && !$phpdoc->getTagsByName('mixin')) { $phpdoc->appendTag(Tag::createInstance("@mixin \\Eloquent", $phpdoc)); } $serializer = new DocBlockSerializer(); $serializer->getDocComment($phpdoc); $docComment = $serializer->getDocComment($phpdoc); if ($this->write) { $filename = $reflection->getFileName(); $contents = $this->files->get($filename); if ($originalDoc) { $contents = str_replace($originalDoc, $docComment, $contents); } else { $needle = "class {$classname}"; $replace = "{$docComment}\nclass {$classname}"; $pos = strpos($contents, $needle); if ($pos !== false) { $contents = substr_replace($contents, $replace, $pos, strlen($needle)); } } if ($this->files->put($filename, $contents)) { $this->info('Written new phpDocBlock to ' . $filename); } } $output = "namespace {$namespace}{\n{$docComment}\n\tclass {$classname} extends \\Eloquent {}\n}\n\n"; return $output; }
/** * @depends testConstructWithTagsOnly * @covers \Barryvdh\Reflection\DocBlock::getTagsByName * * @return void */ public function testGetTagsByNameMultipleMatch() { $fixture = <<<DOCBLOCK /** * @param string * @param int * @return void */ DOCBLOCK; $object = new DocBlock($fixture); $this->assertEmpty($object->getTagsByName('category')); $this->assertCount(1, $object->getTagsByName('return')); $this->assertCount(2, $object->getTagsByName('param')); }
/** * Generate a DocBlock comment. * * @param DocBlock The DocBlock to serialize. * * @return string The serialized doc block. */ public function getDocComment(DocBlock $docblock) { $indent = str_repeat($this->indentString, $this->indent); $firstIndent = $this->isFirstLineIndented ? $indent : ''; $text = $docblock->getText(); if ($this->lineLength) { //3 === strlen(' * ') $wrapLength = $this->lineLength - strlen($indent) - 3; $text = wordwrap($text, $wrapLength); } $text = str_replace("\n", "\n{$indent} * ", $text); $comment = "{$firstIndent}/**\n{$indent} * {$text}\n{$indent} *\n"; /** @var Tag $tag */ foreach ($docblock->getTags() as $tag) { $tagText = (string) $tag; if ($this->lineLength) { $tagText = wordwrap($tagText, $wrapLength); } $tagText = str_replace("\n", "\n{$indent} * ", $tagText); $comment .= "{$indent} * {$tagText}\n"; } $comment .= $indent . ' */'; return $comment; }
/** * @param \ReflectionMethod $reflectionMethod * @return DocBlock */ protected function getInheritDoc($reflectionMethod) { $parentClass = $reflectionMethod->getDeclaringClass()->getParentClass(); //Get either a parent or the interface if ($parentClass) { $method = $parentClass->getMethod($reflectionMethod->getName()); } else { $method = $reflectionMethod->getPrototype(); } if ($method) { $namespace = $method->getDeclaringClass()->getNamespaceName(); $phpdoc = new DocBlock($method, new Context($namespace)); if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) { //Not at the end yet, try another parent/interface.. return $this->getInheritDoc($method); } else { return $phpdoc; } } }
protected function detectMethodsFromDoc(ReflectionClass $reflection) { $docBlock = new DocBlock($reflection); $methods = $docBlock->getTagsByName('method'); foreach ($methods as $method) { $this->methods[] = new MethodTag($method, $this->alias, $reflection, $method->getMethodName(), $this->interfaces); } }
/** * Get method return type based on it DocBlock comment * * @param \ReflectionMethod $reflection * * @return null|string */ protected function getReturnTypeFromDocBlock(\ReflectionMethod $reflection) { $type = null; $phpdoc = new DocBlock($reflection); if ($phpdoc->hasTag('return')) { $type = $phpdoc->getTagsByName('return')[0]->getContent(); } return $type; }