Skip to content

Commit

Permalink
fix: iterate psr-0/4 fallback dirs in composer finders (fixes #22)
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Oct 11, 2024
1 parent 9ae332c commit 9454684
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 178 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
"Kcs\\ClassFinder\\Benchmark\\": "bench/",
"Kcs\\ClassFinder\\Tests\\": "tests/",
"Kcs\\ClassFinder\\Fixtures\\Psr4\\": "data/Composer/Psr4/",
"Kcs\\ClassFinder\\Fixtures\\Psr4WithClassMap\\": "data/Psr4WithClassMap/"
"Kcs\\ClassFinder\\Fixtures\\Psr4WithClassMap\\": "data/Psr4WithClassMap/",
"": "data/Composer/Psr4Fallback"
},
"psr-0": {
"Kcs\\ClassFinder\\Fixtures\\Psr0\\": "data/Composer/Psr0/"
"Kcs\\ClassFinder\\Fixtures\\Psr0\\": "data/Composer/Psr0/",
"": "data/Composer/Psr0Fallback"
},
"classmap": [
"data/Psr4WithClassMap/Map"
Expand Down
369 changes: 194 additions & 175 deletions composer.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions data/Composer/Psr0Fallback/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

class Logger implements LoggerInterface
{
}
7 changes: 7 additions & 0 deletions data/Composer/Psr0Fallback/LoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

interface LoggerInterface
{
}
7 changes: 7 additions & 0 deletions data/Composer/Psr4Fallback/Logger4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

class Logger4 implements LoggerInterface
{
}
18 changes: 18 additions & 0 deletions lib/Iterator/ComposerIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,23 @@ private function searchInPsrMap(): Generator
yield from $itr;
}
}

foreach ($this->classLoader->getFallbackDirsPsr4() as $dir) {
$itr = new Psr4Iterator('', $dir, $this->reflectorFactory, $this->flags, $this->classLoader->getClassMap(), $this->excludeNamespaces, $this->pathCallback);
if (isset($this->fileFinder)) {
$itr->setFileFinder($this->fileFinder);
}

yield from $itr;
}

foreach ($this->classLoader->getFallbackDirs() as $dir) {
$itr = new Psr0Iterator('', $dir, $this->reflectorFactory, $this->flags, $this->classLoader->getClassMap(), $this->excludeNamespaces, $this->pathCallback);
if (isset($this->fileFinder)) {
$itr->setFileFinder($this->fileFinder);
}

yield from $itr;
}
}
}
22 changes: 22 additions & 0 deletions lib/Iterator/FilteredComposerIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ private function searchInPsrMap(): Generator

yield from $itr;
}

if (! $this->validNamespace('')) {
return;
}

foreach ($this->classLoader->getFallbackDirsPsr4() as $dir) {
$itr = new Psr4Iterator('', $dir, $this->reflectorFactory, $this->flags, $this->classLoader->getClassMap(), $this->excludeNamespaces, $this->pathCallback);
if (isset($this->fileFinder)) {
$itr->setFileFinder($this->fileFinder);
}

yield from $itr;
}

foreach ($this->classLoader->getFallbackDirs() as $dir) {
$itr = new Psr0Iterator('', $dir, $this->reflectorFactory, $this->flags, $this->classLoader->getClassMap(), $this->excludeNamespaces, $this->pathCallback);
if (isset($this->fileFinder)) {
$itr->setFileFinder($this->fileFinder);
}

yield from $itr;
}
}

/** @param array<string, string[]|string> $prefixes */
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/Finder/ComposerFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Kcs\ClassFinder\Fixtures\Psr0;
use Kcs\ClassFinder\Fixtures\Psr4;
use Kcs\ClassFinder\Fixtures\Psr4WithClassMap;
use Logger;
use Logger4;
use LoggerInterface;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Symfony\Component\ErrorHandler\DebugClassLoader as ErrorHandlerClassLoader;
Expand Down Expand Up @@ -74,6 +77,8 @@ public function testFinderShouldFilterByDirectory(): void
Psr0\BarBar::class => new ReflectionClass(Psr0\BarBar::class),
Psr0\Foobar::class => new ReflectionClass(Psr0\Foobar::class),
Psr0\SubNs\FooBaz::class => new ReflectionClass(Psr0\SubNs\FooBaz::class),
Logger::class => new ReflectionClass(Logger::class),
LoggerInterface::class => new ReflectionClass(LoggerInterface::class),
], iterator_to_array($finder));
}

