Remove directory arg checks, add braces where missing on conditionals

This commit is contained in:
2023-05-25 14:16:38 +01:00
parent a34d2c5dca
commit 2b1c7b9c12

View File

@@ -22,23 +22,16 @@ final class IconExtension extends AbstractExtension
public function __construct(private array $directories, private array $palletes)
{
if (empty($this->directories))
throw new \InvalidArgumentException('Directories array must contain at least one path!');
$dirsContainNonString = array_reduce($this->directories,
fn($notString, $path) => $notString || !is_string($path));
if ($dirsContainNonString)
throw new \TypeError('Directories array must only contain strings!');
if (empty($this->palletes))
if (empty($this->palletes)) {
throw new \InvalidArgumentException('Palletes array must contain at least one pallet!');
}
$palletesContainNonarray = array_reduce($this->palletes,
fn($notArray, $path) => $notArray || !is_array($path));
if ($palletesContainNonarray)
if ($palletesContainNonarray) {
throw new \TypeError('Palletes array must only contain arrays!');
}
foreach ($this->palletes as $pallete) {
if (!(
@@ -115,8 +108,9 @@ final class IconExtension extends AbstractExtension
private function throwIfUnrecognisedOptionExists(array $options): void
{
foreach (array_keys($options) as $key) {
if (!key_exists($key, self::DEFAULT_OPTIONS))
if (!key_exists($key, self::DEFAULT_OPTIONS)) {
throw new \InvalidArgumentException("Unrecognised option '{$key}'!");
}
}
}
@@ -126,6 +120,7 @@ final class IconExtension extends AbstractExtension
$rawSvgMarkup = $this->getSvgMarkup($iconFilepath);
$cleanSvgMarkup = $this->removeExistingTitleElement($rawSvgMarkup);
$cleanSvgMarkup = $this->removeBlackStrokeAttributes($cleanSvgMarkup);
return $this->removeBlackFillAttributes($cleanSvgMarkup);
}
@@ -134,8 +129,9 @@ final class IconExtension extends AbstractExtension
foreach ($this->directories as $directory) {
$potentialFilepath = sprintf('%s/%s.svg', $directory, $iconName);
if (file_exists($potentialFilepath))
if (file_exists($potentialFilepath)) {
return $potentialFilepath;
}
}
throw new IconNotFound(sprintf('File "%s.svg" not found in %s', $iconName, implode(', ', $this->directories)));
@@ -176,8 +172,9 @@ final class IconExtension extends AbstractExtension
private function getPallete(string $palleteName): array
{
if (array_key_exists($palleteName, $this->palletes))
if (array_key_exists($palleteName, $this->palletes)) {
return $this->palletes[$palleteName];
}
throw new PalleteNotFound("The pallete '$palleteName' was not found!");
}
@@ -191,7 +188,10 @@ final class IconExtension extends AbstractExtension
private function addTitleToSvgIfNotNull(string $svg, ?string $title): string
{
if (null === $title) return $svg;
if (null === $title) {
return $svg;
}
$this->throwIfTitleIsEmpty($title);
return preg_replace('/(<svg(.|\n)*?>\n?)/', "$1<title>$title</title>", $svg);
@@ -215,8 +215,9 @@ final class IconExtension extends AbstractExtension
private function throwIfTitleIsEmpty(string $title): void
{
if ('' === $title)
if ('' === $title) {
throw new \InvalidArgumentException('Title string must not be empty!');
}
}
private function setSvgHeightAndWidth(string $content, int $size): string
@@ -230,8 +231,9 @@ final class IconExtension extends AbstractExtension
private function throwIfSizeIsNegative(int $size): void
{
if ($size < 0)
if ($size < 0) {
throw new \InvalidArgumentException('Size must not be negative');
}
}
}