From ac5bf961b0fbb6a81d96da2c5f202ca1ef0001ba Mon Sep 17 00:00:00 2001 From: Naxvog Date: Wed, 6 Mar 2024 00:35:22 +0100 Subject: [PATCH 1/6] Simplify method hints --- src/Layer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Layer.php b/src/Layer.php index 2a87939..443154d 100644 --- a/src/Layer.php +++ b/src/Layer.php @@ -6,14 +6,14 @@ use IteratorAggregate; use PHPUnit\Architecture\Elements\Layer\Layer as BaseLayer; +use PHPUnit\Architecture\Elements\Layer\LayerLeave; use PHPUnit\Architecture\Elements\ObjectDescription; use Traversable; /** * @method Layer assertDoesNotDependOn(string ...$objects) * @method Layer excludeByNameStart(string $name) - * @method Layer exclude(callable $callback) - * @method Layer leaveByNameStart(string $name) + * @mixin LayerLeave * * @implements IteratorAggregate */ From f31d361a68320d4a256c989a3ae0149a25d5a812 Mon Sep 17 00:00:00 2001 From: Naxvog Date: Wed, 6 Mar 2024 00:35:49 +0100 Subject: [PATCH 2/6] Allow namespace selection using wildcards --- src/Factories/LayerFactory.php | 4 +++- src/Repositories/ObjectsRepository.php | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Factories/LayerFactory.php b/src/Factories/LayerFactory.php index 77e4575..888f92d 100644 --- a/src/Factories/LayerFactory.php +++ b/src/Factories/LayerFactory.php @@ -60,7 +60,9 @@ public function make(LayerOptions $options, string $name, bool $onlyUserDefinedU return $object; }, $this->objectsStorage->allByNamespace($name, $onlyUserDefinedUses)); - $layer = Layer::fromBase($objects)->leaveByNameStart($name); + $layer = str_contains($name, '*') + ? Layer::fromBase($objects)->leaveByNameRegex('/'.strtr(preg_quote($name, '/'), ['\\*' => '[^\\\\]+']).'/') + : Layer::fromBase($objects)->leaveByNameStart($name); foreach ($options->exclude as $exclude) { $layer = $layer->excludeByNameStart($exclude); diff --git a/src/Repositories/ObjectsRepository.php b/src/Repositories/ObjectsRepository.php index 4970287..d7fdcdc 100644 --- a/src/Repositories/ObjectsRepository.php +++ b/src/Repositories/ObjectsRepository.php @@ -103,7 +103,7 @@ public function allByNamespace(string $namespace, bool $onlyUserDefinedUses = tr $objectsPerPrefix = array_values(array_filter(array_reduce($directories, fn (array $files, string $fileOrDirectory): array => array_merge($files, array_values(array_map( static fn (SplFileInfo $file): ?ObjectDescription => ObjectDescriptionFactory::make($file->getPathname(), $onlyUserDefinedUses), - is_dir($fileOrDirectory) ? iterator_to_array(Finder::create()->files()->in($fileOrDirectory)->name('*.php')) : [new SplFileInfo($fileOrDirectory)], + is_dir($fileOrDirectory) || str_contains($fileOrDirectory, '*') ? iterator_to_array(Finder::create()->files()->in($fileOrDirectory)->name('*.php')) : [new SplFileInfo($fileOrDirectory)], ))), []))); $objects = [...$objects, ...$this->cachedObjectsPerPrefix[$prefix][(int) $onlyUserDefinedUses] = $objectsPerPrefix]; @@ -151,13 +151,15 @@ private function directoriesByNamespace(string $name): array $directoriesByNamespace[$name] = [...$directoriesByNamespace[$name] ?? [], ...array_values(array_filter(array_map(static function (string $directory) use ($prefix): string { $fileOrDirectory = $directory.DIRECTORY_SEPARATOR.$prefix; - if (is_dir($fileOrDirectory)) { return $fileOrDirectory; } + if (str_contains($fileOrDirectory, '*')) { + return $fileOrDirectory; + } return $fileOrDirectory.'.php'; - }, $directories), static fn (string $fileOrDirectory): bool => is_dir($fileOrDirectory) || file_exists($fileOrDirectory)))]; + }, $directories), static fn (string $fileOrDirectory): bool => is_dir($fileOrDirectory) || str_contains($fileOrDirectory, '*') || file_exists($fileOrDirectory)))]; } } From 2684a9b960f6febaa7595227ba75359e71fcbbc6 Mon Sep 17 00:00:00 2001 From: Naxvog Date: Wed, 6 Mar 2024 00:36:30 +0100 Subject: [PATCH 3/6] namespace wildcard tests --- .../Domains/A/Contracts/Models/Bazable.php | 10 +++++++++ tests/Fixtures/Domains/A/Models/Article.php | 12 ++++++++++ .../Domains/B/Contracts/Models/Bazable.php | 10 +++++++++ tests/Fixtures/Domains/B/Models/Article.php | 12 ++++++++++ tests/Layer.php | 22 +++++++++++++++++++ tests/Pest.php | 19 ++++++++++++++++ 6 files changed, 85 insertions(+) create mode 100644 tests/Fixtures/Domains/A/Contracts/Models/Bazable.php create mode 100644 tests/Fixtures/Domains/A/Models/Article.php create mode 100644 tests/Fixtures/Domains/B/Contracts/Models/Bazable.php create mode 100644 tests/Fixtures/Domains/B/Models/Article.php diff --git a/tests/Fixtures/Domains/A/Contracts/Models/Bazable.php b/tests/Fixtures/Domains/A/Contracts/Models/Bazable.php new file mode 100644 index 0000000..d8070f1 --- /dev/null +++ b/tests/Fixtures/Domains/A/Contracts/Models/Bazable.php @@ -0,0 +1,10 @@ +toOnlyUse('Pest\Support\Str'); }); +it('loads namespaces', function () { + expect('Tests\Fixtures\Domains\A\Models') + ->getObjects() + ->toBe([ArticleDomainA::class]); +}); + +it('loads namespaces using wildcards', function () { + expect('Tests\Fixtures\Domains\*\Models') + ->getObjects() + ->toBe([ArticleDomainA::class, ArticleDomainB::class]); +}); + +it('loads namespaces using multiple wildcards', function () { + expect('Tests\Fixtures\Domains\*\*\Models') + ->getObjects() + ->toBe([BazableDomainA::class, BazableDomainB::class]); +}); + it('does support enums', function () { expect(Color::class)->toUseNothing() ->and(ColorThatDependsOnColor::class)->toUse([Color::class]); diff --git a/tests/Pest.php b/tests/Pest.php index a88d89a..655339f 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,6 +1,8 @@ beforeEach(function () { $this->arch()->ignore([ @@ -23,3 +25,20 @@ ->toBe($line); }); }); + +expect()->extend('getObjects', function () { + $classes = []; + Targeted::make( + $this, + function (ObjectDescription $object) use (&$classes): bool { + $classes[] = $object->name; + + return true; + }, + '', + fn ($path) => 0, + ); + $this->value = $classes; + + return $this; +}); From 310925909a2c9e7390c8f246ac75861d15884e43 Mon Sep 17 00:00:00 2001 From: Naxvog Date: Wed, 6 Mar 2024 02:12:38 +0100 Subject: [PATCH 4/6] Improved naming --- tests/Layer.php | 6 +++--- tests/Pest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Layer.php b/tests/Layer.php index a7f17fc..d319c86 100644 --- a/tests/Layer.php +++ b/tests/Layer.php @@ -29,19 +29,19 @@ it('loads namespaces', function () { expect('Tests\Fixtures\Domains\A\Models') - ->getObjects() + ->getTargets() ->toBe([ArticleDomainA::class]); }); it('loads namespaces using wildcards', function () { expect('Tests\Fixtures\Domains\*\Models') - ->getObjects() + ->getTargets() ->toBe([ArticleDomainA::class, ArticleDomainB::class]); }); it('loads namespaces using multiple wildcards', function () { expect('Tests\Fixtures\Domains\*\*\Models') - ->getObjects() + ->getTargets() ->toBe([BazableDomainA::class, BazableDomainB::class]); }); diff --git a/tests/Pest.php b/tests/Pest.php index 655339f..821b304 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -26,7 +26,7 @@ }); }); -expect()->extend('getObjects', function () { +expect()->extend('getTargets', function () { $classes = []; Targeted::make( $this, From 368091bafbdf39db9069f07d31159252b73fb7bf Mon Sep 17 00:00:00 2001 From: Naxvog Date: Mon, 30 Sep 2024 02:57:51 +0200 Subject: [PATCH 5/6] Fixed stan issues --- src/Layer.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Layer.php b/src/Layer.php index 443154d..9f06012 100644 --- a/src/Layer.php +++ b/src/Layer.php @@ -6,14 +6,15 @@ use IteratorAggregate; use PHPUnit\Architecture\Elements\Layer\Layer as BaseLayer; -use PHPUnit\Architecture\Elements\Layer\LayerLeave; use PHPUnit\Architecture\Elements\ObjectDescription; use Traversable; /** * @method Layer assertDoesNotDependOn(string ...$objects) * @method Layer excludeByNameStart(string $name) - * @mixin LayerLeave + * @method Layer exclude(callable $callback) + * @method Layer leaveByNameRegex(string $name) + * @method Layer leaveByNameStart(string $name) * * @implements IteratorAggregate */ From 16690cb5ee2f379f32c336f7cc8210c5bbd955f0 Mon Sep 17 00:00:00 2001 From: Naxvog Date: Mon, 30 Sep 2024 15:24:13 +0200 Subject: [PATCH 6/6] Added strict types test --- tests/Arch.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Arch.php b/tests/Arch.php index 0e2aaae..36142c3 100644 --- a/tests/Arch.php +++ b/tests/Arch.php @@ -11,6 +11,8 @@ use Pest\Expectation; use Whoops\Exception\Frame; +arch('strict types')->expect('*')->toUseStrictTypes(); + arch('base') ->expect('Pest\Arch') ->classes->toBeFinal()