Skip to content

Commit

Permalink
Corrected usages of exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
danbettles committed Sep 28, 2022
1 parent 36f8c54 commit d0c6c57
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/OutputHelper/Html5OutputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace DanBettles\Marigold\OutputHelper;

use BadMethodCallException;
use RangeException;
use InvalidArgumentException;
use ReflectionMethod;

use function array_unshift;
Expand Down Expand Up @@ -82,7 +82,7 @@ public function createAttributes(array $attributes): string
}

/**
* @throws RangeException If content was passed when creating a void element.
* @throws InvalidArgumentException If content was passed when creating a void element.
*/
protected function createElement(
string $tagName,
Expand All @@ -92,7 +92,7 @@ protected function createElement(
// See https://html.spec.whatwg.org/multipage/syntax.html#void-elements
if (in_array($tagName, self::TAG_NAMES_BY_TYPE['void'])) {
if (null !== $content) {
throw new RangeException('Content was passed: a void element may not have content.');
throw new InvalidArgumentException('Content was passed: a void element may not have content.');
}

$attributesStr = $this->createAttributes($attributes);
Expand Down
4 changes: 3 additions & 1 deletion src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DanBettles\Marigold;

use InvalidArgumentException;
use OutOfBoundsException;

use function array_combine;
use function array_filter;
Expand Down Expand Up @@ -81,12 +82,13 @@ private function eliminateUnmatchableRoutes(string $path, array $routes): array
/**
* @param array<string, string> $serverVars
* @return array{path: string, action: mixed, parameters: string[]}|null
* @throws OutOfBoundsException If there is no request URI in the server vars.
* @throws InvalidArgumentException If the request URI is invalid.
*/
public function match(array $serverVars): ?array
{
if (!array_key_exists('REQUEST_URI', $serverVars)) {
throw new InvalidArgumentException('There is no request URI in the server vars.');
throw new OutOfBoundsException('There is no request URI in the server vars.');
}

$requestUri = $serverVars['REQUEST_URI'];
Expand Down
10 changes: 5 additions & 5 deletions src/TemplateEngine/TemplateFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace DanBettles\Marigold\TemplateEngine;

use InvalidArgumentException;
use RangeException;
use SplFileInfo;

use function is_dir;
Expand Down Expand Up @@ -69,12 +68,12 @@ public function findTemplate($pathnameOrFileInfo): ?TemplateFile
}

/**
* @throws RangeException If the directory does not exist.
* @throws InvalidArgumentException If the directory does not exist.
*/
private function addTemplateDir(string $dir): self
{
if (!is_dir($dir)) {
throw new RangeException("The directory `{$dir}` does not exist.");
throw new InvalidArgumentException("The directory `{$dir}` does not exist.");
}

$this->templateDirs[] = $dir;
Expand All @@ -84,12 +83,13 @@ private function addTemplateDir(string $dir): self

/**
* @param string[] $dirs
* @throws RangeException If the array of directory paths is empty.
* @throws InvalidArgumentException If the array of directory paths is empty.
*/
private function setTemplateDirs(array $dirs): self
{
if (!$dirs) {
throw new RangeException('The array of directory paths is empty.');
// @todo Really throw an exception? Is this really a problem?
throw new InvalidArgumentException('The array of directory paths is empty.');
}

$this->templateDirs = [];
Expand Down
4 changes: 2 additions & 2 deletions tests/src/OutputHelper/Html5OutputHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use DanBettles\Marigold\AbstractTestCase;
use DanBettles\Marigold\OutputHelper\Html5OutputHelper;
use DanBettles\Marigold\OutputHelper\XmlOutputHelper;
use RangeException;
use InvalidArgumentException;

use const false;
use const true;
Expand Down Expand Up @@ -140,7 +140,7 @@ public function testCreateelCanCreateElementsWithBooleanAttributes(

public function testCreateelThrowsAnExceptionIfAnAttemptIsMadeToCreateAVoidElementWithContent(): void
{
$this->expectException(RangeException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Content was passed: a void element may not have content.');

(new Html5OutputHelper())->createEl('br', 'foo');
Expand Down
3 changes: 2 additions & 1 deletion tests/src/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DanBettles\Marigold\AbstractTestCase;
use DanBettles\Marigold\Router;
use InvalidArgumentException;
use OutOfBoundsException;

class RouterTest extends AbstractTestCase
{
Expand Down Expand Up @@ -245,7 +246,7 @@ public function providesInvalidRequestUris(): array

public function testMatchThrowsAnExceptionIfTheServerVarsDoNotContainTheRequestUri(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(OutOfBoundsException::class);
$this->expectExceptionMessage('There is no request URI in the server vars.');

(new Router([]))
Expand Down
6 changes: 2 additions & 4 deletions tests/src/TemplateEngine/TemplateFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use DanBettles\Marigold\TemplateEngine\TemplateFile;
use DanBettles\Marigold\TemplateEngine\TemplateFileLoader;
use InvalidArgumentException;
use RangeException;
use SplFileInfo;

use function array_reverse;
Expand All @@ -32,10 +31,9 @@ public function testIsConstructedUsingOneOrMoreDirectoryPaths(): void
$this->assertEquals($paths, $loader->getTemplateDirs());
}

// @todo Review this.
public function testThrowsAnExceptionIfTheArrayOfDirectoryPathsIsEmpty(): void
{
$this->expectException(RangeException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The array of directory paths is empty.');

new TemplateFileLoader([]);
Expand All @@ -45,7 +43,7 @@ public function testThrowsAnExceptionIfADirectoryDoesNotExist(): void
{
$nonExistentDir = $this->createFixturePathname(__FUNCTION__ . '/non_existent/');

$this->expectException(RangeException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("The directory `{$nonExistentDir}` does not exist.");

new TemplateFileLoader([
Expand Down

0 comments on commit d0c6c57

Please sign in to comment.