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

Ruleset config refactor #646

Merged
merged 1 commit into from
Jul 26, 2021
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
2 changes: 1 addition & 1 deletion src/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public function analyse(Configuration $configuration): Context
$dependencyResult = $this->resolver->resolve($astMap, $configuration->getAnalyser());
$classLikeLayerResolver = $this->classLikeLayerResolverFactory->create($configuration, $astMap);

return $this->rulesetEngine->process($dependencyResult, $classLikeLayerResolver, $configuration);
return $this->rulesetEngine->process($dependencyResult, $classLikeLayerResolver, $configuration->getRuleset());
}
}
16 changes: 1 addition & 15 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class Configuration
/** @var string[] */
private array $excludeFiles;
private ConfigurationRuleset $ruleset;
private ConfigurationSkippedViolation $skipViolations;
private bool $ignoreUncoveredInternalClasses;
/** @var array<string, string> */
private array $parameters;
private ConfigurationAnalyser $analyser;
Expand Down Expand Up @@ -112,11 +110,9 @@ static function (?array $rules): array {
}

$this->parameters = $options['parameters'];
$this->ruleset = ConfigurationRuleset::fromArray($options['ruleset']);
$this->ruleset = ConfigurationRuleset::fromArray($options['ruleset'], $options['skip_violations'], (bool) $options['ignore_uncovered_internal_classes']);
$this->paths = $options['paths'];
$this->skipViolations = ConfigurationSkippedViolation::fromArray($options['skip_violations']);
$this->excludeFiles = (array) $options['exclude_files'];
$this->ignoreUncoveredInternalClasses = (bool) $options['ignore_uncovered_internal_classes'];
$this->formatters = (array) $options['formatters'];
$this->analyser = ConfigurationAnalyser::fromArray($options['analyser']);
}
Expand Down Expand Up @@ -150,16 +146,6 @@ public function getRuleset(): ConfigurationRuleset
return $this->ruleset;
}

public function getSkipViolations(): ConfigurationSkippedViolation
{
return $this->skipViolations;
}

public function ignoreUncoveredInternalClasses(): bool
{
return $this->ignoreUncoveredInternalClasses;
}

/**
* @return array<string, string>
*/
Expand Down
30 changes: 26 additions & 4 deletions src/Configuration/ConfigurationRuleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,29 @@ final class ConfigurationRuleset
/** @var array<string, string[]> */
private array $layerMap;

/** @var array<string, string[]> */
private array $skipViolations;

private bool $ignoreUncoveredInternalClasses;

/**
* @param array<string, string[]> $arr
* @param array<string, string[]> $layerMap
* @param array<string, string[]> $skipViolations
*/
public static function fromArray(array $arr): self
public static function fromArray(array $layerMap, array $skipViolations, bool $ignoreUncoveredInternalClasses): self
{
return new self($arr);
return new self($layerMap, $skipViolations, $ignoreUncoveredInternalClasses);
}

/**
* @param array<string, string[]> $layerMap
* @param array<string, string[]> $skipViolations
*/
private function __construct(array $layerMap)
private function __construct(array $layerMap, array $skipViolations, bool $ignoreUncoveredInternalClasses)
{
$this->layerMap = $layerMap;
$this->skipViolations = $skipViolations;
$this->ignoreUncoveredInternalClasses = $ignoreUncoveredInternalClasses;
}

/**
Expand Down Expand Up @@ -60,4 +69,17 @@ private function getTransitiveDependencies(string $layerName, array $previousLay

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

/**
* @return array<string, string[]>
*/
public function getSkipViolations(): array
{
return $this->skipViolations;
}

public function ignoreUncoveredInternalClasses(): bool
{
return $this->ignoreUncoveredInternalClasses;
}
}
35 changes: 0 additions & 35 deletions src/Configuration/ConfigurationSkippedViolation.php

This file was deleted.

