From 4e334d6f53108a740ac00d635bad2a9dcb5fc440 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 7 Aug 2024 11:25:27 +0100 Subject: [PATCH] feat: adds `pest-arch-ignore-line` or `pest-arch-ignore-next-line` --- phpstan.neon | 1 - src/Autoload.php | 4 ++-- src/Blueprint.php | 23 +++++++++++++++++++ src/Concerns/Architectable.php | 2 +- src/Objects/FunctionDescription.php | 6 ++--- src/Objects/VendorObjectDescription.php | 2 +- src/PendingArchExpectation.php | 3 +-- tests/Fixtures/Controller.php | 4 +--- .../Controllers/ProductController.php | 2 +- tests/Fixtures/Functions.php | 4 +--- tests/Fixtures/Misc/DependsOnVendor.php | 2 +- tests/Fixtures/NamespacedFunctions.php | 4 +--- tests/Fixtures/Support/Env.php | 4 +--- 13 files changed, 37 insertions(+), 24 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 399349b..e08938a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -15,4 +15,3 @@ parameters: ignoreErrors: - "#has a nullable return type declaration.#" - "# with a nullable type declaration.#" - - "#has parameter \\$closure with null as default value.#" diff --git a/src/Autoload.php b/src/Autoload.php index 1635ef4..5f7234b 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -15,9 +15,9 @@ * argument is the test description; the second argument * is a closure that contains the test expectations. */ - function arch(?string $description = null, ?Closure $closure = null): TestCall + function arch(?string $description = null, ?Closure $closure = null): TestCall // @phpstan-ignore-line { - $test = test(...func_get_args()); + $test = test(...func_get_args()); // @phpstan-ignore-line assert(! $test instanceof HigherOrderTapProxy); diff --git a/src/Blueprint.php b/src/Blueprint.php index cf4c5bb..faeb87e 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -109,7 +109,29 @@ public function targeted(callable $callback, LayerOptions $options, callable $fa } $path = (string) realpath($object->path); + $line = $lineFinder($path); + + $file = file($path); + + if (is_array($file)) { + if (array_key_exists($line - 1, $file)) { + $lineContent = $file[$line - 1]; + + if (str_contains($lineContent, '@pest-arch-ignore-line')) { + continue; + } + } + + if (array_key_exists($line - 2, $file)) { + $lineContent = $file[$line - 2]; + + if (str_contains($lineContent, '@pest-arch-ignore-next-line')) { + continue; + } + } + } + $path = substr($path, strlen(TestSuite::getInstance()->rootPath) + 1); $failure(new Violation($path, $line, $line)); @@ -229,6 +251,7 @@ private function getUsagePathAndLines(Layer $layer, string $objectName, string $ /** @var ObjectDescription $dependOnObject */ $dependOnObject = array_pop($dependOnObjects); + /** @var class-string<\PhpParser\Node> $class */ $class = PhpCoreExpressions::getClass($target) ?? Name::class; $nodes = ServiceContainer::$nodeFinder->findInstanceOf( diff --git a/src/Concerns/Architectable.php b/src/Concerns/Architectable.php index 0318bad..74212dd 100644 --- a/src/Concerns/Architectable.php +++ b/src/Concerns/Architectable.php @@ -21,7 +21,7 @@ trait Architectable */ public function arch(): TestCaseOptions { - $options = $this->options ??= new TestCaseOptions(); + $options = $this->options ??= new TestCaseOptions; return $this->options = $options; } diff --git a/src/Objects/FunctionDescription.php b/src/Objects/FunctionDescription.php index 0cb310b..6cdb517 100644 --- a/src/Objects/FunctionDescription.php +++ b/src/Objects/FunctionDescription.php @@ -18,11 +18,11 @@ final class FunctionDescription extends ObjectDescription // @phpstan-ignore-lin */ public static function make(string $path): self { - $description = new self(); + $description = new self; try { - $description->path = (new ReflectionFunction($path))->getFileName(); - } catch (\Throwable) { + $description->path = (string) (new ReflectionFunction($path))->getFileName(); + } catch (\Throwable) { // @phpstan-ignore-line $description->path = $path; } diff --git a/src/Objects/VendorObjectDescription.php b/src/Objects/VendorObjectDescription.php index eb741e7..c80a532 100644 --- a/src/Objects/VendorObjectDescription.php +++ b/src/Objects/VendorObjectDescription.php @@ -17,7 +17,7 @@ final class VendorObjectDescription extends ObjectDescription // @phpstan-ignore */ public static function make(string $path): ?self // @phpstan-ignore-line { - $object = new self(); + $object = new self; try { $vendorObject = ObjectDescriptionBase::make($path); diff --git a/src/PendingArchExpectation.php b/src/PendingArchExpectation.php index 80ebe14..f09c603 100644 --- a/src/PendingArchExpectation.php +++ b/src/PendingArchExpectation.php @@ -30,8 +30,7 @@ final class PendingArchExpectation public function __construct( private readonly Expectation $expectation, private array $excludeCallbacks, - ) { - } + ) {} /** * Filters the given "targets" by only classes. diff --git a/tests/Fixtures/Controller.php b/tests/Fixtures/Controller.php index 3445d7d..d5b51a6 100644 --- a/tests/Fixtures/Controller.php +++ b/tests/Fixtures/Controller.php @@ -4,6 +4,4 @@ namespace Tests\Fixtures; -abstract class Controller -{ -} +abstract class Controller {} diff --git a/tests/Fixtures/Controllers/ProductController.php b/tests/Fixtures/Controllers/ProductController.php index d4344d0..d64f88b 100644 --- a/tests/Fixtures/Controllers/ProductController.php +++ b/tests/Fixtures/Controllers/ProductController.php @@ -11,7 +11,7 @@ final class ProductController public function index(): array { return [ - new Product(), + new Product, ]; } } diff --git a/tests/Fixtures/Functions.php b/tests/Fixtures/Functions.php index 85e2ea9..78dd2f7 100644 --- a/tests/Fixtures/Functions.php +++ b/tests/Fixtures/Functions.php @@ -1,7 +1,5 @@