From 781c97fa9bb9d1bfe2ecd34c24be8c77809db7ef Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 29 Mar 2014 17:46:55 +0100 Subject: [PATCH] Refactor to avoid global static state --- src/Composer/Autoload/AutoloadGenerator.php | 12 +++------- src/Composer/Autoload/ClassMapGenerator.php | 24 ++++++++----------- .../Test/Autoload/ClassMapGeneratorTest.php | 14 ++++++++--- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 0de53a3bf..20f8c7570 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -40,7 +40,7 @@ class AutoloadGenerator private $devMode = false; - public function __construct(EventDispatcher $eventDispatcher, IOInterface $io=null) + public function __construct(EventDispatcher $eventDispatcher, IOInterface $io = null) { $this->eventDispatcher = $eventDispatcher; $this->io = $io; @@ -184,7 +184,7 @@ EOF; preg_quote($dir), ($psrType === 'psr-0' && strpos($namespace, '_') === false) ? preg_quote(strtr($namespace, '\\', '/')) : '' ); - foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) { + foreach (ClassMapGenerator::createMap($dir, $whitelist, $this->io) as $class => $path) { if ('' === $namespace || 0 === strpos($class, $namespace)) { if (!isset($classMap[$class])) { $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path); @@ -198,18 +198,12 @@ EOF; } foreach ($autoloads['classmap'] as $dir) { - foreach (ClassMapGenerator::createMap($dir) as $class => $path) { + foreach (ClassMapGenerator::createMap($dir, null, $this->io) as $class => $path) { $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path); $classMap[$class] = $path.",\n"; } } - if ($this->io && count(ClassMapGenerator::$ambiguousReferences) > 0) { - foreach (ClassMapGenerator::$ambiguousReferences as $ambiguousReference) { - $this->io->write('Warning: Ambiguous class "'.$ambiguousReference['class'].'" resolution; defined in "'.$ambiguousReference[0].'" and in "'.$ambiguousReference[1].'" files.'); - } - } - ksort($classMap); foreach ($classMap as $class => $code) { $classmapFile .= ' '.var_export($class, true).' => '.$code; diff --git a/src/Composer/Autoload/ClassMapGenerator.php b/src/Composer/Autoload/ClassMapGenerator.php index 4805e7308..719a16102 100644 --- a/src/Composer/Autoload/ClassMapGenerator.php +++ b/src/Composer/Autoload/ClassMapGenerator.php @@ -12,21 +12,18 @@ */ namespace Composer\Autoload; + use Symfony\Component\Finder\Finder; +use Composer\IO\IOInterface; /** * ClassMapGenerator * * @author Gyula Sallai + * @author Jordi Boggiano */ class ClassMapGenerator { - - /** - * @var array - */ - public static $ambiguousReferences = array(); - /** * Generate a class map file * @@ -54,7 +51,7 @@ class ClassMapGenerator * * @throws \RuntimeException When the path is neither an existing file nor directory */ - public static function createMap($path, $whitelist = null) + public static function createMap($path, $whitelist = null, IOInterface $io = null) { if (is_string($path)) { if (is_file($path)) { @@ -85,15 +82,14 @@ class ClassMapGenerator $classes = self::findClasses($filePath); foreach ($classes as $class) { - if (array_key_exists($class, $map)) { - self::$ambiguousReferences[] = array( - 'class' => $class, - '0' => $map[$class], - '1' => $filePath + if (!isset($map[$class])) { + $map[$class] = $filePath; + } elseif ($io) { + $io->write( + 'Warning: Ambiguous class resolution, "'.$class.'"'. + ' was found in both "'.$map[$class].'" and "'.$filePath.'", the first will be used.' ); } - - $map[$class] = $filePath; } } diff --git a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php index 6b5049830..1a855558f 100644 --- a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php +++ b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php @@ -109,10 +109,18 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase $finder = new Finder(); $finder->files()->in(__DIR__ . '/Fixtures/Ambiguous'); - ClassMapGenerator::createMap($finder); + $io = $this->getMockBuilder('Composer\IO\ConsoleIO') + ->disableOriginalConstructor() + ->getMock(); - $this->assertEquals(1, count(ClassMapGenerator::$ambiguousReferences)); - $this->assertEquals('A', ClassMapGenerator::$ambiguousReferences[0]['class']); + $a = realpath(__DIR__.'/Fixtures/Ambiguous/A.php'); + $b = realpath(__DIR__.'/Fixtures/Ambiguous/other/A.php'); + + $io->expects($this->once()) + ->method('write') + ->with('Warning: Ambiguous class resolution, "A" was found in both "'.$a.'" and "'.$b.'", the first will be used.'); + + ClassMapGenerator::createMap($finder, null, $io); } /**