Skip to content

Commit

Permalink
chore(documentator): Removed phpdocumentor/reflection-docblock, the…
Browse files Browse the repository at this point in the history
… `phpstan/phpdoc-parser` will be used instead.
  • Loading branch information
LastDragon-ru committed Jan 16, 2024
1 parent 76f629c commit 6faba1b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"league/commonmark": "^2.4",
"nuwave/lighthouse": "^6.5.0",
"opis/json-schema": "^2.3.0",
"phpdocumentor/reflection-docblock": "^5.2",
"phpdocumentor/type-resolver": "^1.7",
"phpstan/phpdoc-parser": "^1.25",
"psr/http-message": "^1.0.0|^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/documentator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"laravel/framework": "^10.0.0",
"league/commonmark": "^2.4",
"nikic/php-parser": "^4.18|^5.0",
"phpdocumentor/reflection-docblock": "^5.2",
"phpstan/phpdoc-parser": "^1.25",
"symfony/filesystem": "^6.3.0",
"symfony/finder": "^6.3.0",
"symfony/polyfill-php83": "^1.28",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@
use LastDragon_ru\LaraASP\Documentator\Utils\Path;
use LastDragon_ru\LaraASP\Serializer\Contracts\Serializable;
use Override;
use phpDocumentor\Reflection\DocBlockFactory;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\NodeFinder;
use PhpParser\ParserFactory;

use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode as PhpDocBlockNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;

use function array_slice;
use function dirname;
use function file_get_contents;
use function implode;
use function trim;

/**
Expand Down Expand Up @@ -76,45 +84,34 @@ public function process(string $path, string $target, Serializable $parameters):
}

// Class?
try {
$class = null;
$parser = (new ParserFactory())->createForNewestSupportedVersion();
$stmts = (array) $parser->parse($content);
$finder = new NodeFinder();
$class = $finder->findFirst($stmts, static function (Node $node): bool {
return $node instanceof ClassLike;
});
} catch (Exception $exception) {
throw new TargetIsNotValidPhpFile($path, $target, $exception);
}
$class = $this->getClass($content, $path, $target);

if (!($class instanceof ClassLike)) {
if (!$class) {
return '';
}

// DocBlock?
$comment = $class->getDocComment()?->getText();
$node = $this->getDocNode($class);

if (!$comment) {
if (!$node) {
return '';
}

// Parse
$eol = "\n";
$result = '';
$factory = DocBlockFactory::createInstance();
$docblock = $factory->create($comment);
$eol = "\n";
$text = $this->getDocText($node);
$result = '';

if ($parameters->summary) {
$summary = trim($docblock->getSummary());
$summary = trim(implode($eol.$eol, array_slice($text, 0, 1)));

if ($summary) {
$result .= $summary.$eol.$eol;
}
}

if ($parameters->description) {
$description = trim((string) $docblock->getDescription());
$description = trim(implode($eol.$eol, array_slice($text, 1)));

if ($description) {
$result .= $description.$eol.$eol;
Expand All @@ -128,4 +125,61 @@ public function process(string $path, string $target, Serializable $parameters):
// Return
return $result;
}

private function getClass(string $content, string $path, string $target): ?ClassLike {
try {
$class = null;
$parser = (new ParserFactory())->createForNewestSupportedVersion();
$stmts = (array) $parser->parse($content);
$finder = new NodeFinder();
$class = $finder->findFirst($stmts, static function (Node $node): bool {
return $node instanceof ClassLike;
});
} catch (Exception $exception) {
throw new TargetIsNotValidPhpFile($path, $target, $exception);
}

if (!($class instanceof ClassLike)) {
$class = null;
}

return $class;
}

private function getDocNode(ClassLike $class): ?PhpDocBlockNode {
// Comment?
$comment = $class->getDocComment();

if (!$comment || trim($comment->getText()) === '') {
return null;
}

// Parse
$lexer = new Lexer();
$parser = new PhpDocParser(new TypeParser(new ConstExprParser()), new ConstExprParser());
$tokens = new TokenIterator($lexer->tokenize($comment->getText()));
$node = $parser->parse($tokens);

// Return
return $node;
}

/**
* @return list<string>
*/
private function getDocText(PhpDocBlockNode $node): array {
$nodes = [];

foreach ($node->children as $child) {
if ($child instanceof PhpDocTextNode) {
if (trim($child->text) !== '') {
$nodes[] = $child->text;
}
} else {
break;
}
}

return $nodes;
}
}

0 comments on commit 6faba1b

Please sign in to comment.