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