Skip to content
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
33 changes: 33 additions & 0 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,39 @@ public function any(): Any
return new Any;
}

/**
* Excludes files whose paths contain any of the given path fragments from arch analysis.
*
* @param list<string> $paths
*/
public function ignorePaths(array $paths): PendingArchExpectation
{
$normalizedPaths = array_values(array_filter(array_map(
static fn (string $path): string => trim(str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path), DIRECTORY_SEPARATOR),
$paths,
), static fn (string $path): bool => $path !== ''));

return new PendingArchExpectation($this, [
static function (ObjectDescription $object) use ($normalizedPaths): bool {
if (isset($object->path)) {
$normalizedObjectPath = trim(str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $object->path), DIRECTORY_SEPARATOR);
} else {
// Fallback for objects without an initialized path (e.g., vendor objects);
// use the class name as a path approximation.
$normalizedObjectPath = trim(str_replace('\\', DIRECTORY_SEPARATOR, $object->name), DIRECTORY_SEPARATOR);
}

foreach ($normalizedPaths as $path) {
if (str_contains($normalizedObjectPath, $path)) {
return true;
}
}

return false;
},
]);
}

/**
* Asserts that the given expectation target use the given dependencies.
*
Expand Down
52 changes: 52 additions & 0 deletions tests/Features/Expect/ignorePaths.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

use Pest\Arch\Exceptions\ArchExpectationFailedException;

test('single ignored path', function () {
expect('Tests\Fixtures\Arch\IgnorePaths\Single')
->ignorePaths(['database/migrations'])
->toUseStrictTypes();
});

test('multiple ignored paths', function () {
expect('Tests\Fixtures\Arch\IgnorePaths\Multiple')
->ignorePaths(['database/migrations', 'vendor'])
->toUseStrictTypes();
});

test('nested directory ignored', function () {
expect('Tests\Fixtures\Arch\IgnorePaths\Single')
->ignorePaths(['database'])
->toUseStrictTypes();
});

test('partial path matching', function () {
expect('Tests\Fixtures\Arch\IgnorePaths\Single')
->ignorePaths(['migrations'])
->toUseStrictTypes();
});

test('windows path separators', function () {
expect('Tests\Fixtures\Arch\IgnorePaths\Single')
->ignorePaths(['database\\migrations'])
->toUseStrictTypes();
});

test('non-ignored files still analyzed', function () {
expect('Tests\Fixtures\Arch\IgnorePaths\Dirty')
->ignorePaths(['database/migrations'])
->toUseStrictTypes();
})->throws(ArchExpectationFailedException::class);

test('violations caught in all paths without ignorePaths', function () {
expect('Tests\Fixtures\Arch\IgnorePaths\Single')
->toUseStrictTypes();
})->throws(ArchExpectationFailedException::class);

test('chainable with not', function () {
expect('Tests\Fixtures\Arch\IgnorePaths\Single')
->ignorePaths(['database/migrations'])
->not->toBeAbstract();
});
5 changes: 5 additions & 0 deletions tests/Fixtures/Arch/IgnorePaths/Dirty/DirtyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Tests\Fixtures\Arch\IgnorePaths\Dirty;

class DirtyService {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Tests\Fixtures\Arch\IgnorePaths\Dirty\database\migrations;

class CreateUsersTable {}
7 changes: 7 additions & 0 deletions tests/Fixtures/Arch/IgnorePaths/Multiple/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\IgnorePaths\Multiple;

class Service {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Tests\Fixtures\Arch\IgnorePaths\Multiple\database\migrations;

class CreateUsersTable {}
5 changes: 5 additions & 0 deletions tests/Fixtures/Arch/IgnorePaths/Multiple/vendor/Library.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Tests\Fixtures\Arch\IgnorePaths\Multiple\vendor;

class Library {}
7 changes: 7 additions & 0 deletions tests/Fixtures/Arch/IgnorePaths/Single/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\IgnorePaths\Single;

class Service {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Tests\Fixtures\Arch\IgnorePaths\Single\database\migrations;

abstract class AbstractMigration {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Tests\Fixtures\Arch\IgnorePaths\Single\database\migrations;

class CreateUsersTable {}