Skip to content

Commit c1205c4

Browse files
authored
Multiple dirs class set (#400)
* 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. * Renaming getDirs() to getDirsDescription() because now it's getting multiple dirs, and it's still a string so it's not atomic anymore.
1 parent ae343b4 commit c1205c4

File tree

6 files changed

+38
-13
lines changed

6 files changed

+38
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ use Arkitect\Expression\ForClasses\ResideInOneOfTheseNamespaces;
118118
use Arkitect\Rules\Rule;
119119

120120
return static function (Config $config): void {
121-
$mvcClassSet = ClassSet::fromDir(__DIR__.'/mvc');
121+
$mvcClassSet = ClassSet::fromDir(__DIR__.'/mvc', __DIR__.'/lib/my-lib/src');
122122

123123
$rules = [];
124124

src/CLI/Progress/DebugProgress.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(OutputInterface $output)
1717

1818
public function startFileSetAnalysis(ClassSet $set): void
1919
{
20-
$this->output->writeln("Start analyze dir {$set->getDir()}");
20+
$this->output->writeln("Start analyze dirs {$set->getDirsDescription()}");
2121
}
2222

2323
public function startParsingFile(string $file): void

src/CLI/Progress/ProgressBarProgress.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(OutputInterface $output)
3333

3434
public function startFileSetAnalysis(ClassSet $set): void
3535
{
36-
$this->output->writeln("analyze class set {$set->getDir()}");
36+
$this->output->writeln("analyze class set {$set->getDirsDescription()}");
3737
$this->output->writeln('');
3838
$this->progress = new ProgressBar($this->output, iterator_count($set));
3939

src/ClassSet.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
*/
1212
class ClassSet implements \IteratorAggregate
1313
{
14-
private string $directory;
14+
/** @var string[] */
15+
private array $directoryList;
1516

1617
private array $exclude;
1718

18-
private function __construct(string $directory)
19+
private function __construct(string ...$directoryList)
1920
{
20-
$this->directory = $directory;
21+
$this->directoryList = $directoryList;
2122
$this->exclude = [];
2223
}
2324

@@ -28,21 +29,21 @@ public function excludePath(string $pattern): self
2829
return $this;
2930
}
3031

31-
public static function fromDir(string $directory): self
32+
public static function fromDir(string ...$directoryList): self
3233
{
33-
return new self($directory);
34+
return new self(...$directoryList);
3435
}
3536

36-
public function getDir(): string
37+
public function getDirsDescription(): string
3738
{
38-
return $this->directory;
39+
return implode(', ', $this->directoryList);
3940
}
4041

4142
public function getIterator(): \Traversable
4243
{
4344
$finder = (new Finder())
4445
->files()
45-
->in($this->directory)
46+
->in($this->directoryList)
4647
->name('*.php')
4748
->sortByName()
4849
->followLinks()

tests/Unit/CLI/Progress/DebugProgressTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public function test_it_should_generate_text_on_start_file_set_analysis(): void
2828
$output = $this->prophesize(OutputInterface::class);
2929
$debugProgress = new DebugProgress($output->reveal());
3030

31-
$output->writeln('Start analyze dir directory')->shouldBeCalled();
32-
$debugProgress->startFileSetAnalysis(ClassSet::fromDir('directory'));
31+
$output->writeln('Start analyze dirs directory1, directory2')->shouldBeCalled();
32+
$debugProgress->startFileSetAnalysis(ClassSet::fromDir('directory1', 'directory2'));
3333
}
3434

3535
public function test_it_should_not_generate_text_on_end_parsing_file(): void

tests/Unit/ClassSetTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@
1010

1111
class ClassSetTest extends TestCase
1212
{
13+
public function test_can_exclude_files_or_directories_from_multiple_dir_class_set(): void
14+
{
15+
$path = $this->createMvcProjectStructure();
16+
17+
$set = ClassSet::fromDir($path.'/Controller', $path.'/Model')
18+
->excludePath('Repository');
19+
20+
$expected = [
21+
$path.'/Controller/CatalogController.php',
22+
$path.'/Controller/Foo.php',
23+
$path.'/Controller/ProductsController.php',
24+
$path.'/Controller/UserController.php',
25+
$path.'/Controller/YieldController.php',
26+
$path.'/Model/Catalog.php',
27+
$path.'/Model/Products.php',
28+
$path.'/Model/User.php',
29+
];
30+
$actual = array_values(array_map(function ($item) {
31+
/** @var \SplFileInfo $item */
32+
return $item->getPathname();
33+
}, iterator_to_array($set)));
34+
self::assertEquals($expected, $actual);
35+
}
36+
1337
public function test_can_exclude_files_or_directories(): void
1438
{
1539
$path = $this->createMvcProjectStructure();

0 commit comments

Comments
 (0)