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.
This commit is contained in:
Andreas Erhard 2019-03-28 12:16:35 +01:00
parent 71c33c1531
commit 48d5f94cda
No known key found for this signature in database
GPG Key ID: AEE3CABEF33BE656
3 changed files with 32 additions and 3 deletions

View File

@ -0,0 +1,19 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <andreas.erhard@i-med.ac.at>
*/
class DirectoryNotFoundException extends \InvalidArgumentException
{
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Finder;
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 @@ class Finder implements \IteratorAggregate, \Countable
*
* @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 @@ class Finder implements \IteratorAggregate, \Countable
} 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));
}
}

View File

@ -888,7 +888,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
}
/**
* @expectedException \InvalidArgumentException
* @expectedException \Symfony\Component\Finder\Exception\DirectoryNotFoundException
*/
public function testInWithNonExistentDirectory()
{
@ -896,6 +896,15 @@ class FinderTest extends Iterator\RealIteratorTestCase
$finder->in('foobar');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInWithNonExistentDirectoryLegacyException()
{
$finder = new Finder();
$finder->in('foobar');
}
public function testInWithGlob()
{
$finder = $this->buildFinder();