From 37d3f94af2c7b7ed76b9506525827d0c27bcf26a Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 13 Oct 2020 21:54:25 +0200 Subject: [PATCH] Use getLogger in APILoop --- src/danog/MadelineProto/EventHandler.php | 10 ++ src/danog/MadelineProto/Loop/APILoop.php | 3 +- .../MadelineProto/MTProtoTools/Files.php | 1 - src/danog/MadelineProto/PhpDoc/ClassDoc.php | 50 ++++---- src/danog/MadelineProto/PhpDoc/GenericDoc.php | 16 ++- src/danog/MadelineProto/PhpDoc/MethodDoc.php | 40 ++++-- .../MadelineProto/PhpDoc/PropertyDoc.php | 16 --- src/danog/MadelineProto/PhpDocBuilder.php | 57 ++++++++- tools/phpdoc.php | 117 ++++++++++++++++++ 9 files changed, 253 insertions(+), 57 deletions(-) delete mode 100644 src/danog/MadelineProto/PhpDoc/PropertyDoc.php create mode 100644 tools/phpdoc.php diff --git a/src/danog/MadelineProto/EventHandler.php b/src/danog/MadelineProto/EventHandler.php index ac66d245..e5d0ed78 100644 --- a/src/danog/MadelineProto/EventHandler.php +++ b/src/danog/MadelineProto/EventHandler.php @@ -83,4 +83,14 @@ abstract class EventHandler extends InternalDoc { return []; } + + /** + * Get API instance. + * + * @return MTProto + */ + public function getAPI(): MTProto + { + return $this->API; + } } diff --git a/src/danog/MadelineProto/Loop/APILoop.php b/src/danog/MadelineProto/Loop/APILoop.php index 0603a1e9..ecb6baae 100644 --- a/src/danog/MadelineProto/Loop/APILoop.php +++ b/src/danog/MadelineProto/Loop/APILoop.php @@ -18,6 +18,7 @@ namespace danog\MadelineProto\Loop; +use danog\MadelineProto\EventHandler; use danog\MadelineProto\InternalDoc; /** @@ -41,6 +42,6 @@ trait APILoop public function __construct(InternalDoc $API) { $this->API = $API; - $this->setLogger($API->getLogger()); + $this->setLogger($API instanceof EventHandler ? $API->getAPI()->getLogger() : $API->logger); } } diff --git a/src/danog/MadelineProto/MTProtoTools/Files.php b/src/danog/MadelineProto/MTProtoTools/Files.php index 098f659f..f4a7d7ef 100644 --- a/src/danog/MadelineProto/MTProtoTools/Files.php +++ b/src/danog/MadelineProto/MTProtoTools/Files.php @@ -173,7 +173,6 @@ trait Files $start = \microtime(true); while ($part_num < $part_total_num) { $resa = $callable($part_num); - \var_dump($resa); $writePromise = Tools::call($this->methodCallAsyncWrite($method, $resa, ['heavy' => true, 'file' => true, 'datacenter' => &$datacenter])); if (!$seekable) { yield $writePromise; diff --git a/src/danog/MadelineProto/PhpDoc/ClassDoc.php b/src/danog/MadelineProto/PhpDoc/ClassDoc.php index 6aff681b..d39b01f5 100644 --- a/src/danog/MadelineProto/PhpDoc/ClassDoc.php +++ b/src/danog/MadelineProto/PhpDoc/ClassDoc.php @@ -15,7 +15,7 @@ class ClassDoc extends GenericDoc /** * Properties. * - * @var array + * @var array */ private array $properties = []; /** @@ -24,6 +24,10 @@ class ClassDoc extends GenericDoc * @var array */ private array $methods = []; + /** + * Constants. + */ + private array $constants = []; public function __construct(PhpDocBuilder $builder, ReflectionClass $reflectionClass) { $this->builder = $builder; @@ -41,12 +45,10 @@ class ClassDoc extends GenericDoc $tags = $doc->getTags(); foreach ($tags as $tag) { if ($tag instanceof Property) { - $this->properties[$tag->getVariableName()] = new PropertyDoc( - $this->builder, - $tag->getName(), + $this->properties[$tag->getVariableName()] = [ $tag->getType(), $tag->getDescription() - ); + ]; } if ($tag instanceof InvalidTag && $tag->getName() === 'property') { [$type, $description] = \explode(" $", $tag->render(), 2); @@ -54,15 +56,12 @@ class ClassDoc extends GenericDoc [$varName, $description] = \explode(" ", $description, 2); $type = \str_replace('@property ', '', $type); $description ??= ''; - $this->properties[$varName] = new PropertyDoc( - $this->builder, - $varName, + $this->properties[$varName] = [ $type, $description - ); + ]; } } - $constants = []; foreach ($reflectionClass->getConstants() as $key => $value) { $refl = new ReflectionClassConstant($reflectionClass->getName(), $key); if (!$refl->isPublic()) { @@ -84,7 +83,7 @@ class ClassDoc extends GenericDoc } } $description = \trim($description); - $constants[$key] = [ + $this->constants[$key] = [ $value, $description ]; @@ -99,32 +98,39 @@ class ClassDoc extends GenericDoc } $this->methods = \array_filter($this->methods, fn (MethodDoc $doc): bool => !$doc->shouldIgnore()); - //$this->properties = \array_filter($this->properties, fn (PropertyDoc $doc): bool => !$doc->shouldIgnore()); } public function format(): string { $init = parent::format(); - $methods = ''; - $properties = ''; + 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"; + } + } if ($this->methods) { $init .= "\n"; + $init .= "## Method list:\n"; foreach ($this->methods as $method) { - $init .= "* ".$method->getSignature()."\n"; + $init .= "* `".$method->getSignature()."`\n"; } $init .= "\n"; - $init .= "## Methods:\n$methods\n"; + $init .= "## Methods:\n"; foreach ($this->methods as $method) { $init .= $method->format(); $init .= "\n"; } } - if ($properties) { - $init .= "## Properties:\n$properties\n"; - /*foreach ($this->properties as $property) { - $init .= $property->format(); - $init .= "\n"; - }*/ + if ($this->properties) { + $init .= "## Properties\n"; + foreach ($this->properties as $name => [$type, $description]) { + $init .= "* `\$$name`: `$type` $description"; + } } return $init; } diff --git a/src/danog/MadelineProto/PhpDoc/GenericDoc.php b/src/danog/MadelineProto/PhpDoc/GenericDoc.php index ba8d4a16..28fc381b 100644 --- a/src/danog/MadelineProto/PhpDoc/GenericDoc.php +++ b/src/danog/MadelineProto/PhpDoc/GenericDoc.php @@ -37,9 +37,11 @@ abstract class GenericDoc */ protected array $seeAlso = []; /** - * Author. + * Authors. + * + * @var Author[] */ - protected Author $author; + protected array $authors; /** * Ignore this class. */ @@ -60,10 +62,10 @@ abstract class GenericDoc $this->description = $doc->getDescription(); $tags = $doc->getTags(); - $this->author = new Author("Daniil Gentili", "daniil@daniil.it"); + $this->authors = $this->builder->getAuthors(); foreach ($tags as $tag) { if ($tag instanceof Author) { - $this->author = $tag; + $this->authors []= $tag; } if ($tag instanceof Deprecated) { $this->ignore = true; @@ -118,6 +120,10 @@ abstract class GenericDoc } public function format(): string { + $authors = ''; + foreach ($this->authors as $author) { + $authors .= "> Author: $author \n"; + } $seeAlso = $this->seeAlso(); return <<name`: $this->title [Back to API index](index.md) - > Author: $this->author + $authors $this->description $seeAlso diff --git a/src/danog/MadelineProto/PhpDoc/MethodDoc.php b/src/danog/MadelineProto/PhpDoc/MethodDoc.php index 0f2138c7..9df51993 100644 --- a/src/danog/MadelineProto/PhpDoc/MethodDoc.php +++ b/src/danog/MadelineProto/PhpDoc/MethodDoc.php @@ -13,8 +13,10 @@ use ReflectionMethod; class MethodDoc extends GenericDoc { - private $return; + private Return_ $return; + private string $psalmReturn; private array $params = []; + private array $psalmParams = []; public function __construct(PhpDocBuilder $phpDocBuilder, ReflectionFunctionAbstract $method) { $this->builder = $phpDocBuilder; @@ -39,10 +41,10 @@ class MethodDoc extends GenericDoc $tag->getType(), $tag->getDescription() ]; - } elseif ($tag instanceof Return_ && !$this->return) { + } elseif ($tag instanceof Return_ && !isset($this->return)) { $this->return = $tag; } elseif ($tag instanceof Generic && $tag->getName() === 'psalm-return') { - $this->return = $tag; + $this->psalmReturn = $tag; } elseif ($tag instanceof Generic && $tag->getName() === 'psalm-param') { [$type, $description] = \explode(" $", $tag->getDescription(), 2); $description .= ' '; @@ -52,7 +54,7 @@ class MethodDoc extends GenericDoc } else { $description = new Description($description); } - $this->params[$varName] = [ + $this->psalmParams[$varName] = [ $type, $description ]; @@ -71,23 +73,45 @@ class MethodDoc extends GenericDoc } $sig = \trim($sig, ', '); $sig .= ')'; - if ($this->return) { + if (isset($this->return)) { $sig .= ': '; - $sig .= $this->return; + $sig .= $this->builder->resolveTypeAlias($this->className, $this->return); } return $sig; } public function format(): string { - $sig = '### '.$this->getSignature(); + $sig = '### `'.$this->getSignature()."`"; $sig .= "\n\n"; $sig .= $this->title; $sig .= "\n"; $sig .= $this->description; $sig .= "\n"; - if ($this->return && $this->return->getDescription() && $this->return->getDescription()->render()) { + if ($this->psalmParams || $this->params) { + $sig .= "\nParameters:\n"; + foreach ($this->params as $name => [$type, $description]) { + if ($type) { + $type = $this->builder->resolveTypeAlias($this->className, $type); + } + $sig .= "* `\$$name`: `$type` $description \n"; + if (isset($this->psalmParams[$name])) { + [$psalmType] = $this->psalmParams[$name]; + $psalmType = \trim(\str_replace("\n", "\n ", $psalmType)); + + $sig .= " Full type:\n"; + $sig .= " ```\n"; + $sig .= " $psalmType\n"; + $sig .= " ```\n"; + } + } + $sig .= "\n"; + } + if (isset($this->return) && $this->return->getDescription() && $this->return->getDescription()->render()) { $sig .= "\nReturn value: ".$this->return->getDescription()."\n"; } + if (isset($this->psalmReturn)) { + $sig .= "\nFully typed return value:\n```\n".$this->psalmReturn."\n```"; + } $sig .= $this->seeAlso(); $sig .= "\n"; return $sig; diff --git a/src/danog/MadelineProto/PhpDoc/PropertyDoc.php b/src/danog/MadelineProto/PhpDoc/PropertyDoc.php deleted file mode 100644 index 372de094..00000000 --- a/src/danog/MadelineProto/PhpDoc/PropertyDoc.php +++ /dev/null @@ -1,16 +0,0 @@ -factory = DocBlockFactory::createInstance(); $this->namespace = $namespace; $this->mode = $mode; + + $appRoot = new \danog\ClassFinder\AppConfig; + $appRoot = $appRoot->getAppRoot(); + $appRoot .= "/composer.json"; + $json = \json_decode(\file_get_contents($appRoot), true); + $authors = $json['authors'] ?? []; + + foreach ($authors as $author) { + $this->authors []= new Author($author['name'], $author['email']); + } + + if (!$this->namespace) { + $namespaces = array_keys($json['autoload']['psr-4']); + $this->namespace = $namespaces[0]; + foreach ($namespaces as $namespace) { + if (strlen($namespace) && strlen($namespace) < strlen($this->namespace)) { + $this->namespace = $namespace; + } + } + } } /** * Set filter to ignore certain classes. @@ -120,7 +145,7 @@ class PhpDocBuilder $this->addTypeAliases($class); } foreach ($classes as $class) { - if ($this->ignore && ($this->ignore)($class)) { + if ($this->ignore && $this->shouldIgnore($class)) { continue; } $class = \function_exists($class) @@ -233,6 +258,30 @@ class PhpDocBuilder */ public function shouldIgnore(string $class): bool { - return ($this->ignore)($class); + return !($this->ignore)($class); + } + + /** + * Get authors. + * + * @return Author[] + */ + public function getAuthors(): array + { + return $this->authors; + } + + /** + * Set authors. + * + * @param Author[] $authors Authors + * + * @return self + */ + public function setAuthors(array $authors): self + { + $this->authors = $authors; + + return $this; } } diff --git a/tools/phpdoc.php b/tools/phpdoc.php new file mode 100644 index 00000000..29b81e27 --- /dev/null +++ b/tools/phpdoc.php @@ -0,0 +1,117 @@ +isTrait(); +}; + +PhpDocBuilder::fromNamespace() + ->setFilter($filter) + ->setOutput(__DIR__.'/../docs/docs/PHP/') + ->run();