Skip to content

Commit

Permalink
Class Documentation (Wiki) Generator improved
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Nov 16, 2024
1 parent 3a7ba2c commit 08e56c1
Showing 1 changed file with 90 additions and 7 deletions.
97 changes: 90 additions & 7 deletions make/genmethoddocs.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use horstoeko\stringmanagement\StringUtils;
use horstoeko\zugferd\ZugferdDocument;
use horstoeko\zugferd\ZugferdDocumentBuilder;
use horstoeko\zugferd\ZugferdDocumentPdfBuilder;
use horstoeko\zugferd\ZugferdDocumentPdfMerger;
Expand All @@ -10,6 +11,8 @@
use horstoeko\zugferd\ZugferdKositValidator;
use horstoeko\zugferd\ZugferdSettings;
use horstoeko\zugferd\ZugferdXsdValidator;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Printer;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
use phpDocumentor\Reflection\DocBlockFactory;
Expand Down Expand Up @@ -47,6 +50,17 @@ public function getClassName(): string
return $this->className;
}

/**
* Returns the base name of the current classname
*
* @return string
*/
public function getClassBasename(): string
{
$classParts = explode('\\', $this->className);
return end($classParts);
}

/**
* Magic method __toString, String converstion
*
Expand Down Expand Up @@ -144,7 +158,8 @@ public function getArray(): array
'name' => $parameterName,
'type' => $parameterType ? $parameterType->getName() : 'mixed',
'isNullable' => $parameterType && $parameterType->allowsNull(),
'defaultValue' => $parameter->isOptional() ? ($parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : 'none') : 'none',
'defaultValueavailable' => $parameter->isOptional() ? ($parameter->isDefaultValueAvailable() ? true : false) : false,
'defaultValue' => $parameter->isOptional() ? ($parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null) : null,
'description' => $paramDescriptions[$parameterName]['description'] ?? ''
];
}
Expand Down Expand Up @@ -222,6 +237,9 @@ public function generateMarkdown(): MarkDownGenerator

$this->addLineH1($this->extractor->getClassName());

$phpPrinter = new Printer;
$phpClass = new ClassType($this->extractor->getClassBasename());

if (!empty($metaData['class']['summary'])) {
$this->addLine($metaData['class']['summary'] ?? "")->addEmptyLine();
}
Expand All @@ -230,8 +248,12 @@ public function generateMarkdown(): MarkDownGenerator
$this->addLine($metaData['class']['description'] ?? "")->addEmptyLine();
}

if (!empty($metaData['methods'])) {
$this->addLineH2("Methods");
}

foreach ($metaData['methods'] as $methodName => $methodData) {
$this->addLineH2($methodName, $methodData["methodDetails"]["hasadditional"] === false);
$this->addLineH3($methodName, $methodData["methodDetails"]["hasadditional"] === false);

if ($methodData["methodDetails"]["static"] === true) {
$this->addToLastLine('<span style="color: white; background-color: blue; padding: 0.2em 0.5em; border-radius: 0.2em; font-size: .8rem">``[static]``</span>', " ");
Expand All @@ -241,10 +263,36 @@ public function generateMarkdown(): MarkDownGenerator
$this->addToLastLine('<span style="color: white; background-color: red; padding: 0.2em 0.5em; border-radius: 0.2em; font-size: .8rem">``[abstract]``</span>', " ");
}

if ($methodData["methodDetails"]["final"] === true) {
$this->addToLastLine('<span style="color: white; background-color: green; padding: 0.2em 0.5em; border-radius: 0.2em; font-size: .8rem">``[final]``</span>', " ");
}

if ($methodData["methodDetails"]["hasadditional"] === true) {
$this->addEmptyLine();
}

$phpMethod = $phpClass->addMethod($methodName);
$phpMethod->setPublic();
$phpMethod->setStatic($methodData["methodDetails"]["static"] === true);
$phpMethod->setAbstract($methodData["methodDetails"]["abstract"] === true);
$phpMethod->setFinal($methodData["methodDetails"]["final"] === true);
$phpMethod->setReturnType($methodData["return"]["type"]);

foreach ($methodData["parameters"] as $parameter) {
$phpParameter = $phpMethod
->addParameter($parameter["name"])
->setType($parameter["type"])
->setNullable($parameter["isNullable"]);

if ($parameter['defaultValueavailable'] === true) {
$phpParameter->setDefaultValue($parameter["defaultValue"]);
}
}

$this->addLineRaw("```php");
$this->addLineRaw($phpPrinter->printMethod($phpMethod));
$this->addLineRaw("```");

if (!empty($methodData["methodDetails"]["summary"])) {
$this->addLine($methodData["methodDetails"]["summary"])->addEmptyLine();
}
Expand All @@ -253,10 +301,6 @@ public function generateMarkdown(): MarkDownGenerator
$this->addLine($methodData["methodDetails"]["description"])->addEmptyLine();
}

if (!empty($methodData["return"]["type"])) {
$this->addLine('__Returns__ %s', $methodData["return"]["type"])->addEmptyLine();
}

if (!empty($methodData["parameters"])) {
$this->addLine("| Name | Type | Allows Null | Description");
$this->addLine("| :------ | :------ | :-----: | :------");
Expand Down Expand Up @@ -311,6 +355,24 @@ private function addLine(string $string, ...$args): MarkDownGenerator
return $this;
}

/**
* Add a line to internal container
*
* @param string $string
* @param mixed ...$args
* @return MarkDownGenerator
*/
private function addLineRaw(string $string, ...$args): MarkDownGenerator
{
if (StringUtils::stringIsNullOrEmpty($string)) {
return $this;
}

$this->lines[] = sprintf($string, ...$args);

return $this;
}

/**
* Add an empty line to internal container
*
Expand Down Expand Up @@ -368,7 +430,7 @@ private function addLineH2(string $string, bool $newLine = true): MarkDownGenera
*/
private function addLineH3(string $string, bool $newLine = true): MarkDownGenerator
{
$this->addLine("# %s", $string);
$this->addLine("### %s", $string);

if ($newLine) {
$this->addEmptyLine();
Expand Down Expand Up @@ -397,6 +459,26 @@ public function addToLastLine(string $string, string $delimiter = "", ...$args):
return $this;
}

/**
* Add a string to the latest line which was added (before)
*
* @param string $string
* @param string $delimiter
* @param mixed ...$args
* @return MarkDownGenerator
*/
public function addToLastLineBefore(string $string, string $delimiter = "", ...$args): MarkDownGenerator
{
if (empty($this->lines)) {
return $this->addLine($string, ...$args);
}

$lastIndex = count($this->lines) - 1;
$this->lines[$lastIndex] = sprintf($string, ...$args) . $delimiter . $this->lines[$lastIndex];

return $this;
}

/**
* Sanatize a string
*
Expand Down Expand Up @@ -448,6 +530,7 @@ public static function generate(array $classes)
}

BatchMarkDownGenerator::generate([
ZugferdDocument::class => dirname(__FILE__) . '/ClassOverview-ZugferdDocument.md',
ZugferdSettings::class => dirname(__FILE__) . '/ClassOverview-ZugferdSettings.md',
ZugferdDocumentBuilder::class => dirname(__FILE__) . '/ClassOverview-ZugferdDocumentBuilder.md',
ZugferdDocumentReader::class => dirname(__FILE__) . '/ClassOverview-ZugferdDocumentReader.md',
Expand Down

0 comments on commit 08e56c1

Please sign in to comment.