From 6e14229cdc54e7873d39e3ed16ce24405086feac Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Mon, 29 Jun 2015 09:34:04 +0200 Subject: [PATCH 1/2] avoid uneeded duplicate prefix --- ClassLoader.php | 12 ++++++++---- Tests/ClassLoaderTest.php | 14 +++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/ClassLoader.php b/ClassLoader.php index 6fcbb06..f89d13b 100644 --- a/ClassLoader.php +++ b/ClassLoader.php @@ -91,10 +91,14 @@ public function addPrefix($prefix, $paths) return; } if (isset($this->prefixes[$prefix])) { - $this->prefixes[$prefix] = array_merge( - $this->prefixes[$prefix], - (array) $paths - ); + if (is_array($paths)) { + $this->prefixes[$prefix] = array_merge( + $this->prefixes[$prefix], + $paths + ); + } if (!in_array($paths, $this->prefixes[$prefix])) { + $this->prefixes[$prefix][] = $paths; + } } else { $this->prefixes[$prefix] = (array) $paths; } diff --git a/Tests/ClassLoaderTest.php b/Tests/ClassLoaderTest.php index a870935..4ede139 100644 --- a/Tests/ClassLoaderTest.php +++ b/Tests/ClassLoaderTest.php @@ -76,14 +76,26 @@ public function getLoadNonexistentClassTests() ); } - public function testAddPrefix() + public function testAddPrefixSingle() { $loader = new ClassLoader(); $loader->addPrefix('Foo', __DIR__.DIRECTORY_SEPARATOR.'Fixtures'); $loader->addPrefix('Foo', __DIR__.DIRECTORY_SEPARATOR.'Fixtures'); $prefixes = $loader->getPrefixes(); $this->assertArrayHasKey('Foo', $prefixes); + $this->assertCount(1, $prefixes['Foo']); + } + + public function testAddPrefixMulti() + { + $loader = new ClassLoader(); + $loader->addPrefix('Foo', 'foo'); + $loader->addPrefix('Foo', 'bar'); + $prefixes = $loader->getPrefixes(); + $this->assertArrayHasKey('Foo', $prefixes); $this->assertCount(2, $prefixes['Foo']); + $this->assertContains('foo', $prefixes['Foo']); + $this->assertContains('bar', $prefixes['Foo']); } public function testUseIncludePath() From 10bf262f9cdc1cd1a53f3f569f7bcd7e12116711 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Mon, 29 Jun 2015 09:51:33 +0200 Subject: [PATCH 2/2] also avoid duplicate in array --- ClassLoader.php | 8 ++++---- Tests/ClassLoaderTest.php | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ClassLoader.php b/ClassLoader.php index f89d13b..1343bdb 100644 --- a/ClassLoader.php +++ b/ClassLoader.php @@ -92,15 +92,15 @@ public function addPrefix($prefix, $paths) } if (isset($this->prefixes[$prefix])) { if (is_array($paths)) { - $this->prefixes[$prefix] = array_merge( + $this->prefixes[$prefix] = array_unique(array_merge( $this->prefixes[$prefix], $paths - ); - } if (!in_array($paths, $this->prefixes[$prefix])) { + )); + } else if (!in_array($paths, $this->prefixes[$prefix])) { $this->prefixes[$prefix][] = $paths; } } else { - $this->prefixes[$prefix] = (array) $paths; + $this->prefixes[$prefix] = array_unique((array) $paths); } } diff --git a/Tests/ClassLoaderTest.php b/Tests/ClassLoaderTest.php index 4ede139..09eea04 100644 --- a/Tests/ClassLoaderTest.php +++ b/Tests/ClassLoaderTest.php @@ -86,6 +86,16 @@ public function testAddPrefixSingle() $this->assertCount(1, $prefixes['Foo']); } + public function testAddPrefixesSingle() + { + $loader = new ClassLoader(); + $loader->addPrefixes(array('Foo' => array('foo', 'foo'))); + $loader->addPrefixes(array('Foo' => array('foo'))); + $prefixes = $loader->getPrefixes(); + $this->assertArrayHasKey('Foo', $prefixes); + $this->assertCount(1, $prefixes['Foo'], print_r($prefixes, true)); + } + public function testAddPrefixMulti() { $loader = new ClassLoader();