11 changes: 5 additions & 6 deletions src/RulesetEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use JetBrains\PHPStormStub\PhpStormStubsMap;
use Qossmic\Deptrac\AstRunner\AstMap\ClassLikeName;
use Qossmic\Deptrac\AstRunner\AstMap\TokenName;
use Qossmic\Deptrac\Configuration\Configuration;
use Qossmic\Deptrac\Configuration\ConfigurationRuleset;
use Qossmic\Deptrac\Dependency\Result;
use Qossmic\Deptrac\RulesetEngine\Allowed;
use Qossmic\Deptrac\RulesetEngine\Context;
Expand All @@ -24,14 +24,13 @@ class RulesetEngine
public function process(
Result $dependencyResult,
TokenLayerResolverInterface $tokenLayerResolver,
Configuration $configuration
ConfigurationRuleset $configurationRuleset
): Context {
$rules = [];
$warnings = [];
$errors = [];

$configurationRuleset = $configuration->getRuleset();
$skippedViolationHelper = new SkippedViolationHelper($configuration->getSkipViolations());
$skippedViolationHelper = new SkippedViolationHelper($configurationRuleset->getSkipViolations());

foreach ($dependencyResult->getDependenciesAndInheritDependencies() as $dependency) {
$dependant = $dependency->getDependant();
Expand All @@ -53,7 +52,7 @@ public function process(
$dependeeLayerNames = $tokenLayerResolver->getLayersByTokenName($dependee);

if (0 === count($dependeeLayerNames)) {
if ($dependee instanceof ClassLikeName && !$this->ignoreUncoveredInternalClass($configuration, $dependee)) {
if ($dependee instanceof ClassLikeName && !$this->ignoreUncoveredInternalClass($configurationRuleset, $dependee)) {
$rules[] = new Uncovered($dependency, $dependantLayerName);
}
continue;
Expand Down Expand Up @@ -88,7 +87,7 @@ public function process(
return new Context($rules, $errors, $warnings);
}

private function ignoreUncoveredInternalClass(Configuration $configuration, TokenName $tokenName): bool
private function ignoreUncoveredInternalClass(ConfigurationRuleset $configuration, TokenName $tokenName): bool
{
return !$tokenName instanceof ClassLikeName || ($configuration->ignoreUncoveredInternalClasses() && isset(PhpStormStubsMap::CLASSES[$tokenName->toString()]));
}
Expand Down
11 changes: 6 additions & 5 deletions src/RulesetEngine/SkippedViolationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Qossmic\Deptrac\RulesetEngine;

use Qossmic\Deptrac\Configuration\ConfigurationSkippedViolation;

/**
* @psalm-immutable
*/
Expand All @@ -21,10 +19,13 @@ final class SkippedViolationHelper
*/
private array $unmatchedSkippedViolation;

public function __construct(ConfigurationSkippedViolation $configuration)
/**
* @param array<string, string[]> $skipViolations
*/
public function __construct(array $skipViolations)
{
$this->skippedViolation = $configuration->all();
$this->unmatchedSkippedViolation = $configuration->all();
$this->skippedViolation = $skipViolations;
$this->unmatchedSkippedViolation = $skipViolations;
}

public function isViolationSkipped(string $dependant, string $dependee): bool
Expand Down
6 changes: 3 additions & 3 deletions tests/Configuration/ConfigurationRulesetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class ConfigurationRulesetTest extends TestCase
public function testFromArray(): void
{
$configurationRuleSet = ConfigurationRuleset::fromArray(
['foo' => ['bar'], 'lala' => ['xx', 'yy']]
['foo' => ['bar'], 'lala' => ['xx', 'yy']], [], false
);

self::assertEquals(['bar'], $configurationRuleSet->getAllowedDependencies('foo'));
Expand All @@ -30,7 +30,7 @@ public function testFromArrayTransitive(): void
'qux' => ['+quuz', '+grault'],
'quuz' => ['corge'],
'grault' => ['+foo', 'baz'],
]);
], [], false);

self::assertEquals(['baz', 'bar'], $configurationRuleSet->getAllowedDependencies('foo'));
self::assertEquals(['corge', 'quuz', 'baz', 'bar', 'foo', 'grault'], $configurationRuleSet->getAllowedDependencies('qux'));
Expand All @@ -42,7 +42,7 @@ public function testFromArrayTransitiveCircular(): void
'a' => ['+b'],
'b' => ['+c'],
'c' => ['+a'],
]);
], [], false);
$this->expectException(\InvalidArgumentException::class);
$configurationRuleSet->getAllowedDependencies('a');
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function testFromArray(): void
self::assertEquals(['foo', 'bar'], $configuration->getPaths());
self::assertEquals(['foo2', 'bar2'], $configuration->getExcludeFiles());
self::assertEquals(['xx', 'yy'], $configuration->getRuleset()->getAllowedDependencies('some_name'));
self::assertTrue($configuration->ignoreUncoveredInternalClasses());
self::assertTrue($configuration->getRuleset()->ignoreUncoveredInternalClasses());
}

