Skip to content

Commit

Permalink
Fix more sonarqube warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Dec 11, 2022
1 parent 5b7c008 commit d8087af
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 43 deletions.
2 changes: 1 addition & 1 deletion libs/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private function resolveThemeVariant()
}

if (!is_dir($themesPath . $theme)) {
throw new \RuntimeException("Theme '{$theme}' not found");
throw new ConfigurationException("Theme '{$theme}' not found");
}

return [$theme, $variant];
Expand Down
5 changes: 5 additions & 0 deletions libs/ConfigurationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php namespace Todaymade\Daux;

class ConfigurationException extends Exception
{
}
3 changes: 2 additions & 1 deletion libs/ContentTypes/ContentTypeHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Todaymade\Daux\ContentTypes;

use Todaymade\Daux\Exception;
use Todaymade\Daux\Tree\Content;

class ContentTypeHandler
Expand Down Expand Up @@ -48,6 +49,6 @@ public function getType(Content $node)
}
}

throw new \RuntimeException("no contentType found for $path");
throw new Exception("no contentType found for $path");
}
}
2 changes: 1 addition & 1 deletion libs/ContentTypes/Markdown/LinkRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use League\Config\ConfigurationInterface;
use Todaymade\Daux\Config;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception\LinkNotFoundException;
use Todaymade\Daux\LinkNotFoundException;

class LinkRenderer implements NodeRendererInterface, ConfigurationAwareInterface
{
Expand Down
18 changes: 10 additions & 8 deletions libs/Daux.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function getGenerators()
*
* @return null|string
*
* @throws \RuntimeException
* @throws ConfigurationException
*/
public function getProcessorClass()
{
Expand All @@ -160,11 +160,13 @@ public function getProcessorClass()
}

if (!class_exists($processor)) {
throw new \RuntimeException("Class '$processor' not found. We cannot use it as a Processor");
throw new ConfigurationException("Class '$processor' not found. We cannot use it as a Processor");
}

if (!array_key_exists('Todaymade\\Daux\\Processor', class_parents($processor))) {
throw new \RuntimeException("Class '$processor' invalid, should extend '\\Todaymade\\Daux\\Processor'");
throw new ConfigurationException(
"Class '$processor' invalid, should extend '\\Todaymade\\Daux\\Processor'"
);
}

return $processor;
Expand Down Expand Up @@ -211,17 +213,17 @@ public function getGenerator()
$message .= "\n\nDid you mean one of these?\n " . implode("\n ", $alternatives);
}

throw new \RuntimeException($message);
throw new ConfigurationException($message);
}

$class = $generators[$format];
if (!class_exists($class)) {
throw new \RuntimeException("Class '$class' not found. We cannot use it as a Generator");
throw new ConfigurationException("Class '$class' not found. We cannot use it as a Generator");
}

$interface = 'Todaymade\Daux\Format\Base\Generator';
if (!in_array('Todaymade\Daux\Format\Base\Generator', class_implements($class))) {
throw new \RuntimeException("The class '$class' does not implement the '$interface' interface");
throw new ConfigurationException("The class '$class' does not implement the '$interface' interface");
}

