Skip to content
Browse files

[ClassMapGenerator] Improve error message when the path does not exist

i.e. the composer.json has a typo
  • Loading branch information...
1 parent ff5c428 commit ab481145314e952a97df578db1fcabf8b6996f76 @vicb vicb committed Nov 11, 2012
Showing with 29 additions and 13 deletions.
  1. +19 −12 src/Composer/Autoload/ClassMapGenerator.php
  2. +10 −1 tests/Composer/Test/Autoload/ClassMapGeneratorTest.php
View
31 src/Composer/Autoload/ClassMapGenerator.php
@@ -40,42 +40,49 @@ public static function dump($dirs, $file)
/**
* Iterate over all files in the given directory searching for classes
*
- * @param Iterator|string $dir The directory to search in or an iterator
+ * @param Iterator|string $path The path to search in or an iterator
* @param string $whitelist Regex that matches against the file path
*
* @return array A class map array
+ *
+ * @throws \RuntimeException When the path is neither an existing file nor directory
*/
- public static function createMap($dir, $whitelist = null)
+ public static function createMap($path, $whitelist = null)
{
- if (is_string($dir)) {
- if (is_file($dir)) {
- $dir = array(new \SplFileInfo($dir));
+ if (is_string($path)) {
+ if (is_file($path)) {
+ $path = array(new \SplFileInfo($path));
+ } else if (is_dir($path)) {
+ $path = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
} else {
- $dir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
+ throw new \RuntimeException(
+ 'Could not scan for classes inside "'.$path.
+ '" which does not appear to be a file nor a folder'
+ );
}
}
$map = array();
- foreach ($dir as $file) {
+ foreach ($path as $file) {
if (!$file->isFile()) {
continue;
}
- $path = $file->getRealPath();
+ $filePath = $file->getRealPath();
- if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') {
+ if (pathinfo($filePath, PATHINFO_EXTENSION) !== 'php') {
continue;
}
- if ($whitelist && !preg_match($whitelist, strtr($path, '\\', '/'))) {
+ if ($whitelist && !preg_match($whitelist, strtr($filePath, '\\', '/'))) {
continue;
}
- $classes = self::findClasses($path);
+ $classes = self::findClasses($filePath);
foreach ($classes as $class) {
- $map[$class] = $path;
+ $map[$class] = $filePath;
}
}
View
11 tests/Composer/Test/Autoload/ClassMapGeneratorTest.php
@@ -88,7 +88,7 @@ public function testCreateMapFinderSupport()
* @expectedException \RuntimeException
* @expectedExceptionMessage Could not scan for classes inside
*/
- public function testThrowsWhenFileDoesNotExist()
+ public function testFindClassesThrowsWhenFileDoesNotExist()
{
$r = new \ReflectionClass('Composer\\Autoload\\ClassMapGenerator');
$find = $r->getMethod('findClasses');
@@ -97,6 +97,15 @@ public function testThrowsWhenFileDoesNotExist()
$find->invoke(null, __DIR__.'/no-file');
}
+ /**
+ * @expectedException \RuntimeException
+ * @expectedExceptionMessage Could not scan for classes inside
+ */
+ public function testCreateMapThrowsWhenDirectoryDoesNotExist()
+ {
+ ClassMapGenerator::createMap(__DIR__.'/no-file.no-foler');
+ }
+
protected function assertEqualsNormalized($expected, $actual, $message = null)
{
foreach ($expected as $ns => $path) {

0 comments on commit ab48114

Please sign in to comment.
Something went wrong with that request. Please try again.