public function testExcludedFilesAreOptional(): void
Expand Down Expand Up @@ -188,7 +188,7 @@ public function testSkipViolations(): void
'BarClass',
'AnotherClass',
],
], $configuration->getSkipViolations()->all());
], $configuration->getRuleset()->getSkipViolations());
}

public function testIgnoreUncoveredInternalClassesSetToFalse(): void
Expand All @@ -200,6 +200,6 @@ public function testIgnoreUncoveredInternalClassesSetToFalse(): void
'ignore_uncovered_internal_classes' => false,
]);

self::assertFalse($configuration->ignoreUncoveredInternalClasses());
self::assertFalse($configuration->getRuleset()->ignoreUncoveredInternalClasses());
}
}
2 changes: 1 addition & 1 deletion tests/Configuration/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testLoadWithBaseline(): void
'BarClass',
],
],
$configuration->getSkipViolations()->all()
$configuration->getRuleset()->getSkipViolations()
);
}

Expand Down
13 changes: 4 additions & 9 deletions tests/RulesetEngine/SkippedViolationHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

use PHPUnit\Framework\TestCase;
use Qossmic\Deptrac\AstRunner\AstMap\ClassLikeName;
use Qossmic\Deptrac\Configuration\ConfigurationSkippedViolation;
use Qossmic\Deptrac\RulesetEngine\SkippedViolationHelper;

final class SkippedViolationHelperTest extends TestCase
{
public function testIsViolationSkipped(): void
{
$configuration = ConfigurationSkippedViolation::fromArray(
[
$configuration = [
'ClassWithOneDep' => [
'DependencyClass',
],
Expand All @@ -24,8 +22,7 @@ public function testIsViolationSkipped(): void
'DependencyClass2',
'DependencyClass2',
],
]
);
];
$helper = new SkippedViolationHelper($configuration);

self::assertTrue(
Expand Down Expand Up @@ -56,8 +53,7 @@ public function testIsViolationSkipped(): void

public function testUnmatchedSkippedViolations(): void
{
$configuration = ConfigurationSkippedViolation::fromArray(
[
$configuration = [
'ClassWithOneDep' => [
'DependencyClass',
],
Expand All @@ -67,8 +63,7 @@ public function testUnmatchedSkippedViolations(): void
'DependencyClass2',
'DependencyClass2',
],
]
);
];
$helper = new SkippedViolationHelper($configuration);

self::assertTrue(
Expand Down
6 changes: 3 additions & 3 deletions tests/RulesetEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function testProcess(
$context = (new RulesetEngine())->process(
$dependencyResult,
$tokenLayerResolver->reveal(),
$configuration
$configuration->getRuleset()
);

self::assertCount($expectedCount, $context->violations());
Expand Down Expand Up @@ -316,7 +316,7 @@ public function testGetSkippedViolations(array $dependenciesAsArray, array $clas
$context = (new RulesetEngine())->process(
$dependencyResult,
$tokenLayerResolver->reveal(),
$configuration
$configuration->getRuleset()
);

self::assertCount($expectedSkippedViolationCount, $context->skippedViolations());
Expand Down Expand Up @@ -371,7 +371,7 @@ public function testIgnoreUncoveredInternalClasses(array $dependenciesAsArray, a
$context = (new RulesetEngine())->process(
$dependencyResult,
$tokenLayerResolver->reveal(),
$configuration
$configuration->getRuleset()
);

self::assertCount($expectedUncoveredCount, $context->uncovered());
Expand Down