return $this->generator = new $class($this);
Expand Down Expand Up @@ -270,7 +272,7 @@ public static function getOutput()
*
* @param array|string $messages The message as an array of lines or a single string
* @param bool $newline Whether to add a newline
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants)
*/
public static function write($messages, $newline = false, $options = 0)
{
Expand All @@ -281,7 +283,7 @@ public static function write($messages, $newline = false, $options = 0)
* Writes a message to the output and adds a newline at the end.
*
* @param array|string $messages The message as an array of lines of a single string
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants)
*/
public static function writeln($messages, $options = 0)
{
Expand Down
1 change: 0 additions & 1 deletion libs/DauxHelper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php namespace Todaymade\Daux;

use Todaymade\Daux\Exception\LinkNotFoundException;
use Todaymade\Daux\Tree\Builder;
use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Entry;
Expand Down
5 changes: 0 additions & 5 deletions libs/Exception/LinkNotFoundException.php

This file was deleted.

7 changes: 7 additions & 0 deletions libs/Format/Confluence/ConfluenceConfigurationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php namespace Todaymade\Daux\Format\Confluence;

use Todaymade\Daux\Exception;

class ConfluenceConfigurationException extends Exception
{
}
3 changes: 2 additions & 1 deletion libs/Format/Confluence/ContentPage.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Todaymade\Daux\Format\Confluence;

use Todaymade\Daux\Exception;
use Todaymade\Daux\Format\Base\EmbedImages;
use Todaymade\Daux\Tree\ComputedRaw;
use Todaymade\Daux\Tree\Entry;
Expand Down Expand Up @@ -28,7 +29,7 @@ function ($src, array $attributes, Entry $file) {
$filename = $file->getUri();
$this->attachments[$filename] = ['filename' => $filename, 'content' => $file->getContent()];
} else {
throw new \RuntimeException("Cannot embed image as we don't understand its type.");
throw new Exception("Cannot embed image as we don't understand its type.");
}

return $this->createImageTag($filename, $attributes);
Expand Down
4 changes: 3 additions & 1 deletion libs/Format/Confluence/DuplicateTitleException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace Todaymade\Daux\Format\Confluence;

class DuplicateTitleException extends \RuntimeException
use Todaymade\Daux\Exception;

class DuplicateTitleException extends Exception
{
}
8 changes: 5 additions & 3 deletions libs/Format/Confluence/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function checkConfiguration()
$confluence = $config->getConfluenceConfiguration();

if ($confluence == null) {
throw new \RuntimeException('You must specify your Confluence configuration');
throw new ConfluenceConfigurationException('You must specify your Confluence configuration');
}

$mandatory = ['base_url', 'user', 'pass', 'prefix'];
Expand All @@ -43,11 +43,13 @@ public function checkConfiguration()
if (count($errors)) {
$message = "The following options are mandatory for confluence : '" . implode("', '", $errors) . "'";

throw new \RuntimeException($message);
throw new ConfluenceConfigurationException($message);
}

if (!$confluence->hasAncestorId() && !$confluence->hasRootId()) {
throw new \RuntimeException("You must specify an 'ancestor_id' or a 'root_id' for confluence.");
throw new ConfluenceConfigurationException(
"You must specify an 'ancestor_id' or a 'root_id' for confluence."
);
}
}

Expand Down
14 changes: 11 additions & 3 deletions libs/Format/Confluence/Publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public function diff($local, $remote, $level)
if ($local && array_key_exists('children', $local)) {
$remoteChildren = $remote && array_key_exists('children', $remote) ? $remote['children'] : [];
foreach ($local['children'] as $title => $content) {
$this->diff($content, array_key_exists($title, $remoteChildren) ? $remoteChildren[$title] : null, "$level ");
$this->diff(
$content,
array_key_exists($title, $remoteChildren) ? $remoteChildren[$title] : null,
"$level "
);
}
}

Expand Down Expand Up @@ -117,14 +121,18 @@ protected function getRootPage($tree)
array_map(function ($page) { return $page['title']; }, $pages)
);

throw new \RuntimeException("Could not find a page named '$rootTitle' but found ['$pageNames'].");
throw new ConfluenceConfigurationException(
"Could not find a page named '$rootTitle' but found ['$pageNames']."
);
}

if ($this->confluence->hasRootId()) {
return $this->client->getPage($this->confluence->getRootId());
}

throw new \RuntimeException('You must at least specify a `root_id` or `ancestor_id` in your confluence configuration.');
throw new ConfluenceConfigurationException(
'You must at least specify a `root_id` or `ancestor_id` in your confluence configuration.'
);
}

protected function createPage($parentId, $entry, $published)
Expand Down
2 changes: 1 addition & 1 deletion libs/Format/HTML/ContentTypes/Markdown/ImageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use League\Config\ConfigurationInterface;
use Todaymade\Daux\Config;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception\LinkNotFoundException;
use Todaymade\Daux\LinkNotFoundException;

class ImageRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception\LinkNotFoundException;
use Todaymade\Daux\LinkNotFoundException;

