From 6cc1f6ba58dafaa0bd21829fbabf47e657e6a67e Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 28 Dec 2015 10:32:55 +0100 Subject: [PATCH] Fixed error messages if glob is invalid --- src/Iterator/GlobIterator.php | 46 ++++++++++++++++------------------- tests/GlobTest.php | 22 +++++++++++++++-- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/Iterator/GlobIterator.php b/src/Iterator/GlobIterator.php index 9814276..1c0ebba 100644 --- a/src/Iterator/GlobIterator.php +++ b/src/Iterator/GlobIterator.php @@ -53,37 +53,33 @@ public function __construct($glob, $flags = 0) // glob() does not support [^...] on Windows ('\\' !== DIRECTORY_SEPARATOR || false === strpos($glob, '[^')) ) { - if (false === $results = glob($glob, GLOB_BRACE)) { + $results = glob($glob, GLOB_BRACE); + + // $results may be empty or false if $glob is invalid + if (empty($results)) { + // Parse glob and provoke errors if invalid + Glob::toRegEx($glob); + + // Otherwise return empty result set $innerIterator = new EmptyIterator(); } else { $innerIterator = new ArrayIterator($results); } } else { - try { - // Otherwise scan the glob's base directory for matches - $innerIterator = new GlobFilterIterator( - $glob, - new RecursiveIteratorIterator( - new RecursiveDirectoryIterator( - $basePath, - RecursiveDirectoryIterator::CURRENT_AS_PATHNAME - | RecursiveDirectoryIterator::SKIP_DOTS - ), - RecursiveIteratorIterator::SELF_FIRST + // Otherwise scan the glob's base directory for matches + $innerIterator = new GlobFilterIterator( + $glob, + new RecursiveIteratorIterator( + new RecursiveDirectoryIterator( + $basePath, + RecursiveDirectoryIterator::CURRENT_AS_PATHNAME + | RecursiveDirectoryIterator::SKIP_DOTS ), - GlobFilterIterator::FILTER_VALUE, - $flags - ); - } catch (InvalidArgumentException $e) { - if (0 === strpos($e->getMessage(), 'Invalid glob: missing ]') - || 0 === strpos($e->getMessage(), 'Invalid glob: missing }')) { - // Remain compatible with glob() which simply returns - // nothing in this case - $innerIterator = new EmptyIterator(); - } else { - throw $e; - } - } + RecursiveIteratorIterator::SELF_FIRST + ), + GlobFilterIterator::FILTER_VALUE, + $flags + ); } } else { // If the glob's base directory does not exist, return nothing diff --git a/tests/GlobTest.php b/tests/GlobTest.php index f40aa41..51df5c9 100644 --- a/tests/GlobTest.php +++ b/tests/GlobTest.php @@ -263,20 +263,38 @@ public function testGlobEscape() ), Glob::glob($this->tempDir.'/css/style\\^.css')); } - public function testGlobReturnsEmptyArrayIfUnclosedBrace() + /** + * @expectedException \InvalidArgumentException + */ + public function testNativeGlobThrowsExceptionIfUnclosedBrace() { // native impl $this->assertSame(array(), Glob::glob($this->tempDir.'/*.cs{t,s')); + } + /** + * @expectedException \InvalidArgumentException + */ + public function testCustomGlobThrowsExceptionIfUnclosedBrace() + { // custom impl $this->assertSame(array(), Glob::glob($this->tempDir.'/**/*.cs{t,s')); } - public function testGlobReturnsEmptyArrayIfUnclosedBracket() + /** + * @expectedException \InvalidArgumentException + */ + public function testNativeGlobThrowsExceptionIfUnclosedBracket() { // native impl $this->assertSame(array(), Glob::glob($this->tempDir.'/*.cs[ts')); + } + /** + * @expectedException \InvalidArgumentException + */ + public function testCustomGlobThrowsExceptionIfUnclosedBracket() + { // custom impl $this->assertSame(array(), Glob::glob($this->tempDir.'/**/*.cs[ts')); }