From 9fc4b5bcf9632632d17fc691648483e9867570bf Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 22 Feb 2021 16:31:25 +0100 Subject: [PATCH 1/2] Misc. fixes and minor improvements --- src/CI/MissingFilesChecker.php | 5 ++--- src/Command/BuildDocsCommand.php | 4 ++-- src/Directive/AbstractAdmonitionDirective.php | 2 +- src/Directive/AdmonitionDirective.php | 2 +- src/Generator/HtmlForPdfGenerator.php | 8 ++++---- src/Generator/JsonGenerator.php | 4 ++-- src/Listener/BuildProgressListener.php | 2 +- src/Reference/PhpMethodReference.php | 3 +-- src/Renderers/CodeNodeRenderer.php | 4 ++-- src/Twig/AssetsExtension.php | 2 +- 10 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/CI/MissingFilesChecker.php b/src/CI/MissingFilesChecker.php index de831f8..5254905 100644 --- a/src/CI/MissingFilesChecker.php +++ b/src/CI/MissingFilesChecker.php @@ -12,7 +12,6 @@ namespace SymfonyDocsBuilder\CI; use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Finder\Finder; use SymfonyDocsBuilder\BuildContext; class MissingFilesChecker @@ -38,10 +37,10 @@ public function getMissingFiles(): array $htmlFile = sprintf( '%s/%s.html', $this->buildContext->getOutputDir(), - substr($sourcePath, 0, \strlen($sourcePath) - 4) + substr($sourcePath, 0, -4) ); - $firstLine = fgets(fopen($file->getRealPath(), 'r')); + $firstLine = fgets(fopen($file->getRealPath(), 'rb')); if (!$this->filesystem->exists($htmlFile) && ':orphan:' !== trim($firstLine)) { $orphanedFiles[] = $htmlFile; } diff --git a/src/Command/BuildDocsCommand.php b/src/Command/BuildDocsCommand.php index fd65cd1..3617451 100644 --- a/src/Command/BuildDocsCommand.php +++ b/src/Command/BuildDocsCommand.php @@ -132,7 +132,7 @@ protected function initialize(InputInterface $input, OutputInterface $output) $configFileParser->processConfigFile($sourceDir); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $builder = new Builder( KernelFactory::createKernel($this->buildContext, $this->urlChecker ?? null) @@ -182,7 +182,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->io->success('Build completed successfully!'); } - return 0; + return Command::SUCCESS; } private function generateJson(Metas $metas) diff --git a/src/Directive/AbstractAdmonitionDirective.php b/src/Directive/AbstractAdmonitionDirective.php index 3e51f02..6209efc 100644 --- a/src/Directive/AbstractAdmonitionDirective.php +++ b/src/Directive/AbstractAdmonitionDirective.php @@ -34,7 +34,7 @@ final public function processSub(Parser $parser, ?Node $document, string $variab [ 'name' => $this->name, 'text' => $this->text, - 'class' => isset($options['class']) ? $options['class'] : null, + 'class' => $options['class'] ?? null, ] ); diff --git a/src/Directive/AdmonitionDirective.php b/src/Directive/AdmonitionDirective.php index 0bcbe1f..569e91e 100644 --- a/src/Directive/AdmonitionDirective.php +++ b/src/Directive/AdmonitionDirective.php @@ -24,7 +24,7 @@ public function processSub(Parser $parser, ?Node $document, string $variable, st // had a class of 'admonition-" 'name' => '', 'text' => $data, - 'class' => isset($options['class']) ? $options['class'] : null, + 'class' => $options['class'] ?? null, ] ); diff --git a/src/Generator/HtmlForPdfGenerator.php b/src/Generator/HtmlForPdfGenerator.php index 0dbd692..ea0acca 100644 --- a/src/Generator/HtmlForPdfGenerator.php +++ b/src/Generator/HtmlForPdfGenerator.php @@ -99,14 +99,14 @@ private function fixInternalUrls(string $fileContent, string $dir): string { return preg_replace_callback( '/href="([^"]+?)"/', - function ($matches) use ($dir) { + static function ($matches) use ($dir) { if ('http' === substr($matches[1], 0, 4) || '#' === substr($matches[1], 0, 1)) { return $matches[0]; } $path = []; foreach (explode('/', $dir.'/'.str_replace(['.html', '#'], ['', '-'], $matches[1])) as $part) { - if ('..' == $part) { + if ('..' === $part) { array_pop($path); } else { $path[] = $part; @@ -130,7 +130,7 @@ private function fixUniqueIdsAndAnchors(string $fileContent, string $uid): strin { return preg_replace_callback( '/id="([^"]+)"/', - function ($matches) use ($uid) { + static function ($matches) use ($uid) { return sprintf('id="%s-%s"', $uid, $matches[1]); }, $fileContent @@ -145,7 +145,7 @@ private function cleanupContent($content) // convert links to footnote $content = preg_replace_callback( '#]*)>(.*?)#', - function ($matches) { + static function ($matches) { if (0 === strpos($matches[2], 'http')) { return sprintf('%s', $matches[2], $matches[2]); } diff --git a/src/Generator/JsonGenerator.php b/src/Generator/JsonGenerator.php index cd3a3c9..ba84981 100644 --- a/src/Generator/JsonGenerator.php +++ b/src/Generator/JsonGenerator.php @@ -117,7 +117,7 @@ private function guessNext(string $parserFilename): ?array ]; } - list($toc, $indexCurrentFile) = $this->getNextPrevInformation($parserFilename); + [$toc, $indexCurrentFile] = $this->getNextPrevInformation($parserFilename); if (!isset($toc[$indexCurrentFile + 1])) { return null; @@ -147,7 +147,7 @@ private function guessPrev(string $parserFilename): ?array return null; } - list($toc, $indexCurrentFile) = $this->getNextPrevInformation($parserFilename); + [$toc, $indexCurrentFile] = $this->getNextPrevInformation($parserFilename); // if current file is the first one of the chapter, prev is the direct parent if (0 === $indexCurrentFile) { diff --git a/src/Listener/BuildProgressListener.php b/src/Listener/BuildProgressListener.php index fef4771..11d1db6 100644 --- a/src/Listener/BuildProgressListener.php +++ b/src/Listener/BuildProgressListener.php @@ -67,7 +67,7 @@ public function preBuildParse(PreBuildParseEvent $event) public function postParseDocument(PostParseDocumentEvent $postParseDocumentEvent): void { $file = $postParseDocumentEvent->getDocumentNode()->getEnvironment()->getCurrentFileName(); - if (!\in_array($file, $this->parsedFiles)) { + if (!\in_array($file, $this->parsedFiles, true)) { $this->parsedFiles[] = $file; $this->progressBar->advance(); } diff --git a/src/Reference/PhpMethodReference.php b/src/Reference/PhpMethodReference.php index 679d745..ac53744 100644 --- a/src/Reference/PhpMethodReference.php +++ b/src/Reference/PhpMethodReference.php @@ -29,8 +29,7 @@ public function getName(): string public function resolve(Environment $environment, string $data): ResolvedReference { - $class = explode('::', $data)[0]; - $method = explode('::', $data)[1]; + [$class, $method] = explode('::', $data, 2); return new ResolvedReference( $environment->getCurrentFileName(), diff --git a/src/Renderers/CodeNodeRenderer.php b/src/Renderers/CodeNodeRenderer.php index ad4304a..86fef47 100644 --- a/src/Renderers/CodeNodeRenderer.php +++ b/src/Renderers/CodeNodeRenderer.php @@ -90,12 +90,12 @@ public static function isLanguageSupported(string $lang): bool $highlighter = new Highlighter(); $supportedLanguages = array_merge( array_keys(self::LANGUAGES_MAPPING), - $highlighter->listLanguages(true), + $highlighter->listRegisteredLanguages(true), // not highlighted, but valid ['text'] ); - return \in_array($lang, $supportedLanguages); + return \in_array($lang, $supportedLanguages, true); } private function getLines(string $code): array diff --git a/src/Twig/AssetsExtension.php b/src/Twig/AssetsExtension.php index a45a972..670e5b5 100644 --- a/src/Twig/AssetsExtension.php +++ b/src/Twig/AssetsExtension.php @@ -21,7 +21,7 @@ public function getFunctions(): array ]; } - public function asset($path) + public function asset($path): string { return sprintf('assets/%s', $path); } From 13c9576ac67a17557dc20dc64cb2bcf02471e88f Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 22 Feb 2021 19:42:19 +0100 Subject: [PATCH 2/2] - --- src/Generator/HtmlForPdfGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generator/HtmlForPdfGenerator.php b/src/Generator/HtmlForPdfGenerator.php index ea0acca..30ae2a4 100644 --- a/src/Generator/HtmlForPdfGenerator.php +++ b/src/Generator/HtmlForPdfGenerator.php @@ -145,7 +145,7 @@ private function cleanupContent($content) // convert links to footnote $content = preg_replace_callback( '#]*)>(.*?)#', - static function ($matches) { + static function ($matches): string { if (0 === strpos($matches[2], 'http')) { return sprintf('%s', $matches[2], $matches[2]); }