From 0842111c3ce5898443f8fcbb4fe993cfd11a0eb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Felix=20=C5=A0ulc?= Date: Wed, 15 Nov 2023 12:26:45 +0100 Subject: [PATCH] Codebase: add types, codesniffer + phpstan --- src/Console/GenerateDocuCommand.php | 57 +++++-------------- .../Exception/ParamsValidatorException.php | 5 +- src/Console/Utils/Logger.php | 10 ++-- src/Console/Utils/ParamsValidator.php | 22 +++---- src/Generator/Assets.php | 56 ++++++------------ src/Generator/DocuGenerator.php | 15 ++--- .../Exception/DocuFileGeneratorException.php | 5 +- .../Exception/DocuGeneratorException.php | 5 +- src/Http/AuthCredentials.php | 11 ++-- src/Markdown/DocuScope.php | 27 +++------ src/Markdown/Macro/AbstractMacroVariable.php | 14 ++--- src/Markdown/Macro/IMacro.php | 5 +- .../Macro/Index/AbstractIndexSection.php | 19 +++---- .../Exception/PartDidNotMatchException.php | 5 +- src/Markdown/Macro/Index/IIndexSection.php | 6 +- src/Markdown/Macro/Index/IndexSection.php | 10 +--- src/Markdown/Macro/Index/IndexSubTitle.php | 10 +--- src/Markdown/Macro/Index/IndexTitle.php | 10 +--- src/Markdown/Macro/MacroBlockVariable.php | 7 +-- .../Macro/MacroBlockVariableOutput.php | 7 +-- src/Markdown/Macro/MacroInclude.php | 13 ++--- src/Markdown/Macro/MacroIndex.php | 9 +-- src/Markdown/Macro/MacroInlineFileLink.php | 17 ++---- src/Markdown/Macro/MacroInlineVariable.php | 7 +-- .../Macro/MacroInlineVariableOutput.php | 7 +-- src/Markdown/Macro/MacroSection.php | 23 +++----- src/Markdown/Macro/Utils/FileHash.php | 5 +- src/Markdown/Parser.php | 33 ++++------- src/Parsedown/CustomParsedown.php | 15 ++--- 29 files changed, 155 insertions(+), 280 deletions(-) diff --git a/src/Console/GenerateDocuCommand.php b/src/Console/GenerateDocuCommand.php index b8f5632..7ac3e8d 100644 --- a/src/Console/GenerateDocuCommand.php +++ b/src/Console/GenerateDocuCommand.php @@ -1,6 +1,4 @@ -paramsValidator = new ParamsValidator($binDir); } - public function initialize(InputInterface $input, OutputInterface $output): void { $input->validate(); @@ -77,27 +52,27 @@ public function initialize(InputInterface $input, OutputInterface $output): void $addCss = $input->getOption('addCss'); if (!is_string($inputDirectory)) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } if (!is_string($outputDirectory)) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } if (!is_string($httpAuthUser) && $httpAuthUser !== null) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } if (!is_string($httpAuthPass) && $httpAuthPass !== null) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } if (!is_bool($overwriteOutputDir)) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } if (!is_string($addCss) && $addCss !== null) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } $this->inputDirectory = $inputDirectory; @@ -130,7 +105,6 @@ public function initialize(InputInterface $input, OutputInterface $output): void } } - protected function configure(): void { $this->setName('anabelle') @@ -178,7 +152,6 @@ protected function configure(): void ); } - protected function execute(InputInterface $input, OutputInterface $output): int { $docuGenerator = new DocuGenerator( @@ -204,7 +177,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } - private function printError(OutputInterface $output, string $message): void { $formatter = $this->getHelper('formatter'); @@ -215,4 +187,5 @@ private function printError(OutputInterface $output, string $message): void $output->writeln("\n{$block}\n"); } } + } diff --git a/src/Console/Utils/Exception/ParamsValidatorException.php b/src/Console/Utils/Exception/ParamsValidatorException.php index 2b3459d..aaf0666 100644 --- a/src/Console/Utils/Exception/ParamsValidatorException.php +++ b/src/Console/Utils/Exception/ParamsValidatorException.php @@ -1,9 +1,8 @@ -output->writeln("Processing file [$path]..."); } + } diff --git a/src/Console/Utils/ParamsValidator.php b/src/Console/Utils/ParamsValidator.php index 313ee48..2988d28 100644 --- a/src/Console/Utils/ParamsValidator.php +++ b/src/Console/Utils/ParamsValidator.php @@ -1,6 +1,4 @@ -/index.md) @@ -32,7 +31,6 @@ public function validateInputParams( $this->validateSpecialCssFile($specialCssFile); } - /** * @throws ParamsValidatorException */ @@ -47,7 +45,6 @@ private function validateSpecialCssFile(?string $specialCssFile): void } } - /** * @throws ParamsValidatorException */ @@ -80,13 +77,12 @@ private function validateInputDirectory( if (file_exists($outputDirectory) && !$overwriteOutputDir) { throw new ParamsValidatorException( - "Output directory path already exists." - . " Delete it or use option [-o] as for \"overwrite\" output directory" + 'Output directory path already exists.' + . ' Delete it or use option [-o] as for "overwrite" output directory' ); } } - /** * @throws ParamsValidatorException */ @@ -100,7 +96,6 @@ private function validateIndexFile(string $inputDirectory): void } } - /** * @throws ParamsValidatorException */ @@ -110,11 +105,12 @@ private function validateAuthCredentials(AuthCredentials $authCredentials): void * Validate HTTP AUTH */ if ($authCredentials->getPass() === null && $authCredentials->getUser() !== null) { - throw new ParamsValidatorException("Please set --httpAuthPass [-p]"); + throw new ParamsValidatorException('Please set --httpAuthPass [-p]'); } if ($authCredentials->getPass() !== null && $authCredentials->getUser() === null) { - throw new ParamsValidatorException("Please set --httpAuthUser [-u]"); + throw new ParamsValidatorException('Please set --httpAuthUser [-u]'); } } + } diff --git a/src/Generator/Assets.php b/src/Generator/Assets.php index 505669e..018baef 100644 --- a/src/Generator/Assets.php +++ b/src/Generator/Assets.php @@ -1,6 +1,4 @@ - */ + private array $layoutStylesPaths; - /** - * @var string|null - */ - private $layoutStyles; + /** @var array */ + private array $layoutSriptsPaths; - /** - * @var string - */ - private $layoutFavicon; + private ?string $layoutStyles = null; + private string $layoutFavicon; public function __construct( private AuthCredentials $authCredentials, ?string $addCss - ) { + ) + { $this->layoutFile = __DIR__ . '/../../assets/layout.php'; $this->layoutStylesPaths = [ @@ -59,7 +44,6 @@ public function __construct( $this->layoutFavicon = __DIR__ . '/../../assets/favicon.ico'; } - public function saveFile(string $content, string $outputFile, bool $isLayout): void { if ($isLayout) { @@ -70,13 +54,11 @@ public function saveFile(string $content, string $outputFile, bool $isLayout): v } } - public function replaceHttpAuth(string &$template): void // Intentionally & { $template = str_replace('{httpAuth}', $this->getHttpAuthSnippet(), $template); } - public function getHttpAuthSnippet(): string { if ($this->authCredentials->getUser() !== null) { @@ -93,7 +75,6 @@ public function getHttpAuthSnippet(): string return ''; } - private function saveLayout(string $content, string $outputFile): void { $template = file_get_contents($this->layoutFile); @@ -104,7 +85,7 @@ private function saveLayout(string $content, string $outputFile): void $content = preg_replace('/^

.*<\/h1>\w*$/mU', '', $content); if ($content === null) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } $this->replaceContent($template, $content); @@ -113,19 +94,17 @@ private function saveLayout(string $content, string $outputFile): void $template = str_replace('{scripts}', $this->getLayoutSripts(), $template); if (is_array($template)) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } file_put_contents($outputFile, $this->minifyHtml($template)); } - private function saveSection(string $content, string $outputFile): void { file_put_contents($outputFile, $this->minifyHtml($content)); } - private function replaceTitle(string &$template, string $content): void // Intentionally & { $template = preg_match('/

(.+)<\/h1>/', $content, $matches) === 1 @@ -133,17 +112,15 @@ private function replaceTitle(string &$template, string $content): void // Inten : str_replace('{title}', 'API Docs', $template); } - private function replaceContent(string &$template, string $content): void // Intentionally & { $template = str_replace('{content}', $content, $template); } - private function getLayoutStyles(): string { if ($this->layoutStyles === null) { - $minifier = new CSS; + $minifier = new CSS(); foreach ($this->layoutStylesPaths as $file) { $minifier->add($file); @@ -155,10 +132,9 @@ private function getLayoutStyles(): string return $this->layoutStyles; } - private function getLayoutSripts(): string { - $minifier = new JS; + $minifier = new JS(); foreach ($this->layoutSriptsPaths as $file) { $minifier->add($file); @@ -167,7 +143,6 @@ private function getLayoutSripts(): string return $minifier->minify(); } - private function minifyHtml(string $html): string { $return = preg_replace( @@ -177,9 +152,10 @@ private function minifyHtml(string $html): string ); if ($return === null) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } return $return; } + } diff --git a/src/Generator/DocuGenerator.php b/src/Generator/DocuGenerator.php index b02a5e6..ab20788 100644 --- a/src/Generator/DocuGenerator.php +++ b/src/Generator/DocuGenerator.php @@ -1,6 +1,4 @@ -parser = new Parser( true, $authCredentials, @@ -36,7 +31,6 @@ public function __construct( ); } - /** * @throws DocuGeneratorException * @throws DocuFileGeneratorException @@ -52,4 +46,5 @@ public function run(): void $this->outputDirectory . "/index.{$fileType}" ); } + } diff --git a/src/Generator/Exception/DocuFileGeneratorException.php b/src/Generator/Exception/DocuFileGeneratorException.php index 508c72c..f03a86f 100644 --- a/src/Generator/Exception/DocuFileGeneratorException.php +++ b/src/Generator/Exception/DocuFileGeneratorException.php @@ -1,6 +1,4 @@ -user; } - public function getPass(): ?string { return $this->pass; } + } diff --git a/src/Markdown/DocuScope.php b/src/Markdown/DocuScope.php index 0360511..46f376a 100644 --- a/src/Markdown/DocuScope.php +++ b/src/Markdown/DocuScope.php @@ -1,6 +1,4 @@ -outputDirectory; } - /** * @throws DocuGeneratorException */ @@ -43,7 +36,6 @@ public function addInlineVariable(string $varName, string $value): void $this->inlineVariables[$varName] = $value; } - /** * @throws DocuGeneratorException */ @@ -58,7 +50,6 @@ public function getInlineVariable(string $varName): string return $this->inlineVariables[$varName]; } - /** * @throws DocuGeneratorException */ @@ -73,7 +64,6 @@ public function addBlockVariable(string $varName, string $value): void $this->blockVariables[$varName] = $value; } - /** * @throws DocuGeneratorException */ @@ -87,4 +77,5 @@ public function getBlockVariable(string $varName): string return $this->blockVariables[$varName]; } + } diff --git a/src/Markdown/Macro/AbstractMacroVariable.php b/src/Markdown/Macro/AbstractMacroVariable.php index 62afac3..457a9bd 100644 --- a/src/Markdown/Macro/AbstractMacroVariable.php +++ b/src/Markdown/Macro/AbstractMacroVariable.php @@ -1,6 +1,4 @@ -runVariableMacro($content, 1); } - abstract protected function runVariableMacro( string &$content, int $depth ): void; // Intentionally & - /** * @return AbstractMacroVariable[] */ @@ -48,7 +44,6 @@ protected function getMacrosToRunOnBlockVariables(): array ]; } - /** * @return AbstractMacroVariable[] */ @@ -59,4 +54,5 @@ protected function getMacrosToRunOnInlineVariables(): array new MacroInlineVariableOutput($this->docuScope), ]; } + } diff --git a/src/Markdown/Macro/IMacro.php b/src/Markdown/Macro/IMacro.php index c785e43..7c50bde 100644 --- a/src/Markdown/Macro/IMacro.php +++ b/src/Markdown/Macro/IMacro.php @@ -1,6 +1,4 @@ -contentString; } + } diff --git a/src/Markdown/Macro/Index/Exception/PartDidNotMatchException.php b/src/Markdown/Macro/Index/Exception/PartDidNotMatchException.php index a38f4d0..d20c0a8 100644 --- a/src/Markdown/Macro/Index/Exception/PartDidNotMatchException.php +++ b/src/Markdown/Macro/Index/Exception/PartDidNotMatchException.php @@ -1,9 +1,8 @@ -docuScope->addBlockVariable($input[1], $input[2]); return ''; @@ -22,4 +20,5 @@ function(array $input): string { $content ); } + } diff --git a/src/Markdown/Macro/MacroBlockVariableOutput.php b/src/Markdown/Macro/MacroBlockVariableOutput.php index e304b3d..7525e10 100644 --- a/src/Markdown/Macro/MacroBlockVariableOutput.php +++ b/src/Markdown/Macro/MacroBlockVariableOutput.php @@ -1,6 +1,4 @@ -includeFile($dir, basename($input[1])); @@ -35,7 +32,6 @@ function(array $input) use ($inputDirectory): string { ); } - /** * @throws DocuGeneratorException */ @@ -57,7 +53,7 @@ public function includeFile(string $directory, string $filename): string $includeContent = preg_replace_callback( self::INCLUDE_PATTERN, - function(array $input) use ($directory): string { + function (array $input) use ($directory): string { $dir = $directory . '/' . dirname($input[1]); return $this->includeFile($dir, basename($input[1])); @@ -66,9 +62,10 @@ function(array $input) use ($directory): string { ); if ($includeContent === null) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } return $includeContent; } + } diff --git a/src/Markdown/Macro/MacroIndex.php b/src/Markdown/Macro/MacroIndex.php index 357bb67..d13a5ba 100644 --- a/src/Markdown/Macro/MacroIndex.php +++ b/src/Markdown/Macro/MacroIndex.php @@ -1,6 +1,4 @@ -getContentString() . PHP_EOL; } } + } diff --git a/src/Markdown/Macro/MacroInlineFileLink.php b/src/Markdown/Macro/MacroInlineFileLink.php index 676aa4e..e499462 100644 --- a/src/Markdown/Macro/MacroInlineFileLink.php +++ b/src/Markdown/Macro/MacroInlineFileLink.php @@ -1,6 +1,4 @@ -fileHashAlgo = $fileHashAlgo ?? [FileHash::class, 'md5File']; } - /** * @throws DocuGeneratorException */ @@ -40,7 +34,7 @@ public function runMacro( */ $content = preg_replace_callback( self::LINK_PATTERN, - function(array $input) use ($inputDirectory): string { + function (array $input) use ($inputDirectory): string { $text = $input[1]; $path = trim($input[2], '/'); @@ -48,7 +42,7 @@ function(array $input) use ($inputDirectory): string { $fileHash = call_user_func($this->fileHashAlgo, $inputDirectory . '/' . $path); $fileName = $fileHash . '.' . pathinfo($path, PATHINFO_EXTENSION); - $targetPath = $this->docuScope->getOutputDirectory() . '/_files' . '/' . $fileName; + $targetPath = $this->docuScope->getOutputDirectory() . '/_files/' . $fileName; if (!file_exists(dirname($targetPath))) { mkdir(dirname($targetPath), 0755, true); @@ -59,7 +53,7 @@ function(array $input) use ($inputDirectory): string { $targetPath = preg_replace('~^[^/]+/~', '', $targetPath); if ($targetPath === null) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } return (string) Html::el('a')->href('_files/' . basename($targetPath)) @@ -72,4 +66,5 @@ function(array $input) use ($inputDirectory): string { $content ); } + } diff --git a/src/Markdown/Macro/MacroInlineVariable.php b/src/Markdown/Macro/MacroInlineVariable.php index 337e0f0..9a4695c 100644 --- a/src/Markdown/Macro/MacroInlineVariable.php +++ b/src/Markdown/Macro/MacroInlineVariable.php @@ -1,6 +1,4 @@ -docuScope->addInlineVariable($input[1], $input[2]); return ''; @@ -22,4 +20,5 @@ function(array $input): string { $content ); } + } diff --git a/src/Markdown/Macro/MacroInlineVariableOutput.php b/src/Markdown/Macro/MacroInlineVariableOutput.php index 8480c95..b185754 100644 --- a/src/Markdown/Macro/MacroInlineVariableOutput.php +++ b/src/Markdown/Macro/MacroInlineVariableOutput.php @@ -1,6 +1,4 @@ -docuScope->getInlineVariable($input[1]); if ($depth <= parent::MAX_EXECUTE_DEPTH) { @@ -28,4 +26,5 @@ function(array $input) use ($depth): string { $content ); } + } diff --git a/src/Markdown/Macro/MacroSection.php b/src/Markdown/Macro/MacroSection.php index d013c9e..a35fcee 100644 --- a/src/Markdown/Macro/MacroSection.php +++ b/src/Markdown/Macro/MacroSection.php @@ -1,6 +1,4 @@ -parser = new Parser(false, $authCredentials, $logger, $docuScope, null); } - /** * @throws DocuGeneratorException */ @@ -44,13 +38,13 @@ public function runMacro( /** * Find "@@" sections and parse their child .md file * == normal section with json-rpc methods - * + * * Find "@" sections and parse their child .md file * == home section, aditional description etc */ $content = preg_replace_callback( '/^@@? (.+[^:]):(.+\.md)/m', - function(array $input) use ($inputDirectory, $outputDirectory, $fileType): string { + function (array $input) use ($inputDirectory, $outputDirectory, $fileType): string { $inputFile = $inputDirectory . '/' . dirname($input[2]) . '/' . basename($input[2]); /** @@ -59,7 +53,7 @@ function(array $input) use ($inputDirectory, $outputDirectory, $fileType): strin $outputFile = preg_replace('/md$/', $fileType, $inputFile); if ($outputFile === null) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } /** @@ -72,7 +66,7 @@ function(array $input) use ($inputDirectory, $outputDirectory, $fileType): strin $return = preg_replace('/md$/', $fileType, $input[0]); if (!is_string($return)) { - throw new \UnexpectedValueException; + throw new \UnexpectedValueException(); } return $return; @@ -80,4 +74,5 @@ function(array $input) use ($inputDirectory, $outputDirectory, $fileType): strin $content ); } + } diff --git a/src/Markdown/Macro/Utils/FileHash.php b/src/Markdown/Macro/Utils/FileHash.php index 88d2467..5997733 100644 --- a/src/Markdown/Macro/Utils/FileHash.php +++ b/src/Markdown/Macro/Utils/FileHash.php @@ -1,6 +1,4 @@ -parsedown = new CustomParsedown; + ) + { + $this->parsedown = new CustomParsedown(); $this->assets = new Assets($authCredentials, $addCss); $this->setupMacros(); } - /** * @throws DocuGeneratorException * @throws DocuFileGeneratorException @@ -91,10 +80,9 @@ public function parseFile(string $inputFile, string $outputFile): void ); } - private function setupMacros(): void { - $this->macros[] = new MacroInclude; + $this->macros[] = new MacroInclude(); $this->macros[] = new MacroInlineVariable($this->docuScope); $this->macros[] = new MacroInlineVariableOutput($this->docuScope); $this->macros[] = new MacroInlineFileLink($this->docuScope); @@ -102,7 +90,7 @@ private function setupMacros(): void $this->macros[] = new MacroBlockVariableOutput($this->docuScope); if ($this->isLayout) { - $this->macros[] = new MacroIndex; + $this->macros[] = new MacroIndex(); $this->macros[] = new MacroSection( $this->logger, $this->authCredentials, @@ -110,4 +98,5 @@ private function setupMacros(): void ); } } + } diff --git a/src/Parsedown/CustomParsedown.php b/src/Parsedown/CustomParsedown.php index ebeae10..f1e2f22 100644 --- a/src/Parsedown/CustomParsedown.php +++ b/src/Parsedown/CustomParsedown.php @@ -1,6 +1,4 @@ -InlineTypes['@'][]= 'Section'; + $this->InlineTypes['@'][] = 'Section'; $this->inlineMarkerList .= '@'; } - /** * Either "section" or "home" element + * + * @param string[] $excerpt + * @return mixed[]|null */ protected function inlineSection(array $excerpt): ?array { @@ -31,7 +31,7 @@ protected function inlineSection(array $excerpt): ?array : 'button'; return [ - 'extent' => strlen($matches[0]), + 'extent' => strlen($matches[0]), 'element' => [ 'name' => $element, 'text' => $matches[2], @@ -46,4 +46,5 @@ protected function inlineSection(array $excerpt): ?array return null; } + }