Skip to content

Commit

Permalink
Merge pull request #499 from qossmic/classes-in-two-or-more-layers
Browse files Browse the repository at this point in the history
[POC] report warnings about classes are in two or more layers
  • Loading branch information
Simon Mönch authored Feb 26, 2021
2 parents 8716780 + bab943c commit 2bff6f1
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 39 deletions.
37 changes: 33 additions & 4 deletions src/OutputFormatter/ConsoleOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Qossmic\Deptrac\OutputFormatter;

use function count;
use Qossmic\Deptrac\AstRunner\AstMap\FileOccurrence;
use Qossmic\Deptrac\Console\Command\AnalyzeCommand;
use Qossmic\Deptrac\Console\Output;
Expand Down Expand Up @@ -57,6 +58,10 @@ public function finish(
$this->printErrors($context, $output);
}

if ($context->hasWarnings()) {
$this->printWarnings($context, $output);
}

$this->printSummary($context, $output);
}

Expand Down Expand Up @@ -104,10 +109,12 @@ private function printInheritPath(Output $output, InheritDependency $dependency)

private function printSummary(Context $context, Output $output): void
{
$violationCount = \count($context->violations());
$skippedViolationCount = \count($context->skippedViolations());
$uncoveredCount = \count($context->uncovered());
$allowedCount = \count($context->allowed());
$violationCount = count($context->violations());
$skippedViolationCount = count($context->skippedViolations());
$uncoveredCount = count($context->uncovered());
$allowedCount = count($context->allowed());
$warningsCount = count($context->warnings());
$errorsCount = count($context->errors());

$output->writeLineFormatted('');
$output->writeLineFormatted('Report:');
Expand All @@ -133,6 +140,20 @@ private function printSummary(Context $context, Output $output): void
)
);
$output->writeLineFormatted(sprintf('<info>Allowed: %d</info>', $allowedCount));
$output->writeLineFormatted(
sprintf(
'<fg=%s>Warnings: %d</>',
$warningsCount > 0 ? 'yellow' : 'default',
$warningsCount
)
);
$output->writeLineFormatted(
sprintf(
'<fg=%s>Errors: %d</>',
$errorsCount > 0 ? 'red' : 'default',
$errorsCount
)
);
}

private function printUncovered(Context $context, Output $output): void
Expand Down Expand Up @@ -173,4 +194,12 @@ private function printErrors(Context $context, Output $output): void
$output->writeLineFormatted(sprintf('<fg=red>[ERROR]</> %s', $error->toString()));
}
}

private function printWarnings(Context $context, Output $output): void
{
$output->writeLineFormatted('');
foreach ($context->warnings() as $error) {
$output->writeLineFormatted(sprintf('<fg=yellow>[WARNING]</> %s', $error->toString()));
}
}
}
11 changes: 11 additions & 0 deletions src/OutputFormatter/GithubActionsOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public function finish(Context $context, Output $output, OutputFormatterInput $o
if ($context->hasErrors()) {
$this->printErrors($context, $output);
}

if ($context->hasWarnings()) {
$this->printWarnings($context, $output);
}
}

private function determineLogLevel(Rule $rule): string
Expand Down Expand Up @@ -148,4 +152,11 @@ private function printErrors(Context $context, Output $output): void
$output->writeLineFormatted('::error ::'.$error->toString());
}
}

private function printWarnings(Context $context, Output $output): void
{
foreach ($context->warnings() as $error) {
$output->writeLineFormatted('::warning ::'.$error->toString());
}
}
}
33 changes: 28 additions & 5 deletions src/OutputFormatter/TableOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Qossmic\Deptrac\OutputFormatter;

use function count;
use Qossmic\Deptrac\Console\Command\AnalyzeCommand;
use Qossmic\Deptrac\Console\Output;
use Qossmic\Deptrac\Dependency\InheritDependency;
Expand All @@ -14,6 +15,7 @@
use Qossmic\Deptrac\RulesetEngine\SkippedViolation;
use Qossmic\Deptrac\RulesetEngine\Uncovered;
use Qossmic\Deptrac\RulesetEngine\Violation;
use Qossmic\Deptrac\RulesetEngine\Warning;
use Symfony\Component\Console\Helper\TableSeparator;

final class TableOutputFormatter implements OutputFormatterInterface
Expand Down Expand Up @@ -73,6 +75,10 @@ public function finish(
$this->printErrors($context, $output);
}

if ($context->hasWarnings()) {
$this->printWarnings($context, $output);
}

$this->printSummary($context, $output);
}

Expand Down Expand Up @@ -123,10 +129,12 @@ private function formatInheritPath(InheritDependency $dependency): string

