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

Feat Introduces new unless modifier #13

Draft
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Draft
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: 2 additions & 2 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ private function getUsagePathAndLines(Layer $layer, string $objectName, string $

$class = PhpCoreExpressions::getClass($target) ?? Name::class;

$nodes = ServiceContainer::$nodeFinder->findInstanceOf(
$nodes = ServiceContainer::$nodeFinder->findInstanceOf( //@phpstan-ignore-line
$dependOnObject->stmts,
$class,
$class, //@phpstan-ignore-line
);

/** @var array<int, Name|Expr> $nodes */
Expand Down
8 changes: 8 additions & 0 deletions src/Contracts/ArchExpectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function mergeExcludeCallbacks(array $callbacks): void;
* @internal
*/
public function excludeCallbacks(): array;

/**
* Expectations that the given "targets" or "dependencies" are to ignore.
*
* @param array<int, string>|string $expectations
* @return $this
*/
public function unless(array|string $expectations): self;
}
12 changes: 12 additions & 0 deletions src/GroupArchExpectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,16 @@ private function ensureLazyExpectationIsVerified(): void
$expectation->ensureLazyExpectationIsVerified();
}
}

/**
* {@inheritDoc}
*/
public function unless(mixed $expectations): self
{
foreach ($this->expectations as $expectation) {
$expectation->unless($expectations);
}

return $this;
}
}
15 changes: 15 additions & 0 deletions src/SingleArchExpectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,19 @@ public function ensureLazyExpectationIsVerified(): void
($this->opposite)(); // @phpstan-ignore-line
}
}

/**
* {@inheritDoc}
*/
public function unless(array|string $expectations): self
{
$modifier = UnlessModifier::make($this->expectation);
$expectations = is_array($expectations) ? $expectations : [$expectations => true];

$this->ignoring([
...$modifier->targetsToIgnore($expectations, LayerOptions::fromExpectation($this)),
]);

return $this;
}
}
114 changes: 114 additions & 0 deletions src/UnlessModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace Pest\Arch;

use Pest\Arch\Collections\Dependencies;
use Pest\Arch\Options\LayerOptions;
use Pest\Arch\ValueObjects\Targets;
use Pest\Expectation;
use PHPUnit\Architecture\Elements\ObjectDescription;

/**
* @internal
*
* @mixin Expectation<array|string>
*/
final readonly class UnlessModifier
{
public static function make(Expectation $expectation): self
{
return new self($expectation);
}

public function __construct(protected Expectation $expectation)
{
}

/* @phpstan-ignore-next-line */
public function targetsToIgnore(array $expectations, LayerOptions $options): array
{
$ignoreArr = [];
$blueprint = Blueprint::make(
Targets::fromExpectation($this->expectation),
Dependencies::fromExpectationInput([]),
);

$targets = (fn (): array => $this->target->value)->call($blueprint);
$layerFactory = (fn (): \Pest\Arch\Factories\LayerFactory => $this->layerFactory)->call($blueprint);

foreach ($targets as $targetValue) {
$targetLayer = $layerFactory->make($options, $targetValue);

foreach ($targetLayer as $object) {
/** @var \Pest\Arch\Objects\ObjectDescription $objectDescription */
$objectDescription = $object;

foreach ($expectations as $expectation => $value) {
if ($ignore = $this->handleExpectation($objectDescription, $expectation, $value)) { // @phpstan-ignore-line
$ignoreArr[] = $ignore;
}
}
}
}

return $ignoreArr;
}

/**
* Handles the expectation.
*/
private function handleExpectation(ObjectDescription $objectDescription, string $expectation, mixed $value): ?string
{
return match ($expectation) {
'abstractParent' => $this->handleAbstractParent($objectDescription, $value),
'extends' => $this->handleExtends($objectDescription, $value),
default => null,
};
}

/**
* Handles the "extends" expectation.
*/
private function handleExtends(ObjectDescription $objectDescription, mixed $value): ?string
{
$reflection = $objectDescription->reflectionClass;

if ($value === true && $reflection->getParentClass() !== false) {
return $reflection->getName();
}

if (! is_string($value)) {
return null;
}

if (! $reflection->isSubclassOf($value)) {
return null;
}

return $reflection->getName();
}

/**
* Handles the "abstractParent" expectation.
*/
private function handleAbstractParent(ObjectDescription $objectDescription, mixed $value): ?string
{
$reflection = $objectDescription->reflectionClass;

if ($reflection->getParentClass() === false) {
return null;
}

if ($value !== true) {
return null;
}

if (! $reflection->getParentClass()->isAbstract()) {
return null;
}

return $reflection->getName();
}
}
15 changes: 15 additions & 0 deletions tests/Arch.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,18 @@
->expect(Targets::class)
->toOnlyUse([Expectation::class])
->ignoring('PHPUnit\Framework');

arch('avoid mutation unless extending controller')
->expect('Tests\Fixtures\Controllers\Service')
->toHavePrefix('Service')
->toBeReadonly()
->unless([
'extends' => Tests\Fixtures\Controller::class,
'abstractParent' => true,
]);

arch('avoid mutation if extends')
->expect('Tests\Fixtures\Controllers\Service')
->toHavePrefix('Service')
->toBeReadonly()
->unless('extends');
9 changes: 9 additions & 0 deletions tests/Fixtures/Controllers/AbstractServiceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Controllers;

abstract class AbstractServiceController
{
}
9 changes: 9 additions & 0 deletions tests/Fixtures/Controllers/Service/ServiceControllerA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Controllers\Service;

final readonly class ServiceControllerA
{
}
9 changes: 9 additions & 0 deletions tests/Fixtures/Controllers/Service/ServiceControllerB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Controllers\Service;

final readonly class ServiceControllerB
{
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Controllers/Service/ServiceControllerC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Controllers\Service;

use Tests\Fixtures\Controllers\ServiceControllerBase;

final class ServiceControllerC extends ServiceControllerBase
{
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Controllers/Service/ServiceControllerD.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Controllers\Service;

use Tests\Fixtures\Controllers\AbstractServiceController;

final class ServiceControllerD extends AbstractServiceController
{
}
11 changes: 11 additions & 0 deletions tests/Fixtures/Controllers/ServiceControllerBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Controllers;

use Tests\Fixtures\Controller;

class ServiceControllerBase extends Controller
{
}