MadelineProto/src/danog/MadelineProto/PhpDoc/ClassDoc.php

138 lines
4.5 KiB
PHP
Raw Normal View History

2020-10-08 21:35:32 +02:00
<?php
namespace danog\MadelineProto\PhpDoc;
use danog\MadelineProto\Logger;
use danog\MadelineProto\PhpDocBuilder;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use phpDocumentor\Reflection\DocBlock\Tags\Property;
use ReflectionClass;
use ReflectionClassConstant;
use ReflectionMethod;
class ClassDoc extends GenericDoc
{
/**
* Properties.
*
2020-10-13 21:54:25 +02:00
* @var array<string, array>
2020-10-08 21:35:32 +02:00
*/
private array $properties = [];
/**
* Methods.
*
* @var array<string, MethodDoc>
*/
private array $methods = [];
2020-10-13 21:54:25 +02:00
/**
* Constants.
*/
private array $constants = [];
2020-10-13 10:03:35 +02:00
public function __construct(PhpDocBuilder $builder, ReflectionClass $reflectionClass)
2020-10-08 21:35:32 +02:00
{
2020-10-13 10:03:35 +02:00
$this->builder = $builder;
$this->name = $reflectionClass->getName();
2020-10-08 21:35:32 +02:00
$doc = $reflectionClass->getDocComment();
if (!$doc) {
Logger::log($reflectionClass->getName()." has no PHPDOC");
$this->ignore = true;
return;
}
2020-10-13 10:03:35 +02:00
$doc = $this->builder->getFactory()->create($doc);
2020-10-08 21:35:32 +02:00
2020-10-13 10:03:35 +02:00
parent::__construct($doc, $reflectionClass);
2020-10-08 21:35:32 +02:00
$tags = $doc->getTags();
foreach ($tags as $tag) {
if ($tag instanceof Property) {
2020-10-13 21:54:25 +02:00
$this->properties[$tag->getVariableName()] = [
2020-10-08 21:35:32 +02:00
$tag->getType(),
$tag->getDescription()
2020-10-13 21:54:25 +02:00
];
2020-10-08 21:35:32 +02:00
}
if ($tag instanceof InvalidTag && $tag->getName() === 'property') {
[$type, $description] = \explode(" $", $tag->render(), 2);
$description .= ' ';
[$varName, $description] = \explode(" ", $description, 2);
$type = \str_replace('@property ', '', $type);
$description ??= '';
2020-10-13 21:54:25 +02:00
$this->properties[$varName] = [
2020-10-08 21:35:32 +02:00
$type,
$description
2020-10-13 21:54:25 +02:00
];
2020-10-08 21:35:32 +02:00
}
}
foreach ($reflectionClass->getConstants() as $key => $value) {
$refl = new ReflectionClassConstant($reflectionClass->getName(), $key);
if (!$refl->isPublic()) {
continue;
}
$description = '';
if ($refl->getDocComment()) {
2020-10-13 10:03:35 +02:00
$docConst = $this->builder->getFactory()->create($refl->getDocComment());
if ($this->builder->shouldIgnore($refl->getDeclaringClass()->getName())) {
2020-10-08 21:35:32 +02:00
continue;
}
$description .= $docConst->getSummary();
if ($docConst->getDescription()) {
$description .= "\n\n";
$description .= $docConst->getDescription();
}
if ($docConst->getTagsByName('internal')) {
continue;
}
}
$description = \trim($description);
2020-10-13 21:54:25 +02:00
$this->constants[$key] = [
2020-10-08 21:35:32 +02:00
$value,
$description
];
}
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
2020-10-13 10:03:35 +02:00
if (str_starts_with($method->getName(), '__') && $method !== '__construct') {
continue;
}
$this->methods[$method->getName()] = new MethodDoc($this->builder, $method);
2020-10-08 21:35:32 +02:00
}
2020-10-13 10:03:35 +02:00
$this->methods = \array_filter($this->methods, fn (MethodDoc $doc): bool => !$doc->shouldIgnore());
2020-10-08 21:35:32 +02:00
}
public function format(): string
{
2020-10-13 10:03:35 +02:00
$init = parent::format();
2020-10-13 21:54:25 +02:00
if ($this->constants) {
$init .= "\n";
$init .= "## Constants\n";
foreach ($this->constants as $name => [, $description]) {
$description = \trim($description);
$description = \str_replace("\n", "\n ", $description);
$init .= "* {$this->className}::$name: $description\n";
$init .= "\n";
}
}
2020-10-13 10:03:35 +02:00
if ($this->methods) {
$init .= "\n";
2020-10-13 21:54:25 +02:00
$init .= "## Method list:\n";
2020-10-13 10:03:35 +02:00
foreach ($this->methods as $method) {
2020-10-13 21:54:25 +02:00
$init .= "* `".$method->getSignature()."`\n";
2020-10-13 10:03:35 +02:00
}
$init .= "\n";
2020-10-13 21:54:25 +02:00
$init .= "## Methods:\n";
2020-10-13 10:03:35 +02:00
foreach ($this->methods as $method) {
$init .= $method->format();
$init .= "\n";
}
}
2020-10-13 21:54:25 +02:00
if ($this->properties) {
$init .= "## Properties\n";
foreach ($this->properties as $name => [$type, $description]) {
$init .= "* `\$$name`: `$type` $description";
}
2020-10-13 10:03:35 +02:00
}
return $init;
2020-10-08 21:35:32 +02:00
}
}