Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow usage of namespace selection using wildcards v2 #16

Open
wants to merge 6 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Factories/LayerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/Layer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @method Layer assertDoesNotDependOn(string ...$objects)
* @method Layer excludeByNameStart(string $name)
* @method Layer exclude(callable $callback)
* @method Layer leaveByNameRegex(string $name)
* @method Layer leaveByNameStart(string $name)
*
* @implements IteratorAggregate<int, ObjectDescription>
Expand Down
8 changes: 5 additions & 3 deletions src/Repositories/ObjectsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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)))];
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Arch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Pest\Expectation;
use Whoops\Exception\Frame;

arch('strict types')->expect('*')->toUseStrictTypes();

arch('base')
->expect('Pest\Arch')
->classes->toBeFinal()
Expand Down
10 changes: 10 additions & 0 deletions tests/Fixtures/Domains/A/Contracts/Models/Bazable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Domains\A\Contracts\Models;

interface Bazable
{
// ...
}
12 changes: 12 additions & 0 deletions tests/Fixtures/Domains/A/Models/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Domains\A\Models;

use Tests\Fixtures\Domains\A\Contracts\Models\Bazable;

final class Article implements Bazable
{
// ...
}
10 changes: 10 additions & 0 deletions tests/Fixtures/Domains/B/Contracts/Models/Bazable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Domains\B\Contracts\Models;

interface Bazable
{
// ...
}
12 changes: 12 additions & 0 deletions tests/Fixtures/Domains/B/Models/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Domains\B\Models;

use Tests\Fixtures\Domains\B\Contracts\Models\Bazable;

final class Article implements Bazable
{
// ...
}
22 changes: 22 additions & 0 deletions tests/Layer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Tests\Fixtures\Domains\A\Contracts\Models\Bazable as BazableDomainA;
use Tests\Fixtures\Domains\A\Models\Article as ArticleDomainA;
use Tests\Fixtures\Domains\B\Contracts\Models\Bazable as BazableDomainB;
use Tests\Fixtures\Domains\B\Models\Article as ArticleDomainB;
use Tests\Fixtures\Enums\Color;
use Tests\Fixtures\Enums\ColorThatDependsOnColor;
use Tests\Fixtures\Misc\DependOnGlobalFunctions;
Expand All @@ -23,6 +27,24 @@
->toOnlyUse('Pest\Support\Str');
});

it('loads namespaces', function () {
expect('Tests\Fixtures\Domains\A\Models')
->getTargets()
->toBe([ArticleDomainA::class]);
});

it('loads namespaces using wildcards', function () {
expect('Tests\Fixtures\Domains\*\Models')
->getTargets()
->toBe([ArticleDomainA::class, ArticleDomainB::class]);
});

it('loads namespaces using multiple wildcards', function () {
expect('Tests\Fixtures\Domains\*\*\Models')
->getTargets()
->toBe([BazableDomainA::class, BazableDomainB::class]);
});

it('does support enums', function () {
expect(Color::class)->toUseNothing()
->and(ColorThatDependsOnColor::class)->toUse([Color::class]);
Expand Down
19 changes: 19 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use Pest\Arch\Exceptions\ArchExpectationFailedException;
use Pest\Arch\Expectations\Targeted;
use Pest\Arch\Objects\ObjectDescription;

uses()->beforeEach(function () {
$this->arch()->ignore([
Expand All @@ -23,3 +25,20 @@
->toBe($line);
});
});

expect()->extend('getTargets', function () {
$classes = [];
Targeted::make(
$this,
function (ObjectDescription $object) use (&$classes): bool {
$classes[] = $object->name;

return true;
},
'',
fn ($path) => 0,
);
$this->value = $classes;

return $this;
});
Loading