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

Make allowed layer resolution public #897

Merged
merged 1 commit into from
Jun 2, 2022
Merged
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
7 changes: 5 additions & 2 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
use Qossmic\Deptrac\Layer\Collector\SuperglobalCollector;
use Qossmic\Deptrac\Layer\Collector\TraitCollector;
use Qossmic\Deptrac\Layer\Collector\UsesCollector;
use Qossmic\Deptrac\Layer\LayerProvider;
use Qossmic\Deptrac\Layer\LayerResolver;
use Qossmic\Deptrac\Layer\LayerResolverInterface;
use Qossmic\Deptrac\OutputFormatter\BaselineOutputFormatter;
Expand Down Expand Up @@ -261,10 +262,12 @@
->set(MatchingLayersHandler::class)
->tag('event_listener', ['priority' => 16]);
$services
->set(AllowDependencyHandler::class)
->set(LayerProvider::class)
->args([
'$allowedLayers' => param('ruleset'),
])
]);
$services
->set(AllowDependencyHandler::class)
->tag('event_listener', ['priority' => 4]);
$services
->set(ViolationHandler::class)
Expand Down
50 changes: 5 additions & 45 deletions src/Analyser/EventHandler/AllowDependencyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,18 @@

use Qossmic\Deptrac\Analyser\Event\ProcessEvent;
use Qossmic\Deptrac\Layer\Exception\CircularReferenceException;
use Qossmic\Deptrac\Layer\LayerProvider;
use Qossmic\Deptrac\Result\Allowed;
use Qossmic\Deptrac\Result\Error;
use function array_merge;
use function array_unique;
use function array_values;
use function in_array;
use function ltrim;
use function strncmp;

class AllowDependencyHandler
{
/**
* @var array<string, string[]>
*/
private array $allowedLayers;
private LayerProvider $layerProvider;

/**
* @param array<string, string[]> $allowedLayers
*/
public function __construct(array $allowedLayers)
public function __construct(LayerProvider $layerProvider)
{
$this->allowedLayers = $allowedLayers;
$this->layerProvider = $layerProvider;
}

public function __invoke(ProcessEvent $event): void
Expand All @@ -38,7 +28,7 @@ public function __invoke(ProcessEvent $event): void

foreach ($event->getDependentLayers() as $dependentLayer) {
try {
$allowedLayers = $this->getAllowedLayers($dependerLayer);
$allowedLayers = $this->layerProvider->getAllowedLayers($dependerLayer);
} catch (CircularReferenceException $circularReferenceException) {
$ruleset->addError(new Error($circularReferenceException->getMessage()));
$event->stopPropagation();
Expand All @@ -55,34 +45,4 @@ public function __invoke(ProcessEvent $event): void
$event->stopPropagation();
}
}

/**
* @return string[]
*/
private function getAllowedLayers(string $layerName): array
{
return array_values(array_unique($this->getTransitiveDependencies($layerName, [])));
}

/**
* @param string[] $previousLayers
*
* @return string[]
*/
private function getTransitiveDependencies(string $layerName, array $previousLayers): array
{
if (in_array($layerName, $previousLayers, true)) {
throw CircularReferenceException::circularLayerDependency($layerName, $previousLayers);
}
$dependencies = [];
foreach ($this->allowedLayers[$layerName] ?? [] as $layer) {
if (0 === strncmp($layer, '+', 1)) {
$layer = ltrim($layer, '+');
$dependencies[] = $this->getTransitiveDependencies($layer, array_merge([$layerName], $previousLayers));
}
$dependencies[] = [$layer];
}

return [] === $dependencies ? [] : array_merge(...$dependencies);
}
}
57 changes: 57 additions & 0 deletions src/Layer/LayerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Qossmic\Deptrac\Layer;

use Qossmic\Deptrac\Layer\Exception\CircularReferenceException;

class LayerProvider
{
/**
* @var array<string, string[]>
*/
private array $allowedLayers;

/**
* @param array<string, string[]> $allowedLayers
*/
public function __construct(array $allowedLayers)
{
$this->allowedLayers = $allowedLayers;
}

/**
* @return string[]
*
* @throws CircularReferenceException
*/
public function getAllowedLayers(string $layerName): array
{
return array_values(array_unique($this->getTransitiveDependencies($layerName, [])));
}

/**
* @param string[] $previousLayers
*
* @return string[]
*
* @throws CircularReferenceException
*/
private function getTransitiveDependencies(string $layerName, array $previousLayers): array
{
if (in_array($layerName, $previousLayers, true)) {
throw CircularReferenceException::circularLayerDependency($layerName, $previousLayers);
}
$dependencies = [];
foreach ($this->allowedLayers[$layerName] ?? [] as $layer) {
if (0 === strncmp($layer, '+', 1)) {
$layer = ltrim($layer, '+');
$dependencies[] = $this->getTransitiveDependencies($layer, array_merge([$layerName], $previousLayers));
}
$dependencies[] = [$layer];
}

return [] === $dependencies ? [] : array_merge(...$dependencies);
}
}