From 13b57112a9fe467eeb9df2e351a31c6f790d54b9 Mon Sep 17 00:00:00 2001 From: Jan Brecka Date: Thu, 20 Mar 2014 13:37:05 +0100 Subject: [PATCH] Throw RuntimeException when ambiguous reference was found. --- src/Composer/Autoload/ClassMapGenerator.php | 4 ++++ .../Test/Autoload/ClassMapGeneratorTest.php | 16 ++++++++++++++++ .../Test/Autoload/Fixtures/Ambiguous/A.php | 6 ++++++ .../Test/Autoload/Fixtures/Ambiguous/other/A.php | 6 ++++++ 4 files changed, 32 insertions(+) create mode 100644 tests/Composer/Test/Autoload/Fixtures/Ambiguous/A.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/Ambiguous/other/A.php diff --git a/src/Composer/Autoload/ClassMapGenerator.php b/src/Composer/Autoload/ClassMapGenerator.php index 73c036a37..d470ca2da 100644 --- a/src/Composer/Autoload/ClassMapGenerator.php +++ b/src/Composer/Autoload/ClassMapGenerator.php @@ -79,6 +79,10 @@ class ClassMapGenerator $classes = self::findClasses($filePath); foreach ($classes as $class) { + if (array_key_exists($class, $map)) { + throw new \RuntimeException('Ambiguous reference to class "'.$class.'" was found.'); + } + $map[$class] = $filePath; } } diff --git a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php index ddadc89c5..fe33b1f36 100644 --- a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php +++ b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php @@ -104,6 +104,22 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase $find->invoke(null, __DIR__.'/no-file'); } + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage Ambiguous reference to class "A" was found. + */ + public function testAmbiguousReference() + { + if (!class_exists('Symfony\\Component\\Finder\\Finder')) { + $this->markTestSkipped('Finder component is not available'); + } + + $finder = new \Symfony\Component\Finder\Finder(); + $finder->files()->in(__DIR__ . '/Fixtures/Ambiguous'); + + ClassMapGenerator::createMap($finder); + } + /** * @expectedException \RuntimeException * @expectedExceptionMessage Could not scan for classes inside diff --git a/tests/Composer/Test/Autoload/Fixtures/Ambiguous/A.php b/tests/Composer/Test/Autoload/Fixtures/Ambiguous/A.php new file mode 100644 index 000000000..ee9325efa --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/Ambiguous/A.php @@ -0,0 +1,6 @@ +