From f27b65a4929e2e47b08d2d3a1f04490548647bd3 Mon Sep 17 00:00:00 2001 From: Herberto Graca Date: Fri, 28 Jul 2023 20:45:10 +0200 Subject: [PATCH] Allow ClassSet to use several root dirs This way we can specify just the directories we want it to scan, as opposed to include all dirs in the root and then excluding all dirs we don't want in the set. --- src/ClassSet.php | 16 ++++++++-------- tests/Unit/ClassSetTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/ClassSet.php b/src/ClassSet.php index 519a8953..3430b324 100644 --- a/src/ClassSet.php +++ b/src/ClassSet.php @@ -7,15 +7,15 @@ class ClassSet implements \IteratorAggregate { - /** @var string */ - private $directory; + /** @var string[] */ + private $directoryList; /** @var array */ private $exclude; - private function __construct(string $directory) + private function __construct(string ...$directoryList) { - $this->directory = $directory; + $this->directoryList = $directoryList; $this->exclude = []; } @@ -26,21 +26,21 @@ public function excludePath(string $pattern): self return $this; } - public static function fromDir(string $directory): self + public static function fromDir(string ...$directoryList): self { - return new self($directory); + return new self(...$directoryList); } public function getDir(): string { - return $this->directory; + return implode(', ', $this->directoryList); } public function getIterator(): \Traversable { $finder = (new Finder()) ->files() - ->in($this->directory) + ->in($this->directoryList) ->name('*.php') ->sortByName() ->followLinks() diff --git a/tests/Unit/ClassSetTest.php b/tests/Unit/ClassSetTest.php index 5ea3dca1..c9ba60c0 100644 --- a/tests/Unit/ClassSetTest.php +++ b/tests/Unit/ClassSetTest.php @@ -10,6 +10,30 @@ class ClassSetTest extends TestCase { + public function test_can_exclude_files_or_directories_from_multiple_dir_class_set(): void + { + $path = $this->createMvcProjectStructure(); + + $set = ClassSet::fromDir($path.'/Controller', $path.'/Model') + ->excludePath('Repository'); + + $expected = [ + $path.'/Controller/CatalogController.php', + $path.'/Controller/Foo.php', + $path.'/Controller/ProductsController.php', + $path.'/Controller/UserController.php', + $path.'/Controller/YieldController.php', + $path.'/Model/Catalog.php', + $path.'/Model/Products.php', + $path.'/Model/User.php', + ]; + $actual = array_values(array_map(function ($item) { + /** @var \SplFileInfo $item */ + return $item->getPathname(); + }, iterator_to_array($set))); + self::assertEquals($expected, $actual); + } + public function test_can_exclude_files_or_directories(): void { $path = $this->createMvcProjectStructure();