From 688de1d5b9058a96c2a2de9267a6472c6e97af4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Sat, 30 Nov 2019 22:24:10 +0100 Subject: [PATCH] Fix warnings and errors reported by Scrutinizer --- libs/Console/RunAction.php | 2 +- libs/ContentTypes/Markdown/LinkRenderer.php | 11 ++-------- libs/DauxHelper.php | 1 + .../ContentTypes/Markdown/LinkRenderer.php | 13 +++-------- .../ContentTypes/Markdown/TOCRenderer.php | 5 +++++ libs/Format/Confluence/Publisher.php | 12 +++++----- .../ContentTypes/Markdown/ImageRenderer.php | 22 +++++++++++++------ .../ContentTypes/Markdown/TOC/Processor.php | 2 +- .../ContentTypes/Markdown/TOC/Renderer.php | 5 +++++ libs/Format/HTML/Template.php | 6 ++--- libs/Format/HTMLFile/ContentPage.php | 6 +++-- .../ContentTypes/Markdown/LinkRenderer.php | 15 ++++--------- templates/home.php | 8 +++---- templates/layout/00_layout.php | 2 +- templates/partials/search_script.php | 2 +- 15 files changed, 56 insertions(+), 56 deletions(-) diff --git a/libs/Console/RunAction.php b/libs/Console/RunAction.php index 66cfe11..26e3a2b 100644 --- a/libs/Console/RunAction.php +++ b/libs/Console/RunAction.php @@ -19,7 +19,7 @@ trait RunAction $padding = $width - $this->getLength($title) - 10; try { - $response = $closure(function ($content) use (&$padding, $verbose) { + $response = $closure(function($content) use (&$padding, $verbose) { $padding -= $this->getLength($content); Daux::write($content, $verbose); }); diff --git a/libs/ContentTypes/Markdown/LinkRenderer.php b/libs/ContentTypes/Markdown/LinkRenderer.php index 38b3a07..67f5b54 100644 --- a/libs/ContentTypes/Markdown/LinkRenderer.php +++ b/libs/ContentTypes/Markdown/LinkRenderer.php @@ -38,15 +38,8 @@ class LinkRenderer implements InlineRendererInterface, ConfigurationAwareInterfa */ public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) { - // This can't be in the method type as - // the method is an abstract and should - // have the same interface - if (!$inline instanceof Link) { - throw new \RuntimeException( - 'Wrong type passed to ' . __CLASS__ . '::' . __METHOD__ . - " the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" . - get_class($inline) . "' was provided" - ); + if (!($inline instanceof Link)) { + throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline)); } $element = $this->parent->render($inline, $htmlRenderer); diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index f10ea20..0b54fea 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -3,6 +3,7 @@ use Todaymade\Daux\Exception\LinkNotFoundException; use Todaymade\Daux\Tree\Builder; use Todaymade\Daux\Tree\Directory; +use Todaymade\Daux\Tree\Entry; class DauxHelper { diff --git a/libs/Format/Confluence/ContentTypes/Markdown/LinkRenderer.php b/libs/Format/Confluence/ContentTypes/Markdown/LinkRenderer.php index b8b5257..8c0cf22 100644 --- a/libs/Format/Confluence/ContentTypes/Markdown/LinkRenderer.php +++ b/libs/Format/Confluence/ContentTypes/Markdown/LinkRenderer.php @@ -16,15 +16,8 @@ class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer */ public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) { - // This can't be in the method type as - // the method is an abstract and should - // have the same interface - if (!$inline instanceof Link) { - throw new \RuntimeException( - 'Wrong type passed to ' . __CLASS__ . '::' . __METHOD__ . - " the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" . - get_class($inline) . "' was provided" - ); + if (!($inline instanceof Link)) { + throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline)); } // Default handling @@ -34,7 +27,7 @@ class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer // empty urls, anchors and absolute urls // should not go through the url resolver - if (!$this->isValidUrl($url) || $this->isExternalUrl($url)) { + if (!DauxHelper::isValidUrl($url) || DauxHelper::isExternalUrl($url)) { return $element; } diff --git a/libs/Format/Confluence/ContentTypes/Markdown/TOCRenderer.php b/libs/Format/Confluence/ContentTypes/Markdown/TOCRenderer.php index 9e5ee90..6d2a3ef 100644 --- a/libs/Format/Confluence/ContentTypes/Markdown/TOCRenderer.php +++ b/libs/Format/Confluence/ContentTypes/Markdown/TOCRenderer.php @@ -3,11 +3,16 @@ use League\CommonMark\Block\Element\AbstractBlock; use League\CommonMark\Block\Renderer\BlockRendererInterface; use League\CommonMark\ElementRendererInterface; +use Todaymade\Daux\ContentTypes\Markdown\TableOfContents; class TOCRenderer implements BlockRendererInterface { public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false) { + if (!($block instanceof TableOfContents)) { + throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block)); + } + return ''; } } diff --git a/libs/Format/Confluence/Publisher.php b/libs/Format/Confluence/Publisher.php index 94a4832..ec13a4e 100644 --- a/libs/Format/Confluence/Publisher.php +++ b/libs/Format/Confluence/Publisher.php @@ -54,7 +54,7 @@ class Publisher $this->run( 'Getting already published pages...', - function () use (&$published) { + function() use (&$published) { if ($published != null) { $published['children'] = $this->client->getList($published['id'], true); } @@ -63,7 +63,7 @@ class Publisher $published = $this->run( "Create placeholder pages...", - function () use ($tree, $published) { + function() use ($tree, $published) { return $this->createRecursive($this->confluence['ancestor_id'], $tree, $published); } ); @@ -143,7 +143,7 @@ class Publisher protected function createRecursive($parent_id, $entry, $published) { - $callback = function ($parent_id, $entry, $published) { + $callback = function($parent_id, $entry, $published) { // nothing to do if the ID already exists if (array_key_exists('id', $published)) { return $published; @@ -164,7 +164,7 @@ class Publisher protected function updateRecursive($parent_id, $entry, $published) { - $callback = function ($parent_id, $entry, $published) { + $callback = function($parent_id, $entry, $published) { if (array_key_exists('id', $published) && array_key_exists('page', $entry)) { $this->updatePage($parent_id, $entry, $published); } @@ -182,7 +182,7 @@ class Publisher $this->run( '- ' . PublisherUtilities::niceTitle($entry['file']->getUrl()), - function () use ($entry, $published, $parent_id, $updateThreshold) { + function() use ($entry, $published, $parent_id, $updateThreshold) { $generated_content = $entry['page']->getContent(); if (PublisherUtilities::shouldUpdate($entry['page'], $generated_content, $published, $updateThreshold)) { $this->client->updatePage( @@ -200,7 +200,7 @@ class Publisher foreach ($entry['page']->attachments as $attachment) { $this->run( " With attachment: $attachment[filename]", - function ($write) use ($published, $attachment) { + function($write) use ($published, $attachment) { $this->client->uploadAttachment($published['id'], $attachment, $write); } ); diff --git a/libs/Format/HTML/ContentTypes/Markdown/ImageRenderer.php b/libs/Format/HTML/ContentTypes/Markdown/ImageRenderer.php index 26ef57e..79181aa 100644 --- a/libs/Format/HTML/ContentTypes/Markdown/ImageRenderer.php +++ b/libs/Format/HTML/ContentTypes/Markdown/ImageRenderer.php @@ -9,6 +9,7 @@ use League\CommonMark\Util\ConfigurationAwareInterface; use League\CommonMark\Util\ConfigurationInterface; use Todaymade\Daux\Config; use Todaymade\Daux\DauxHelper; +use Todaymade\Daux\Exception\LinkNotFoundException; class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterface { @@ -22,6 +23,11 @@ class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterf */ protected $config; + /** + * @var \League\CommonMark\Inline\Renderer\ImageRenderer + */ + protected $parent; + public function __construct($daux) { $this->daux = $daux; @@ -32,11 +38,10 @@ class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterf * Relative URLs can be done using either the folder with * number prefix or the final name (with prefix stripped). * This ensures that we always use the final name when generating. + * @throws LinkNotFoundException */ - protected function getCleanUrl(Image $element) + protected function getCleanUrl($url) { - $url = $element->getUrl(); - // empty urls and anchors should // not go through the url resolver if (!DauxHelper::isValidUrl($url)) { @@ -55,8 +60,6 @@ class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterf if ($this->daux->isStatic()) { throw $e; } - - $element->setAttribute('class', 'Link--broken'); } return $url; @@ -67,11 +70,16 @@ class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterf * @param ElementRendererInterface $htmlRenderer * * @return HtmlElement + * + * @throws LinkNotFoundException */ public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) { - $original = $inline->getUrl(); - $inline->setUrl($this->getCleanUrl($inline)); + if (!($inline instanceof Image)) { + throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline)); + } + + $inline->setUrl($this->getCleanUrl($inline->getUrl())); return $this->parent->render($inline, $htmlRenderer); } diff --git a/libs/Format/HTML/ContentTypes/Markdown/TOC/Processor.php b/libs/Format/HTML/ContentTypes/Markdown/TOC/Processor.php index 100ab42..995ad56 100644 --- a/libs/Format/HTML/ContentTypes/Markdown/TOC/Processor.php +++ b/libs/Format/HTML/ContentTypes/Markdown/TOC/Processor.php @@ -140,7 +140,7 @@ class Processor } } - $node->data['attributes']['id'] = $this->getUniqueId($document,'page_'. $this->escaped($text)); + $node->data['attributes']['id'] = $this->getUniqueId($document, 'page_'. $this->escaped($text)); } /** diff --git a/libs/Format/HTML/ContentTypes/Markdown/TOC/Renderer.php b/libs/Format/HTML/ContentTypes/Markdown/TOC/Renderer.php index e9b348d..14853a0 100644 --- a/libs/Format/HTML/ContentTypes/Markdown/TOC/Renderer.php +++ b/libs/Format/HTML/ContentTypes/Markdown/TOC/Renderer.php @@ -4,6 +4,7 @@ use League\CommonMark\Block\Element\AbstractBlock; use League\CommonMark\Block\Renderer\BlockRendererInterface; use League\CommonMark\ElementRendererInterface; use Todaymade\Daux\Config; +use Todaymade\Daux\ContentTypes\Markdown\TableOfContents; class Renderer implements BlockRendererInterface { @@ -19,6 +20,10 @@ class Renderer implements BlockRendererInterface public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false) { + if (!($block instanceof TableOfContents)) { + throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block)); + } + $content = $htmlRenderer->renderBlocks($block->children()); return $this->config->templateRenderer ->getEngine($this->config) diff --git a/libs/Format/HTML/Template.php b/libs/Format/HTML/Template.php index 2d133a0..1ab8ce9 100644 --- a/libs/Format/HTML/Template.php +++ b/libs/Format/HTML/Template.php @@ -77,13 +77,13 @@ class Template protected function registerFunctions($engine) { - $engine->registerFunction('get_navigation', function ($tree, $path, $current_url, $base_page, $mode) { + $engine->registerFunction('get_navigation', function($tree, $path, $current_url, $base_page, $mode) { $nav = $this->buildNavigation($tree, $path, $current_url, $base_page, $mode); return $this->renderNavigation($nav); }); - $engine->registerFunction('translate', function ($key) { + $engine->registerFunction('translate', function($key) { $language = $this->params['language']; if (isset($this->engine->getData('page')['page'])) { @@ -106,7 +106,7 @@ class Template return "Unknown key $key"; }); - $engine->registerFunction('get_breadcrumb_title', function ($page, $base_page) { + $engine->registerFunction('get_breadcrumb_title', function($page, $base_page) { $title = ''; $breadcrumb_trail = $page['breadcrumb_trail']; $separator = $this->getSeparator($page['breadcrumb_separator']); diff --git a/libs/Format/HTMLFile/ContentPage.php b/libs/Format/HTMLFile/ContentPage.php index e9825f6..a672b6f 100644 --- a/libs/Format/HTMLFile/ContentPage.php +++ b/libs/Format/HTMLFile/ContentPage.php @@ -11,19 +11,21 @@ class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage { $content = parent::generatePage(); - //Embed images + // Embed images // We do it after generation so we can catch the images that were in html already $content = (new EmbedImages($this->params['tree'])) ->embed( $content, $this->file, function($src, array $attributes, Raw $file) { - $content = base64_encode(file_get_contents($file->getPath())); + // TODO :: ignore absolute paths + $content = base64_encode(file_get_contents($file->getPath())); $attr = ''; foreach ($attributes as $name => $value) { $attr .= ' ' . $name . '="' . htmlentities($value, ENT_QUOTES, 'UTF-8', false) . '"'; } + // TODO :: handle other formats than PNG as well return ""; } ); diff --git a/libs/Format/HTMLFile/ContentTypes/Markdown/LinkRenderer.php b/libs/Format/HTMLFile/ContentTypes/Markdown/LinkRenderer.php index 22e7d73..3685f82 100644 --- a/libs/Format/HTMLFile/ContentTypes/Markdown/LinkRenderer.php +++ b/libs/Format/HTMLFile/ContentTypes/Markdown/LinkRenderer.php @@ -19,15 +19,8 @@ class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer */ public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) { - // This can't be in the method type as - // the method is an abstract and should - // have the same interface - if (!$inline instanceof Link) { - throw new \RuntimeException( - 'Wrong type passed to ' . __CLASS__ . '::' . __METHOD__ . - " the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" . - get_class($inline) . "' was provided" - ); + if (!($inline instanceof Link)) { + throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline)); } $element = parent::render($inline, $htmlRenderer); @@ -36,12 +29,12 @@ class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer // empty urls and anchors should // not go through the url resolver - if (!$this->isValidUrl($url)) { + if (!DauxHelper::isValidUrl($url)) { return $element; } // Absolute urls, shouldn't either - if ($this->isExternalUrl($url)) { + if (DauxHelper::isExternalUrl($url)) { $element->setAttribute('class', 'Link--external'); return $element; diff --git a/templates/home.php b/templates/home.php index e253793..0da40f7 100755 --- a/templates/home.php +++ b/templates/home.php @@ -10,11 +10,11 @@
- ' . $params['tagline'] . '' : '' ?> + ' . $params['tagline'] . '' : '' ?>
- ' : '' ?> + ' : '' ?>
@@ -27,8 +27,8 @@ foreach ($page['entry_page'] as $key => $node) { echo '' . str_replace("__VIEW_DOCUMENTATION__", $view_doc, $key) . ''; } - if(isset($params['html']['buttons']) && is_array($params['html']['buttons'])) { - foreach ($params['html']['buttons'] as $name => $link ) { + if (isset($params['html']['buttons']) && is_array($params['html']['buttons'])) { + foreach ($params['html']['buttons'] as $name => $link) { echo '' . $name . ''; } } diff --git a/templates/layout/00_layout.php b/templates/layout/00_layout.php index 4aa7d8d..e730533 100755 --- a/templates/layout/00_layout.php +++ b/templates/layout/00_layout.php @@ -1,7 +1,7 @@ - <?= $page['title']; ?> <?= ($page['title'] != $params['title'])? '- ' . $params['title'] : "" ?> + <?= $page['title']; ?> <?= ($page['title'] != $params['title']) ? '- ' . $params['title'] : "" ?> \n"; diff --git a/templates/partials/search_script.php b/templates/partials/search_script.php index 6f6b428..e31f57e 100644 --- a/templates/partials/search_script.php +++ b/templates/partials/search_script.php @@ -13,7 +13,7 @@ "Link_next", ]; $search_translations = []; - foreach($search_strings as $key) { + foreach ($search_strings as $key) { $search_translations[$key] = $this->translate($key); } ?>