* A Foo class. * * @deprecated * * @version v1.1 * * @see Foo::bar() * @see google.com */ class Foo { /** * Does something that is really * cool and makes your life easy. * * @param string $name Your name * * @return string */ public function bar($name) { return "FooBar {$name}"; } } $reflect = new ReflectionClass('Foo'); $doc = new DocBlock($reflect); echo "## Comment ##\n"; echo $doc->getComment() . "\n\n"; echo "## Tags ##\n"; var_export($doc->getTags()); echo "\n";
public function testTagExists() { $doc = new DocBlock(new ReflectionClass('Number')); $this->assertTrue($doc->tagExists('see')); $this->assertTrue($doc->tagExists('version')); $this->assertFalse($doc->tagExists('deprecated')); $num = new Number(1); $refl = new ReflectionObject($num); // Method $doc = new DocBlock($refl->getMethod('value')); $this->assertTrue($doc->tagExists('return')); // Property $doc = new DocBlock($refl->getProperty('number')); $this->assertTrue($doc->tagExists('var')); // Trait-Imported Method $doc = new DocBlock($refl->getMethod('subtract')); $this->assertTrue($doc->tagExists('deprecated')); }
/** * @param \ReflectionMethod $method * * @return TypeInfoMember|null */ protected function methodToTypeInfoMember(\ReflectionMethod $method) { if (substr($method->name, 0, 2) === '__') { return; } $docb = new DocBlock($method); $hint = $docb->getComment() ?: ''; $link = $docb->getTag('link', '') ?: ''; if (is_array($link)) { $link = $link[0]; } if ($docb->tagExists('param')) { // detect return from docblock $return = explode(' ', $docb->getTag('return', 'void'), 2)[0]; } else { // detect return from reflection $return = $this->canInspectReflectionReturnType ? $method->getReturnType() : ''; } if ($docb->tagExists('param')) { // detect params from docblock $params = array_map([$this, 'parseDocBlockPropOrParam'], $docb->getTag('param', [], true)); } else { // detect params from reflection $params = array_map([$this, 'parseReflectedParams'], $method->getParameters()); } $signature = sprintf('<div class="cm-signature">' . '<span class="type">%s</span> <span class="name">%s</span>' . '(<span class="args">%s</span>)</span>' . '</div>', $return, $method->name, implode(', ', array_map(function ($param) { $result = '???'; if ($param) { $result = sprintf('<span class="%s" title="%s"><span class="type">%s</span>$%s</span>', $param['hint'] ? 'arg hint' : 'arg', $param['hint'], count($param['types']) ? implode('|', $param['types']) . ' ' : '', $param['name']); } return $result; }, $params))); return new TypeInfoMember($method->name, ['method'], $signature . $hint, $link); }