Expand All @@ -97,6 +102,15 @@ public function testFinderShouldFilterByInterfaceImplementation(): void
Psr4\BarBar::class => new ReflectionClass(Psr4\BarBar::class),
Psr0\BarBar::class => new ReflectionClass(Psr0\BarBar::class),
], iterator_to_array($finder));

$finder = (new ComposerFinder())->useAutoloading(false);
$finder->in([__DIR__ . '/../../../data']);
$finder->implementationOf(LoggerInterface::class);

self::assertEquals([
Logger4::class => new ReflectionClass(Logger4::class),
Logger::class => new ReflectionClass(Logger::class),
], iterator_to_array($finder));
}

public function testFinderShouldFilterBySuperClass(): void
Expand Down Expand Up @@ -187,6 +201,9 @@ public function testFinderShouldFilterByNotPath(): void
Psr0\BarBar::class => new ReflectionClass(Psr0\BarBar::class),
Psr0\Foobar::class => new ReflectionClass(Psr0\Foobar::class),
Psr4WithClassMap\BarBar::class => new ReflectionClass(Psr4WithClassMap\BarBar::class),
Logger4::class => new ReflectionClass(Logger4::class),
Logger::class => new ReflectionClass(Logger::class),
LoggerInterface::class => new ReflectionClass(LoggerInterface::class),
], iterator_to_array($finder));
}

Expand All @@ -205,6 +222,9 @@ public function testFinderShouldFilterByNotPathRegex(): void
Psr0\BarBar::class => new ReflectionClass(Psr0\BarBar::class),
Psr0\Foobar::class => new ReflectionClass(Psr0\Foobar::class),
Psr4WithClassMap\BarBar::class => new ReflectionClass(Psr4WithClassMap\BarBar::class),
Logger4::class => new ReflectionClass(Logger4::class),
Logger::class => new ReflectionClass(Logger::class),
LoggerInterface::class => new ReflectionClass(LoggerInterface::class),
], iterator_to_array($finder));
}

Expand All @@ -222,6 +242,9 @@ public function testFinderShouldFilterByPathCallback(): void
Psr0\Foobar::class => new ReflectionClass(Psr0\Foobar::class),
Psr4\SubNs\FooBaz::class => new ReflectionClass(Psr4\SubNs\FooBaz::class),
Psr0\SubNs\FooBaz::class => new ReflectionClass(Psr0\SubNs\FooBaz::class),
Logger4::class => new ReflectionClass(Logger4::class),
Logger::class => new ReflectionClass(Logger::class),
LoggerInterface::class => new ReflectionClass(LoggerInterface::class),
], iterator_to_array($finder));
}
}
2 changes: 1 addition & 1 deletion tests/unit/Finder/PhpDocumentorFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function testFinderShouldFilterByPathCallback(): void

$classes = iterator_to_array($finder);

self::assertCount(8, $classes);
self::assertCount(11, $classes);
self::assertArrayHasKey(Psr4\AbstractClass::class, $classes);
self::assertInstanceOf(Class_::class, $classes[Psr4\AbstractClass::class]);
self::assertArrayHasKey(Psr0\Foobar::class, $classes);
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/Finder/RecursiveFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Kcs\ClassFinder\Fixtures\Psr0;
use Kcs\ClassFinder\Fixtures\Psr4;
use Kcs\ClassFinder\Fixtures\Recursive;
use Logger;
use LoggerInterface;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use SplFileInfo;
Expand Down Expand Up @@ -54,6 +56,8 @@ public function testFinderShouldFilterByDirectory(): void
Psr0\BarBar::class => new ReflectionClass(Psr0\BarBar::class),
Psr0\Foobar::class => new ReflectionClass(Psr0\Foobar::class),
Psr0\SubNs\FooBaz::class => new ReflectionClass(Psr0\SubNs\FooBaz::class),
Logger::class => new ReflectionClass(Logger::class),
LoggerInterface::class => new ReflectionClass(LoggerInterface::class),
], iterator_to_array($finder));
}

Expand Down

0 comments on commit 9454684

Please sign in to comment.