Fix warnings and errors reported by Scrutinizer

This commit is contained in:
Stéphane Goetz 2019-11-30 22:24:10 +01:00
parent abaf36bec6
commit 688de1d5b9
15 changed files with 56 additions and 56 deletions

View File

@ -19,7 +19,7 @@ trait RunAction
$padding = $width - $this->getLength($title) - 10; $padding = $width - $this->getLength($title) - 10;
try { try {
$response = $closure(function ($content) use (&$padding, $verbose) { $response = $closure(function($content) use (&$padding, $verbose) {
$padding -= $this->getLength($content); $padding -= $this->getLength($content);
Daux::write($content, $verbose); Daux::write($content, $verbose);
}); });

View File

@ -38,15 +38,8 @@ class LinkRenderer implements InlineRendererInterface, ConfigurationAwareInterfa
*/ */
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
{ {
// This can't be in the method type as if (!($inline instanceof Link)) {
// the method is an abstract and should throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
// 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"
);
} }
$element = $this->parent->render($inline, $htmlRenderer); $element = $this->parent->render($inline, $htmlRenderer);

View File

@ -3,6 +3,7 @@
use Todaymade\Daux\Exception\LinkNotFoundException; use Todaymade\Daux\Exception\LinkNotFoundException;
use Todaymade\Daux\Tree\Builder; use Todaymade\Daux\Tree\Builder;
use Todaymade\Daux\Tree\Directory; use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Entry;
class DauxHelper class DauxHelper
{ {

View File

@ -16,15 +16,8 @@ class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer
*/ */
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
{ {
// This can't be in the method type as if (!($inline instanceof Link)) {
// the method is an abstract and should throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
// 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"
);
} }
// Default handling // Default handling
@ -34,7 +27,7 @@ class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer
// empty urls, anchors and absolute urls // empty urls, anchors and absolute urls
// should not go through the url resolver // should not go through the url resolver
if (!$this->isValidUrl($url) || $this->isExternalUrl($url)) { if (!DauxHelper::isValidUrl($url) || DauxHelper::isExternalUrl($url)) {
return $element; return $element;
} }

View File

@ -3,11 +3,16 @@
use League\CommonMark\Block\Element\AbstractBlock; use League\CommonMark\Block\Element\AbstractBlock;
use League\CommonMark\Block\Renderer\BlockRendererInterface; use League\CommonMark\Block\Renderer\BlockRendererInterface;
use League\CommonMark\ElementRendererInterface; use League\CommonMark\ElementRendererInterface;
use Todaymade\Daux\ContentTypes\Markdown\TableOfContents;
class TOCRenderer implements BlockRendererInterface class TOCRenderer implements BlockRendererInterface
{ {
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false) public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
{ {
if (!($block instanceof TableOfContents)) {
throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
}
return '<ac:structured-macro ac:name="toc"></ac:structured-macro>'; return '<ac:structured-macro ac:name="toc"></ac:structured-macro>';
} }
} }

View File

@ -54,7 +54,7 @@ class Publisher
$this->run( $this->run(
'Getting already published pages...', 'Getting already published pages...',
function () use (&$published) { function() use (&$published) {
if ($published != null) { if ($published != null) {
$published['children'] = $this->client->getList($published['id'], true); $published['children'] = $this->client->getList($published['id'], true);
} }
@ -63,7 +63,7 @@ class Publisher
$published = $this->run( $published = $this->run(
"Create placeholder pages...", "Create placeholder pages...",
function () use ($tree, $published) { function() use ($tree, $published) {
return $this->createRecursive($this->confluence['ancestor_id'], $tree, $published); return $this->createRecursive($this->confluence['ancestor_id'], $tree, $published);
} }
); );
@ -143,7 +143,7 @@ class Publisher
protected function createRecursive($parent_id, $entry, $published) 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 // nothing to do if the ID already exists
if (array_key_exists('id', $published)) { if (array_key_exists('id', $published)) {
return $published; return $published;
@ -164,7 +164,7 @@ class Publisher
protected function updateRecursive($parent_id, $entry, $published) 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)) { if (array_key_exists('id', $published) && array_key_exists('page', $entry)) {
$this->updatePage($parent_id, $entry, $published); $this->updatePage($parent_id, $entry, $published);
} }
@ -182,7 +182,7 @@ class Publisher
$this->run( $this->run(
'- ' . PublisherUtilities::niceTitle($entry['file']->getUrl()), '- ' . PublisherUtilities::niceTitle($entry['file']->getUrl()),
function () use ($entry, $published, $parent_id, $updateThreshold) { function() use ($entry, $published, $parent_id, $updateThreshold) {
$generated_content = $entry['page']->getContent(); $generated_content = $entry['page']->getContent();
if (PublisherUtilities::shouldUpdate($entry['page'], $generated_content, $published, $updateThreshold)) { if (PublisherUtilities::shouldUpdate($entry['page'], $generated_content, $published, $updateThreshold)) {
$this->client->updatePage( $this->client->updatePage(
@ -200,7 +200,7 @@ class Publisher
foreach ($entry['page']->attachments as $attachment) { foreach ($entry['page']->attachments as $attachment) {
$this->run( $this->run(
" With attachment: $attachment[filename]", " With attachment: $attachment[filename]",
function ($write) use ($published, $attachment) { function($write) use ($published, $attachment) {
$this->client->uploadAttachment($published['id'], $attachment, $write); $this->client->uploadAttachment($published['id'], $attachment, $write);
} }
); );

View File

@ -9,6 +9,7 @@ use League\CommonMark\Util\ConfigurationAwareInterface;
use League\CommonMark\Util\ConfigurationInterface; use League\CommonMark\Util\ConfigurationInterface;
use Todaymade\Daux\Config; use Todaymade\Daux\Config;
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception\LinkNotFoundException;
class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterface class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterface
{ {
@ -22,6 +23,11 @@ class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterf
*/ */
protected $config; protected $config;
/**
* @var \League\CommonMark\Inline\Renderer\ImageRenderer
*/
protected $parent;
public function __construct($daux) public function __construct($daux)
{ {
$this->daux = $daux; $this->daux = $daux;
@ -32,11 +38,10 @@ class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterf
* Relative URLs can be done using either the folder with * Relative URLs can be done using either the folder with
* number prefix or the final name (with prefix stripped). * number prefix or the final name (with prefix stripped).
* This ensures that we always use the final name when generating. * 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 // empty urls and anchors should
// not go through the url resolver // not go through the url resolver
if (!DauxHelper::isValidUrl($url)) { if (!DauxHelper::isValidUrl($url)) {
@ -55,8 +60,6 @@ class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterf
if ($this->daux->isStatic()) { if ($this->daux->isStatic()) {
throw $e; throw $e;
} }
$element->setAttribute('class', 'Link--broken');
} }
return $url; return $url;
@ -67,11 +70,16 @@ class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterf
* @param ElementRendererInterface $htmlRenderer * @param ElementRendererInterface $htmlRenderer
* *
* @return HtmlElement * @return HtmlElement
*
* @throws LinkNotFoundException
*/ */
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
{ {
$original = $inline->getUrl(); if (!($inline instanceof Image)) {
$inline->setUrl($this->getCleanUrl($inline)); throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
}
$inline->setUrl($this->getCleanUrl($inline->getUrl()));
return $this->parent->render($inline, $htmlRenderer); return $this->parent->render($inline, $htmlRenderer);
} }

View File

@ -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));
} }
/** /**

View File

@ -4,6 +4,7 @@ use League\CommonMark\Block\Element\AbstractBlock;
use League\CommonMark\Block\Renderer\BlockRendererInterface; use League\CommonMark\Block\Renderer\BlockRendererInterface;
use League\CommonMark\ElementRendererInterface; use League\CommonMark\ElementRendererInterface;
use Todaymade\Daux\Config; use Todaymade\Daux\Config;
use Todaymade\Daux\ContentTypes\Markdown\TableOfContents;
class Renderer implements BlockRendererInterface class Renderer implements BlockRendererInterface
{ {
@ -19,6 +20,10 @@ class Renderer implements BlockRendererInterface
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false) 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()); $content = $htmlRenderer->renderBlocks($block->children());
return $this->config->templateRenderer return $this->config->templateRenderer
->getEngine($this->config) ->getEngine($this->config)

View File

@ -77,13 +77,13 @@ class Template
protected function registerFunctions($engine) 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); $nav = $this->buildNavigation($tree, $path, $current_url, $base_page, $mode);
return $this->renderNavigation($nav); return $this->renderNavigation($nav);
}); });
$engine->registerFunction('translate', function ($key) { $engine->registerFunction('translate', function($key) {
$language = $this->params['language']; $language = $this->params['language'];
if (isset($this->engine->getData('page')['page'])) { if (isset($this->engine->getData('page')['page'])) {
@ -106,7 +106,7 @@ class Template
return "Unknown key $key"; return "Unknown key $key";
}); });
$engine->registerFunction('get_breadcrumb_title', function ($page, $base_page) { $engine->registerFunction('get_breadcrumb_title', function($page, $base_page) {
$title = ''; $title = '';
$breadcrumb_trail = $page['breadcrumb_trail']; $breadcrumb_trail = $page['breadcrumb_trail'];
$separator = $this->getSeparator($page['breadcrumb_separator']); $separator = $this->getSeparator($page['breadcrumb_separator']);

View File

@ -11,19 +11,21 @@ class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage
{ {
$content = parent::generatePage(); $content = parent::generatePage();
//Embed images // Embed images
// We do it after generation so we can catch the images that were in html already // We do it after generation so we can catch the images that were in html already
$content = (new EmbedImages($this->params['tree'])) $content = (new EmbedImages($this->params['tree']))
->embed( ->embed(
$content, $content,
$this->file, $this->file,
function($src, array $attributes, Raw $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 = ''; $attr = '';
foreach ($attributes as $name => $value) { foreach ($attributes as $name => $value) {
$attr .= ' ' . $name . '="' . htmlentities($value, ENT_QUOTES, 'UTF-8', false) . '"'; $attr .= ' ' . $name . '="' . htmlentities($value, ENT_QUOTES, 'UTF-8', false) . '"';
} }
// TODO :: handle other formats than PNG as well
return "<img $attr src=\"data:image/png;base64,$content\"/>"; return "<img $attr src=\"data:image/png;base64,$content\"/>";
} }
); );

View File

@ -19,15 +19,8 @@ class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer
*/ */
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
{ {
// This can't be in the method type as if (!($inline instanceof Link)) {
// the method is an abstract and should throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
// 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"
);
} }
$element = parent::render($inline, $htmlRenderer); $element = parent::render($inline, $htmlRenderer);
@ -36,12 +29,12 @@ class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer
// empty urls and anchors should // empty urls and anchors should
// not go through the url resolver // not go through the url resolver
if (!$this->isValidUrl($url)) { if (!DauxHelper::isValidUrl($url)) {
return $element; return $element;
} }
// Absolute urls, shouldn't either // Absolute urls, shouldn't either
if ($this->isExternalUrl($url)) { if (DauxHelper::isExternalUrl($url)) {
$element->setAttribute('class', 'Link--external'); $element->setAttribute('class', 'Link--external');
return $element; return $element;

View File

@ -10,11 +10,11 @@
<div class="Homepage"> <div class="Homepage">
<div class="HomepageTitle Container"> <div class="HomepageTitle Container">
<?= ($params['tagline'])? '<h2>' . $params['tagline'] . '</h2>' : '' ?> <?= ($params['tagline']) ? '<h2>' . $params['tagline'] . '</h2>' : '' ?>
</div> </div>
<div class="HomepageImage Container"> <div class="HomepageImage Container">
<?= ($params['image'])? '<img class="homepage-image img-responsive" src="' . $params['image'] . '" alt="' . $params['title'] . '">' : '' ?> <?= ($params['image']) ? '<img class="homepage-image img-responsive" src="' . $params['image'] . '" alt="' . $params['title'] . '">' : '' ?>
</div> </div>
<div class="HomepageButtons"> <div class="HomepageButtons">
@ -27,8 +27,8 @@
foreach ($page['entry_page'] as $key => $node) { foreach ($page['entry_page'] as $key => $node) {
echo '<a href="' . $node . '" class="Button Button--primary Button--hero">' . str_replace("__VIEW_DOCUMENTATION__", $view_doc, $key) . '</a>'; echo '<a href="' . $node . '" class="Button Button--primary Button--hero">' . str_replace("__VIEW_DOCUMENTATION__", $view_doc, $key) . '</a>';
} }
if(isset($params['html']['buttons']) && is_array($params['html']['buttons'])) { if (isset($params['html']['buttons']) && is_array($params['html']['buttons'])) {
foreach ($params['html']['buttons'] as $name => $link ) { foreach ($params['html']['buttons'] as $name => $link) {
echo '<a href="' . $link . '" class="Button Button--secondary Button--hero">' . $name . '</a>'; echo '<a href="' . $link . '" class="Button Button--secondary Button--hero">' . $name . '</a>';
} }
} }

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html class="no-js" lang="<?=$params['language'] ?>"> <html class="no-js" lang="<?=$params['language'] ?>">
<head> <head>
<title><?= $page['title']; ?> <?= ($page['title'] != $params['title'])? '- ' . $params['title'] : "" ?></title> <title><?= $page['title']; ?> <?= ($page['title'] != $params['title']) ? '- ' . $params['title'] : "" ?></title>
<?php //SEO meta tags... <?php //SEO meta tags...
if (array_key_exists('attributes', $page) && array_key_exists('description', $page['attributes'])) { if (array_key_exists('attributes', $page) && array_key_exists('description', $page['attributes'])) {
echo " <meta name=\"description\" content=\"{$page['attributes']['description']}\">\n"; echo " <meta name=\"description\" content=\"{$page['attributes']['description']}\">\n";

View File

@ -13,7 +13,7 @@
"Link_next", "Link_next",
]; ];
$search_translations = []; $search_translations = [];
foreach($search_strings as $key) { foreach ($search_strings as $key) {
$search_translations[$key] = $this->translate($key); $search_translations[$key] = $this->translate($key);
} }
?> ?>