Skip to content

Commit

Permalink
[minor] add named PathOutsideRoot exception
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Jun 1, 2022
1 parent 2815ad4 commit 786c84c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Uri/Exception/PathOutsideRoot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Zenstruck\Uri\Exception;

/**
* @author Kevin Bond <[email protected]>
*/
final class PathOutsideRoot extends \RuntimeException
{
}
4 changes: 3 additions & 1 deletion src/Uri/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Zenstruck\Uri;

use Zenstruck\Uri\Exception\PathOutsideRoot;

/**
* @author Kevin Bond <[email protected]>
*/
Expand Down Expand Up @@ -61,7 +63,7 @@ public function absolute(): string

switch (true) {
case '..' === $segment && empty($stack):
throw new \RuntimeException(\sprintf('Cannot resolve absolute path for "%s". It is outside of the root.', $this->toString()));
throw new PathOutsideRoot(\sprintf('Cannot resolve absolute path for "%s". It is outside of the root.', $this->toString()));
case '..' === $segment:
\array_pop($stack);

Expand Down
3 changes: 2 additions & 1 deletion tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Zenstruck\Uri;
use Zenstruck\Uri\Exception\PathOutsideRoot;

/**
* @source https://github.com/guzzle/psr7/blob/7858757f390bbe4b3d81762a97d6e6e786bb70ad/tests/UriTest.php
Expand Down Expand Up @@ -588,7 +589,7 @@ public static function validAbsolutePaths(): iterable
*/
public function cannot_get_absolute_path_outside_root($uri): void
{
$this->expectException(\RuntimeException::class);
$this->expectException(PathOutsideRoot::class);

Uri::new($uri)->path()->absolute();
}
Expand Down

0 comments on commit 786c84c

Please sign in to comment.