class LinkRenderer extends \Todaymade\Daux\ContentTypes\Markdown\LinkRenderer
{
Expand Down
2 changes: 1 addition & 1 deletion libs/GeneratorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function copyRecursive($source, $destination)
$dir = opendir($source);

if ($dir === false) {
throw new \RuntimeException("Cannot copy '$source' to '$destination'");
throw new Exception("Cannot copy '$source' to '$destination'");
}

while (false !== ($file = readdir($dir))) {
Expand Down
5 changes: 5 additions & 0 deletions libs/LinkNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php namespace Todaymade\Daux;

class LinkNotFoundException extends Exception
{
}
6 changes: 3 additions & 3 deletions libs/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private function getTemporaryFile($postfix)
{
$sysFileName = tempnam(sys_get_temp_dir(), 'daux');
if ($sysFileName === false) {
throw new \RuntimeException('Could not create temporary file');
throw new Exception('Could not create temporary file');
}

$newFileName = $sysFileName . $postfix;
Expand All @@ -91,7 +91,7 @@ private function getTemporaryFile($postfix)
return $newFileName;
}

throw new \RuntimeException('Could not create temporary file');
throw new Exception('Could not create temporary file');
}

/**
Expand Down Expand Up @@ -194,7 +194,7 @@ private function getPage($request)
$generator = $this->daux->getGenerator();

if (!$generator instanceof LiveGenerator) {
throw new \RuntimeException(
throw new Exception(
"The generator '" . get_class($generator) . "' does not implement the interface " .
"'Todaymade\\Daux\\Format\\Base\\LiveGenerator' and thus doesn't support live rendering."
);
Expand Down
9 changes: 5 additions & 4 deletions libs/Tree/Builder.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php namespace Todaymade\Daux\Tree;

use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception;

class Builder
{
protected static $IGNORED = [
protected static $ignoredPaths = [
// Popular VCS Systems
'.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg',

Expand All @@ -16,7 +17,7 @@ protected static function isIgnored(\SplFileInfo $file, $ignore)
{
$filename = $file->getFilename();

if (in_array($filename, static::$IGNORED)) {
if (in_array($filename, static::$ignoredPaths)) {
return true;
}

Expand Down Expand Up @@ -44,7 +45,7 @@ protected static function getName($path)
preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m);

if (!isset($m[3])) {
throw new \RuntimeException('Name not found');
throw new Exception('Name not found');
}

return $m[3];
Expand Down Expand Up @@ -143,7 +144,7 @@ public static function createContent(Directory $parent, \SplFileInfo $file)
*/
public static function removeSortingInformations($filename)
{
preg_match('/^[-+]?[0-9]*_?(.*)/', $filename, $matches);
preg_match('/^[-+]?\d*_?(.*)/', $filename, $matches);

// Remove the numeric part
// of the filename, only if
Expand Down
5 changes: 3 additions & 2 deletions libs/Tree/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use League\CommonMark\Extension\FrontMatter\Data\SymfonyYamlFrontMatterParser;
use League\CommonMark\Extension\FrontMatter\Exception\InvalidFrontMatterException;
use League\CommonMark\Extension\FrontMatter\FrontMatterParser;
use Todaymade\Daux\Exception;

class Content extends ContentAbstract
{
Expand All @@ -28,7 +29,7 @@ protected function getFrontMatter()
if ($this->manuallySetContent) {
$content = $this->content;
} elseif (!$this->getPath()) {
throw new \RuntimeException('Empty content');
throw new Exception('Empty content');
} else {
$content = file_get_contents($this->getPath());
}
Expand Down Expand Up @@ -120,7 +121,7 @@ protected function parseAttributes()
$file = $this->getUrl();
}

throw new \RuntimeException('Could not parse front matter in ' . $file, 0, $e);
throw new Exception('Could not parse front matter in "' . $file . '"', 0, $e);
}
}

Expand Down
10 changes: 4 additions & 6 deletions libs/Tree/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function removeChild(Entry $entry): void
public function getConfig(): Config
{
if (!$this->parent) {
throw new \RuntimeException('Could not retrieve configuration. Are you sure that your tree has a Root ?');
throw new TreeException('Could not retrieve configuration. Are you sure that your tree has a Root ?');
}

return $this->parent->getConfig();
Expand Down Expand Up @@ -224,10 +224,8 @@ public function hasContent(): bool
if ($node instanceof Content) {
return true;
}
if ($node instanceof self) {
if ($node->hasContent()) {
return true;
}
if ($node instanceof self && $node->hasContent()) {
return true;
}
}

Expand Down Expand Up @@ -281,7 +279,7 @@ public function offsetGet($offset): mixed
public function offsetSet($offset, $value): void
{
if (!$value instanceof Entry) {
throw new \RuntimeException('The value is not of type Entry');
throw new TreeException('The value is not of type Entry');
}

$this->addChild($value);
Expand Down
7 changes: 7 additions & 0 deletions libs/Tree/TreeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php namespace Todaymade\Daux\Tree;

use Todaymade\Daux\Exception;

class TreeException extends Exception
{
}

0 comments on commit d8087af

Please sign in to comment.