private function printSummary(Context $context, Output $output): void
{
$violationCount = \count($context->violations());
$skippedViolationCount = \count($context->skippedViolations());
$uncoveredCount = \count($context->uncovered());
$allowedCount = \count($context->allowed());
$violationCount = count($context->violations());
$skippedViolationCount = count($context->skippedViolations());
$uncoveredCount = count($context->uncovered());
$allowedCount = count($context->allowed());
$warningsCount = count($context->warnings());
$errorsCount = count($context->errors());

$style = $output->getStyle();
$style->newLine();
Expand All @@ -136,7 +144,9 @@ private function printSummary(Context $context, Output $output): void
['Violations' => sprintf('<fg=%s>%d</>', $violationCount > 0 ? 'red' : 'default', $violationCount)],
['Skipped violations' => sprintf('<fg=%s>%d</>', $skippedViolationCount > 0 ? 'yellow' : 'default', $skippedViolationCount)],
['Uncovered' => sprintf('<fg=%s>%d</>', $uncoveredCount > 0 ? 'yellow' : 'default', $uncoveredCount)],
['Allowed' => $allowedCount]
['Allowed' => $allowedCount],
['Warnings' => sprintf('<fg=%s>%d</>', $warningsCount > 0 ? 'yellow' : 'default', $warningsCount)],
['Errors' => sprintf('<fg=%s>%d</>', $errorsCount > 0 ? 'red' : 'default', $errorsCount)]
);
}

Expand Down Expand Up @@ -175,4 +185,17 @@ static function (Error $error) {
)
);
}

private function printWarnings(Context $context, Output $output): void
{
$output->getStyle()->table(
['<fg=yellow>Warnings</>'],
array_map(
static function (Warning $warning) {
return [$warning->toString()];
},
$context->warnings()
)
);
}
}
9 changes: 8 additions & 1 deletion src/RulesetEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Qossmic\Deptrac\RulesetEngine\SkippedViolationHelper;
use Qossmic\Deptrac\RulesetEngine\Uncovered;
use Qossmic\Deptrac\RulesetEngine\Violation;
use Qossmic\Deptrac\RulesetEngine\Warning;

class RulesetEngine
{
Expand All @@ -24,13 +25,19 @@ public function process(
Configuration $configuration
): Context {
$rules = [];
$warnings = [];

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

foreach ($dependencyResult->getDependenciesAndInheritDependencies() as $dependency) {
$layerNames = $classNameLayerResolver->getLayersByClassName($dependency->getClassLikeNameA());

$classLikeANameString = $dependency->getClassLikeNameA()->toString();
if (!isset($warnings[$classLikeANameString]) && count($layerNames) > 1) {
$warnings[$classLikeANameString] = Warning::classLikeIsInMoreThanOneLayer($dependency->getClassLikeNameA(), $layerNames);
}

foreach ($layerNames as $layerName) {
$allowedDependencies = $configurationRuleset->getAllowedDependencies($layerName);

Expand Down Expand Up @@ -70,7 +77,7 @@ public function process(
}
}

return new Context($rules, $errors);
return new Context($rules, $errors, $warnings);
}

private function ignoreUncoveredInternalClass(Configuration $configuration, ClassLikeName $classLikeName): bool
Expand Down
30 changes: 26 additions & 4 deletions src/RulesetEngine/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Qossmic\Deptrac\RulesetEngine;

use function array_filter;
use function count;

final class Context
{
/**
Expand All @@ -14,15 +17,21 @@ final class Context
* @var Error[]
*/
private $errors;
/**
* @var Warning[]
*/
private $warnings;

/**
* @param Rule[] $rules
* @param Error[] $errors
* @param Rule[] $rules
* @param Error[] $errors
* @param Warning[] $warnings
*/
public function __construct(array $rules, array $errors)
public function __construct(array $rules, array $errors, array $warnings)
{
$this->rules = $rules;
$this->errors = $errors;
$this->warnings = $warnings;
}

/**
Expand Down Expand Up @@ -85,7 +94,7 @@ public function allowed(): array

public function hasErrors(): bool
{
return \count($this->errors) > 0;
return count($this->errors) > 0;
}

/**
Expand All @@ -95,4 +104,17 @@ public function errors(): array
{
return $this->errors;
}

public function hasWarnings(): bool
{
return count($this->warnings) > 0;
}

/**
* @return Warning[]
*/
public function warnings(): array
{
return $this->warnings;
}
}
39 changes: 39 additions & 0 deletions src/RulesetEngine/Warning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Qossmic\Deptrac\RulesetEngine;

use Qossmic\Deptrac\AstRunner\AstMap\ClassLikeName;

final class Warning
{
/**
* @var string
*/
private $message;

private function __construct(string $message)
{
$this->message = $message;
}

/**
* @param string[] $layerNames
*/
public static function classLikeIsInMoreThanOneLayer(
ClassLikeName $getClassLikeNameA,
array $layerNames
): self {
return new self(sprintf(
'%s is in more than one layer ["%s"]. It is recommended that one class should only be in one layer.',
$getClassLikeNameA->toString(),
implode('", "', $layerNames)
));
}

public function toString(): string
{
return $this->message;
}
}
2 changes: 1 addition & 1 deletion tests/OutputFormatter/BaselineOutputFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function testBasic(array $rules, string $expectedOutput): void

$formatter = new BaselineOutputFormatter();
$formatter->finish(
new Context($rules, []),
new Context($rules, [], []),
$this->createSymfonyOutput($output),
new OutputFormatterInput(['baseline-dump' => $generatedBaselineFile])
);
Expand Down
Loading

0 comments on commit 2bff6f1

Please sign in to comment.