Skip to content

Commit

Permalink
Merge pull request #266 from sensiolabs-de/track-uncovered-dependencies
Browse files Browse the repository at this point in the history
POC: track uncovered dependencies
  • Loading branch information
Simon Mönch authored Nov 20, 2019
2 parents c79156b + c099d55 commit 27270b6
Show file tree
Hide file tree
Showing 21 changed files with 502 additions and 530 deletions.
21 changes: 4 additions & 17 deletions src/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SensioLabs\Deptrac\Collector\Registry;
use SensioLabs\Deptrac\Configuration\Configuration;
use SensioLabs\Deptrac\Dependency\Resolver;
use SensioLabs\Deptrac\RulesetEngine\Context;

class Analyser
{
Expand All @@ -29,7 +30,7 @@ public function __construct(
$this->rulesetEngine = $rulesetEngine;
}

public function analyse(Configuration $configuration): DependencyContext
public function analyse(Configuration $configuration): Context
{
$astMap = $this->astRunner->createAstMapByFiles($this->fileResolver->resolve($configuration));
$dependencyResult = $this->resolver->resolve($astMap);
Expand All @@ -38,24 +39,10 @@ public function analyse(Configuration $configuration): DependencyContext
new ClassNameLayerResolver($configuration, $astMap, $this->collectorRegistry)
);

/** @var RulesetEngine\RulesetViolation[] $violations */
$violations = $this->rulesetEngine->getViolations(
return $this->rulesetEngine->process(
$dependencyResult,
$classNameLayerResolver,
$configuration->getRuleset()
);

$skippedViolations = $this->rulesetEngine->getSkippedViolations(
$violations,
$configuration->getSkipViolations()
);

return new DependencyContext(
$astMap,
$dependencyResult,
$classNameLayerResolver,
$violations,
$skippedViolations
$configuration
);
}
}
6 changes: 3 additions & 3 deletions src/Console/Command/AnalyzeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$this->printCollectViolations($output);
$dependencyContext = $this->analyser->analyse($configuration);
$context = $this->analyser->analyse($configuration);

$this->printFormattingStart($output);

