From 5cdb255402548c7d9b2e515f8fd668ccb27c5be3 Mon Sep 17 00:00:00 2001 From: patrickkusebauch Date: Wed, 1 Jun 2022 19:25:06 +0200 Subject: [PATCH] LayerProvider.php as a new extension API point --- config/services.php | 7 ++- .../EventHandler/AllowDependencyHandler.php | 50 ++-------------- src/Layer/LayerProvider.php | 57 +++++++++++++++++++ 3 files changed, 67 insertions(+), 47 deletions(-) create mode 100644 src/Layer/LayerProvider.php diff --git a/config/services.php b/config/services.php index acd41bece..aa01ba039 100644 --- a/config/services.php +++ b/config/services.php @@ -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; @@ -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) diff --git a/src/Analyser/EventHandler/AllowDependencyHandler.php b/src/Analyser/EventHandler/AllowDependencyHandler.php index 0977964f9..7261cb512 100644 --- a/src/Analyser/EventHandler/AllowDependencyHandler.php +++ b/src/Analyser/EventHandler/AllowDependencyHandler.php @@ -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 - */ - private array $allowedLayers; + private LayerProvider $layerProvider; - /** - * @param array $allowedLayers - */ - public function __construct(array $allowedLayers) + public function __construct(LayerProvider $layerProvider) { - $this->allowedLayers = $allowedLayers; + $this->layerProvider = $layerProvider; } public function __invoke(ProcessEvent $event): void @@ -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(); @@ -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); - } } diff --git a/src/Layer/LayerProvider.php b/src/Layer/LayerProvider.php new file mode 100644 index 000000000..253717ad1 --- /dev/null +++ b/src/Layer/LayerProvider.php @@ -0,0 +1,57 @@ + + */ + private array $allowedLayers; + + /** + * @param array $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); + } +}