-
Notifications
You must be signed in to change notification settings - Fork 134
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
Better Violations #1105
Merged
dbrumann
merged 6 commits into
qossmic:main
from
patrickkusebauch:feature/eventHandlers
Mar 10, 2023
Merged
Better Violations #1105
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
07b94e2
WIP: Better Violations
patrickkusebauch fc51e78
Outline of the proposal, test are still failing, I know...
patrickkusebauch 7e9ae49
Fixing tests
patrickkusebauch 307e6cc
Documentation updates
patrickkusebauch c39ba40
Rebase fixes
patrickkusebauch 3cbc61d
Merge branch 'main' into feature/eventHandlers
patrickkusebauch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Qossmic\Deptrac\Contract\Analyser; | ||
|
||
use Qossmic\Deptrac\Contract\Layer\LayerProvider; | ||
use Qossmic\Deptrac\Contract\Result\SkippedViolation; | ||
use Qossmic\Deptrac\Contract\Result\Violation; | ||
|
||
class EventHelper | ||
{ | ||
/** | ||
* @var array<string, string[]> | ||
*/ | ||
private array $unmatchedSkippedViolation; | ||
|
||
/** | ||
* @param array<string, string[]> $skippedViolations | ||
*/ | ||
public function __construct( | ||
private readonly array $skippedViolations, | ||
public readonly LayerProvider $layerProvider, | ||
) { | ||
$this->unmatchedSkippedViolation = $skippedViolations; | ||
} | ||
|
||
public function isViolationSkipped(string $depender, string $dependent): bool | ||
{ | ||
$matched = isset($this->skippedViolations[$depender]) && in_array($dependent, $this->skippedViolations[$depender], true); | ||
|
||
if ($matched && false !== ($key = array_search($dependent, $this->unmatchedSkippedViolation[$depender], true))) { | ||
unset($this->unmatchedSkippedViolation[$depender][$key]); | ||
} | ||
|
||
return $matched; | ||
} | ||
|
||
/** | ||
* @return array<string, string[]> | ||
*/ | ||
public function unmatchedSkippedViolations(): array | ||
{ | ||
return array_filter($this->unmatchedSkippedViolation); | ||
} | ||
|
||
public function addSkippableViolation(ProcessEvent $event, AnalysisResult $result, string $dependentLayer, ViolationCreatingInterface $violationCreatingRule): void | ||
{ | ||
if ($this->isViolationSkipped( | ||
$event->dependency->getDepender() | ||
->toString(), | ||
$event->dependency->getDependent() | ||
->toString() | ||
) | ||
) { | ||
$result->addRule(new SkippedViolation($event->dependency, $event->dependerLayer, $dependentLayer)); | ||
} else { | ||
$result->addRule(new Violation($event->dependency, $event->dependerLayer, $dependentLayer, $violationCreatingRule)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Qossmic\Deptrac\Contract\Analyser; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
interface ViolationCreatingInterface extends EventSubscriberInterface | ||
{ | ||
/** | ||
* @psalm-pure | ||
*/ | ||
public function ruleName(): string; | ||
|
||
/** | ||
* @psalm-pure | ||
*/ | ||
public function ruleDescription(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Core/Analyser/EventHandler/DependsOnDisallowedLayer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Qossmic\Deptrac\Core\Analyser\EventHandler; | ||
|
||
use Qossmic\Deptrac\Contract\Analyser\EventHelper; | ||
use Qossmic\Deptrac\Contract\Analyser\ProcessEvent; | ||
use Qossmic\Deptrac\Contract\Analyser\ViolationCreatingInterface; | ||
use Qossmic\Deptrac\Contract\Layer\CircularReferenceException; | ||
use Qossmic\Deptrac\Contract\Result\Error; | ||
|
||
use function in_array; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class DependsOnDisallowedLayer implements ViolationCreatingInterface | ||
{ | ||
public function __construct(private readonly EventHelper $eventHelper) | ||
{ | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
ProcessEvent::class => ['invoke', -1], | ||
]; | ||
} | ||
|
||
public function invoke(ProcessEvent $event): void | ||
{ | ||
$ruleset = $event->getResult(); | ||
|
||
try { | ||
$allowedLayers = $this->eventHelper->layerProvider->getAllowedLayers($event->dependerLayer); | ||
} catch (CircularReferenceException $circularReferenceException) { | ||
$ruleset->addError(new Error($circularReferenceException->getMessage())); | ||
$event->stopPropagation(); | ||
|
||
return; | ||
} | ||
|
||
foreach ($event->dependentLayers as $dependentLayer => $_) { | ||
if (!in_array($dependentLayer, $allowedLayers, true)) { | ||
$this->eventHelper->addSkippableViolation($event, $ruleset, $dependentLayer, $this); | ||
$event->stopPropagation(); | ||
} | ||
} | ||
} | ||
|
||
public function ruleName(): string | ||
{ | ||
return 'DependsOnDisallowedLayer'; | ||
} | ||
|
||
public function ruleDescription(): string | ||
{ | ||
return 'You are depending on token that is a part of a layer that you are not allowed to depend on.'; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea! I really like this interface and the naming around it 👍