From 48d5f94cda13e693c4729d41395e607704b74343 Mon Sep 17 00:00:00 2001 From: Andreas Erhard Date: Thu, 28 Mar 2019 12:16:35 +0100 Subject: [PATCH] Throw a dedicated exception for non-existing directory Makes Finder::in() throw a DirectoryNotFoundException instead of an InvalidArgumentException if one of the directories is not found. This behavior is more consistent with the AccessDeniedException for files which are unreadable due to insufficient permissions. To keep BC, the new exception class inherits from InvalidArgumentException. --- .../Exception/DirectoryNotFoundException.php | 19 +++++++++++++++++++ src/Symfony/Component/Finder/Finder.php | 5 +++-- .../Component/Finder/Tests/FinderTest.php | 11 ++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Finder/Exception/DirectoryNotFoundException.php diff --git a/src/Symfony/Component/Finder/Exception/DirectoryNotFoundException.php b/src/Symfony/Component/Finder/Exception/DirectoryNotFoundException.php new file mode 100644 index 0000000000000..c6cc0f2736370 --- /dev/null +++ b/src/Symfony/Component/Finder/Exception/DirectoryNotFoundException.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Finder\Exception; + +/** + * @author Andreas Erhard + */ +class DirectoryNotFoundException extends \InvalidArgumentException +{ +} diff --git a/src/Symfony/Component/Finder/Finder.php b/src/Symfony/Component/Finder/Finder.php index f791f8cf32a8f..33b1805979469 100644 --- a/src/Symfony/Component/Finder/Finder.php +++ b/src/Symfony/Component/Finder/Finder.php @@ -13,6 +13,7 @@ use Symfony\Component\Finder\Comparator\DateComparator; use Symfony\Component\Finder\Comparator\NumberComparator; +use Symfony\Component\Finder\Exception\DirectoryNotFoundException; use Symfony\Component\Finder\Iterator\CustomFilterIterator; use Symfony\Component\Finder\Iterator\DateRangeFilterIterator; use Symfony\Component\Finder\Iterator\DepthRangeFilterIterator; @@ -585,7 +586,7 @@ public function ignoreUnreadableDirs($ignore = true) * * @return $this * - * @throws \InvalidArgumentException if one of the directories does not exist + * @throws DirectoryNotFoundException if one of the directories does not exist */ public function in($dirs) { @@ -597,7 +598,7 @@ public function in($dirs) } elseif ($glob = glob($dir, (\defined('GLOB_BRACE') ? GLOB_BRACE : 0) | GLOB_ONLYDIR)) { $resolvedDirs = array_merge($resolvedDirs, array_map([$this, 'normalizeDir'], $glob)); } else { - throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir)); + throw new DirectoryNotFoundException(sprintf('The "%s" directory does not exist.', $dir)); } } diff --git a/src/Symfony/Component/Finder/Tests/FinderTest.php b/src/Symfony/Component/Finder/Tests/FinderTest.php index 6b9a4b00c6d68..2e4e55b3b5d86 100644 --- a/src/Symfony/Component/Finder/Tests/FinderTest.php +++ b/src/Symfony/Component/Finder/Tests/FinderTest.php @@ -888,7 +888,7 @@ public function testIn() } /** - * @expectedException \InvalidArgumentException + * @expectedException \Symfony\Component\Finder\Exception\DirectoryNotFoundException */ public function testInWithNonExistentDirectory() { @@ -896,6 +896,15 @@ public function testInWithNonExistentDirectory() $finder->in('foobar'); } + /** + * @expectedException \InvalidArgumentException + */ + public function testInWithNonExistentDirectoryLegacyException() + { + $finder = new Finder(); + $finder->in('foobar'); + } + public function testInWithGlob() { $finder = $this->buildFinder();