Skip to content

Commit

Permalink
Fixed error messages if glob is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Dec 28, 2015
1 parent 3199593 commit 6cc1f6b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
46 changes: 21 additions & 25 deletions src/Iterator/GlobIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions tests/GlobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down

0 comments on commit 6cc1f6b

Please sign in to comment.