mirror of
https://github.com/danog/phpdoc.git
synced 2024-11-26 12:04:47 +01:00
Add some basic @see logic
This commit is contained in:
parent
5e7cd94ec1
commit
e3c13d54aa
@ -394,7 +394,7 @@ class PhpDoc
|
||||
return "?".$this->resolveTypeAlias($fromClass, \substr($name, 1), $resolved);
|
||||
}
|
||||
$res = $this->useMap[$fromClass][$name] ?? $name;
|
||||
if ($res[0] !== '\\' && !self::isScalar($res)) {
|
||||
if ($res[0] !== '\\' && !self::isScalar($res) && \str_contains($res, '\\')) {
|
||||
$res = "\\$res";
|
||||
}
|
||||
$resolved []= $res;
|
||||
|
@ -35,7 +35,7 @@ abstract class GenericDoc
|
||||
/**
|
||||
* See also array.
|
||||
*
|
||||
* @var array<string, GenericTagValueNode>
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected array $seeAlso = [];
|
||||
/**
|
||||
@ -95,7 +95,7 @@ abstract class GenericDoc
|
||||
if ($tag->name === '@see') {
|
||||
$tag = $tag->value;
|
||||
\assert($tag instanceof GenericTagValueNode);
|
||||
$this->seeAlso[$tag->value] = $tag;
|
||||
$this->seeAlso[$tag->value] = $tag->value;
|
||||
}
|
||||
}
|
||||
$this->authors = \array_unique($this->authors);
|
||||
@ -111,8 +111,36 @@ abstract class GenericDoc
|
||||
$namespace = \explode('\\', $namespace);
|
||||
|
||||
$seeAlso = '';
|
||||
foreach ($this->seeAlso as $see) {
|
||||
$seeAlso .= "* ".$see->value."\n";
|
||||
$empty = [];
|
||||
foreach ($this->seeAlso as $ref) {
|
||||
if (\str_starts_with(\trim($ref), 'http')) {
|
||||
[$url, $desc] = \explode(' ', $ref.' ');
|
||||
$desc = $desc ?: $url;
|
||||
$seeAlso .= "* [$desc]($url)\n";
|
||||
} else {
|
||||
[$type, $desc] = \explode(' ', $ref.' ');
|
||||
$ref = $this->builder->resolveTypeAlias($this->className, $type, $empty);
|
||||
|
||||
$to = \explode("\\", $ref.".md");
|
||||
if (\count($to) === 2 || !$this->builder->hasClass($ref)) {
|
||||
$seeAlso .= "* `$ref`\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
\array_shift($to);
|
||||
\array_unshift($to, ...\array_fill(0, \count($namespace), '..'));
|
||||
$relPath = $to;
|
||||
$path = \implode('/', $relPath);
|
||||
|
||||
if (!$desc) {
|
||||
if ($desc = $this->builder->getTitle($ref)) {
|
||||
$desc = "`$ref`: $desc";
|
||||
} else {
|
||||
$desc = $ref;
|
||||
}
|
||||
}
|
||||
$seeAlso .= "* [$desc]($path)\n";
|
||||
}
|
||||
}
|
||||
if ($seeAlso) {
|
||||
$seeAlso = "\n#### See also: \n$seeAlso\n\n";
|
||||
@ -177,8 +205,11 @@ abstract class GenericDoc
|
||||
if (\str_contains($type, ' ')) {
|
||||
continue;
|
||||
}
|
||||
if (\is_numeric($type) || $type === 'T') {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$this->seeAlso[$type] = new GenericTagValueNode($type);
|
||||
$this->seeAlso[$type] = $type;
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@
|
||||
namespace danog\PhpDoc\PhpDoc;
|
||||
|
||||
use danog\PhpDoc\PhpDoc;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
|
||||
use ReflectionFunctionAbstract;
|
||||
use ReflectionMethod;
|
||||
|
||||
@ -14,7 +13,8 @@ use ReflectionMethod;
|
||||
*/
|
||||
class MethodDoc extends GenericDoc
|
||||
{
|
||||
private ReturnTagValueNode $return;
|
||||
private string $return;
|
||||
private string $returnDescription;
|
||||
private array $params = [];
|
||||
/**
|
||||
* Constructor.
|
||||
@ -35,7 +35,6 @@ class MethodDoc extends GenericDoc
|
||||
$optional = [];
|
||||
$params = [];
|
||||
|
||||
$docReflection = "/**\n";
|
||||
foreach ($method->getParameters() as $param) {
|
||||
$order []= '$'.$param->getName();
|
||||
$opt = $param->isOptional() && !$param->isVariadic();
|
||||
@ -72,10 +71,15 @@ class MethodDoc extends GenericDoc
|
||||
if ($this->name !== '__construct') {
|
||||
foreach (['@return', '@psalm-return', '@phpstan-return'] as $t) {
|
||||
foreach ($doc->getReturnTagValues($t) as $tag) {
|
||||
$this->return = $tag;
|
||||
$this->return = (string) $tag->type;
|
||||
$this->returnDescription = $tag->description;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isset($this->return) && $ret = $method->getReturnType()) {
|
||||
$this->return = (string) $ret;
|
||||
$this->returnDescription = '';
|
||||
}
|
||||
|
||||
foreach ($order as $param) {
|
||||
$this->params[$param] = $params[$param];
|
||||
@ -112,7 +116,7 @@ class MethodDoc extends GenericDoc
|
||||
$sig .= ')';
|
||||
if (isset($this->return)) {
|
||||
$sig .= ': ';
|
||||
$sig .= $this->resolveTypeAlias((string) $this->return->type);
|
||||
$sig .= $this->resolveTypeAlias($this->return);
|
||||
}
|
||||
return $sig;
|
||||
}
|
||||
@ -162,8 +166,8 @@ class MethodDoc extends GenericDoc
|
||||
}
|
||||
$sig .= "\n";
|
||||
}
|
||||
if (isset($this->return) && $this->return->description) {
|
||||
$sig .= "\nReturn value: ".$this->return->description."\n";
|
||||
if (isset($this->returnDescription) && $this->returnDescription) {
|
||||
$sig .= "\nReturn value: ".$this->returnDescription."\n";
|
||||
}
|
||||
$sig .= $this->seeAlso($namespace ?? $this->namespace);
|
||||
$sig .= "\n";
|
||||
|
Loading…
Reference in New Issue
Block a user