1
0
mirror of https://github.com/danog/phpdoc.git synced 2024-11-26 20:14:51 +01:00

Add some basic @see logic

This commit is contained in:
Daniil Gentili 2024-04-06 18:37:53 +02:00
parent 5e7cd94ec1
commit e3c13d54aa
3 changed files with 48 additions and 13 deletions

View File

@ -394,7 +394,7 @@ class PhpDoc
return "?".$this->resolveTypeAlias($fromClass, \substr($name, 1), $resolved); return "?".$this->resolveTypeAlias($fromClass, \substr($name, 1), $resolved);
} }
$res = $this->useMap[$fromClass][$name] ?? $name; $res = $this->useMap[$fromClass][$name] ?? $name;
if ($res[0] !== '\\' && !self::isScalar($res)) { if ($res[0] !== '\\' && !self::isScalar($res) && \str_contains($res, '\\')) {
$res = "\\$res"; $res = "\\$res";
} }
$resolved []= $res; $resolved []= $res;

View File

@ -35,7 +35,7 @@ abstract class GenericDoc
/** /**
* See also array. * See also array.
* *
* @var array<string, GenericTagValueNode> * @var array<string, string>
*/ */
protected array $seeAlso = []; protected array $seeAlso = [];
/** /**
@ -95,7 +95,7 @@ abstract class GenericDoc
if ($tag->name === '@see') { if ($tag->name === '@see') {
$tag = $tag->value; $tag = $tag->value;
\assert($tag instanceof GenericTagValueNode); \assert($tag instanceof GenericTagValueNode);
$this->seeAlso[$tag->value] = $tag; $this->seeAlso[$tag->value] = $tag->value;
} }
} }
$this->authors = \array_unique($this->authors); $this->authors = \array_unique($this->authors);
@ -111,8 +111,36 @@ abstract class GenericDoc
$namespace = \explode('\\', $namespace); $namespace = \explode('\\', $namespace);
$seeAlso = ''; $seeAlso = '';
foreach ($this->seeAlso as $see) { $empty = [];
$seeAlso .= "* ".$see->value."\n"; 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) { if ($seeAlso) {
$seeAlso = "\n#### See also: \n$seeAlso\n\n"; $seeAlso = "\n#### See also: \n$seeAlso\n\n";
@ -177,8 +205,11 @@ abstract class GenericDoc
if (\str_contains($type, ' ')) { if (\str_contains($type, ' ')) {
continue; continue;
} }
if (\is_numeric($type) || $type === 'T') {
continue;
}
try { try {
$this->seeAlso[$type] = new GenericTagValueNode($type); $this->seeAlso[$type] = $type;
} catch (\Throwable $e) { } catch (\Throwable $e) {
} }
} }

View File

@ -3,7 +3,6 @@
namespace danog\PhpDoc\PhpDoc; namespace danog\PhpDoc\PhpDoc;
use danog\PhpDoc\PhpDoc; use danog\PhpDoc\PhpDoc;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use ReflectionFunctionAbstract; use ReflectionFunctionAbstract;
use ReflectionMethod; use ReflectionMethod;
@ -14,7 +13,8 @@ use ReflectionMethod;
*/ */
class MethodDoc extends GenericDoc class MethodDoc extends GenericDoc
{ {
private ReturnTagValueNode $return; private string $return;
private string $returnDescription;
private array $params = []; private array $params = [];
/** /**
* Constructor. * Constructor.
@ -35,7 +35,6 @@ class MethodDoc extends GenericDoc
$optional = []; $optional = [];
$params = []; $params = [];
$docReflection = "/**\n";
foreach ($method->getParameters() as $param) { foreach ($method->getParameters() as $param) {
$order []= '$'.$param->getName(); $order []= '$'.$param->getName();
$opt = $param->isOptional() && !$param->isVariadic(); $opt = $param->isOptional() && !$param->isVariadic();
@ -72,10 +71,15 @@ class MethodDoc extends GenericDoc
if ($this->name !== '__construct') { if ($this->name !== '__construct') {
foreach (['@return', '@psalm-return', '@phpstan-return'] as $t) { foreach (['@return', '@psalm-return', '@phpstan-return'] as $t) {
foreach ($doc->getReturnTagValues($t) as $tag) { 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) { foreach ($order as $param) {
$this->params[$param] = $params[$param]; $this->params[$param] = $params[$param];
@ -112,7 +116,7 @@ class MethodDoc extends GenericDoc
$sig .= ')'; $sig .= ')';
if (isset($this->return)) { if (isset($this->return)) {
$sig .= ': '; $sig .= ': ';
$sig .= $this->resolveTypeAlias((string) $this->return->type); $sig .= $this->resolveTypeAlias($this->return);
} }
return $sig; return $sig;
} }
@ -162,8 +166,8 @@ class MethodDoc extends GenericDoc
} }
$sig .= "\n"; $sig .= "\n";
} }
if (isset($this->return) && $this->return->description) { if (isset($this->returnDescription) && $this->returnDescription) {
$sig .= "\nReturn value: ".$this->return->description."\n"; $sig .= "\nReturn value: ".$this->returnDescription."\n";
} }
$sig .= $this->seeAlso($namespace ?? $this->namespace); $sig .= $this->seeAlso($namespace ?? $this->namespace);
$sig .= "\n"; $sig .= "\n";