foreach ($this->formatterFactory->getActiveFormatters($input) as $formatter) {
try {
$formatter->finish(
$dependencyContext,
$context,
$output,
$this->formatterFactory->getOutputFormatterInput($formatter, $input)
);
Expand All @@ -87,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

return $dependencyContext->hasViolations() ? 1 : 0;
return $context->hasViolations() ? 1 : 0;
}

protected function printBanner(OutputInterface $output): void
Expand Down
111 changes: 0 additions & 111 deletions src/DependencyContext.php

This file was deleted.

105 changes: 65 additions & 40 deletions src/OutputFormatter/ConsoleOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use SensioLabs\Deptrac\AstRunner\AstMap\AstInherit;
use SensioLabs\Deptrac\Dependency\InheritDependency;
use SensioLabs\Deptrac\DependencyContext;
use SensioLabs\Deptrac\RulesetEngine\RulesetViolation;
use SensioLabs\Deptrac\RulesetEngine\Context;
use SensioLabs\Deptrac\RulesetEngine\Rule;
use SensioLabs\Deptrac\RulesetEngine\SkippedViolation;
use SensioLabs\Deptrac\RulesetEngine\Violation;
use Symfony\Component\Console\Output\OutputInterface;

class ConsoleOutputFormatter implements OutputFormatterInterface
final class ConsoleOutputFormatter implements OutputFormatterInterface
{
public function getName(): string
{
Expand All @@ -28,73 +30,96 @@ public function enabledByDefault(): bool
}

public function finish(
DependencyContext $dependencyContext,
Context $context,
OutputInterface $output,
OutputFormatterInput $outputFormatterInput
): void {
foreach ($dependencyContext->getViolations() as $violation) {
if ($violation->getDependency() instanceof InheritDependency) {
$this->handleInheritDependency($violation, $output, $dependencyContext->isViolationSkipped($violation));
foreach ($context->all() as $rule) {
if (!$rule instanceof Violation && !$rule instanceof SkippedViolation) {
continue;
}

$this->handleDependency($violation, $output, $dependencyContext->isViolationSkipped($violation));
}
if ($rule->getDependency() instanceof InheritDependency) {
$this->handleInheritDependency($rule, $output);
continue;
}

$violationCount = \count($dependencyContext->getViolations());
$skippedViolationCount = \count($dependencyContext->getSkippedViolations());
if ($violationCount > $skippedViolationCount) {
$output->writeln(
sprintf(
'Found <error>%s Violations</error>'.($skippedViolationCount ? ' and %s Violations skipped' : ''),
$violationCount - $skippedViolationCount,
$skippedViolationCount
)
);
} else {
$output->writeln(
sprintf(
'Found <info>%s Violations</info>'.($skippedViolationCount ? ' and %s Violations skipped' : ''),
$violationCount - $skippedViolationCount,
$skippedViolationCount
)
);
$this->handleDependency($rule, $output);
}

$violationCount = \count($context->violations());
$skippedViolationCount = \count($context->skippedViolations());
$uncoveredCount = \count($context->uncovered());
$allowedCount = \count($context->allowed());

$output->writeln('');
$output->writeln('Report:');
$output->writeln(
sprintf(
'<%1$s>Violations: %2$d</%1$s>',
$violationCount > 0 ? 'error' : 'info',
$violationCount
)
);
$output->writeln(
sprintf(
'<%1$s>Skipped violations: %2$d</%1$s>',
$skippedViolationCount > 0 ? 'comment' : 'info',
$skippedViolationCount
)
);
$output->writeln(
sprintf(
'<%1$s>Uncovered: %2$d</%1$s>',
$uncoveredCount > 0 ? 'comment' : 'info',
$uncoveredCount
)
);
$output->writeln(sprintf('<info>Allowed: %d</info>', $allowedCount));
}

private function handleInheritDependency(RulesetViolation $violation, OutputInterface $output, bool $isSkipped)
/**
* @param Violation|SkippedViolation $rule
*/
private function handleInheritDependency(Rule $rule, OutputInterface $output): void
{
/** @var InheritDependency $dependency */
$dependency = $violation->getDependency();
$dependency = $rule->getDependency();

$output->writeln(
sprintf(
"%s<info>%s</info> must not depend on <info>%s</info> (%s on %s) \n%s",
$isSkipped ? '[SKIPPED] ' : '',
$rule instanceof SkippedViolation ? '[SKIPPED] ' : '',
$dependency->getClassA(),
$dependency->getClassB(),
$violation->getLayerA(),
$violation->getLayerB(),
$rule->getLayerA(),
$rule->getLayerB(),
$this->formatPath($dependency->getPath(), $dependency)
)
);
}

private function handleDependency(RulesetViolation $violation, OutputInterface $output, bool $isSkipped)
/**
* @param Violation|SkippedViolation $rule
*/
private function handleDependency(Rule $rule, OutputInterface $output): void
{
$dependency = $rule->getDependency();

$output->writeln(
sprintf(
'%s<info>%s</info>::%s must not depend on <info>%s</info> (%s on %s)',
$isSkipped ? '[SKIPPED] ' : '',
$violation->getDependency()->getClassA(),
$violation->getDependency()->getClassALine(),
$violation->getDependency()->getClassB(),
$violation->getLayerA(),
$violation->getLayerB()
$rule instanceof SkippedViolation ? '[SKIPPED] ' : '',
$dependency->getClassA(),
$dependency->getClassALine(),
$dependency->getClassB(),
$rule->getLayerA(),
$rule->getLayerB()
)
);
}

private function formatPath(AstInherit $astInherit, InheritDependency $dependency)
private function formatPath(AstInherit $astInherit, InheritDependency $dependency): string
{
$buffer = [];
foreach ($astInherit->getPath() as $p) {
Expand Down
Loading

0 comments on commit 27270b6

Please sign in to comment.