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

github actions show inherit path #402

Merged
merged 1 commit into from
Oct 23, 2020
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
40 changes: 34 additions & 6 deletions src/OutputFormatter/GithubActionsOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SensioLabs\Deptrac\Console\Command\AnalyzeCommand;
use SensioLabs\Deptrac\Console\Output;
use SensioLabs\Deptrac\Dependency\InheritDependency;
use SensioLabs\Deptrac\Env;
use SensioLabs\Deptrac\RulesetEngine\Context;
use SensioLabs\Deptrac\RulesetEngine\Rule;
Expand Down Expand Up @@ -63,18 +64,27 @@ public function finish(Context $context, Output $output, OutputFormatterInput $o
if (!$rule instanceof Violation && !$rule instanceof SkippedViolation) {
continue;
}

$dependency = $rule->getDependency();
$output->writeLineFormatted(sprintf(
'::%s file=%s,line=%s::%s%s must not depend on %s (%s on %s)',
$this->determineLogLevel($rule),
$dependency->getFileOccurrence()->getFilepath(),
$dependency->getFileOccurrence()->getLine(),

$message = sprintf(
'%s%s must not depend on %s (%s on %s)',
$rule instanceof SkippedViolation ? '[SKIPPED] ' : '',
$dependency->getClassLikeNameA()->toString(),
$dependency->getClassLikeNameB()->toString(),
$rule->getLayerA(),
$rule->getLayerB()
);

if ($dependency instanceof InheritDependency) {
$message .= '%0A'.$this->inheritPathMessage($dependency);
}

$output->writeLineFormatted(sprintf(
'::%s file=%s,line=%s::%s',
$this->determineLogLevel($rule),
$dependency->getFileOccurrence()->getFilepath(),
$dependency->getFileOccurrence()->getLine(),
$message
));
}

Expand Down Expand Up @@ -117,4 +127,22 @@ private function printUncovered(Context $context, Output $output): void
);
}
}

private function inheritPathMessage(InheritDependency $dependency): string
{
$buffer = [];
$astInherit = $dependency->getInheritPath();
foreach ($astInherit->getPath() as $p) {
array_unshift($buffer, sprintf('%s::%d', $p->getClassLikeName()->toString(), $p->getFileOccurrence()->getLine()));
}

$buffer[] = sprintf('%s::%d', $astInherit->getClassLikeName()->toString(), $astInherit->getFileOccurrence()->getLine());
$buffer[] = sprintf(
'%s::%d',
$dependency->getOriginalDependency()->getClassLikeNameB()->toString(),
$dependency->getOriginalDependency()->getFileOccurrence()->getLine()
);

return implode(' ->%0A', $buffer);
}
}
25 changes: 24 additions & 1 deletion tests/OutputFormatter/GithubActionsOutputFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Tests\SensioLabs\Deptrac\OutputFormatter;

use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\AstRunner\AstMap\AstInherit;
use SensioLabs\Deptrac\AstRunner\AstMap\ClassLikeName;
use SensioLabs\Deptrac\AstRunner\AstMap\FileOccurrence;
use SensioLabs\Deptrac\Console\Command\AnalyzeCommand;
use SensioLabs\Deptrac\Console\Symfony\Style;
use SensioLabs\Deptrac\Console\Symfony\SymfonyOutput;
use SensioLabs\Deptrac\Dependency\Dependency;
use SensioLabs\Deptrac\Dependency\InheritDependency;
use SensioLabs\Deptrac\OutputFormatter\GithubActionsOutputFormatter;
use SensioLabs\Deptrac\OutputFormatter\OutputFormatterInput;
use SensioLabs\Deptrac\RulesetEngine\Context;
Expand Down Expand Up @@ -47,7 +49,7 @@ public function testFinish(array $rules, string $expectedOutput): void
self::assertEquals($expectedOutput, $bufferedOutput->fetch());
}

public function finishProvider()
public function finishProvider(): iterable
{
yield 'No Rules, No Output' => [
[],
Expand Down Expand Up @@ -89,6 +91,27 @@ public function finishProvider()
],
"::warning file=/home/testuser/originalA.php,line=12::ACME\OriginalA has uncovered dependency on ACME\OriginalB (LayerA)\n",
];

yield 'Inherit dependency' => [
[
new Violation(
new InheritDependency(
ClassLikeName::fromFQCN('ClassA'),
ClassLikeName::fromFQCN('ClassB'),
new Dependency($originalA, $originalB, FileOccurrence::fromFilepath('originalA.php', 12)),
AstInherit::newExtends(ClassLikeName::fromFQCN('ClassInheritA'), FileOccurrence::fromFilepath('originalA.php', 3))
->withPath([
AstInherit::newExtends(ClassLikeName::fromFQCN('ClassInheritB'), FileOccurrence::fromFilepath('originalA.php', 4)),
AstInherit::newExtends(ClassLikeName::fromFQCN('ClassInheritC'), FileOccurrence::fromFilepath('originalA.php', 5)),
AstInherit::newExtends(ClassLikeName::fromFQCN('ClassInheritD'), FileOccurrence::fromFilepath('originalA.php', 6)),
])
),
'LayerA',
'LayerB'
),
],
"::error file=originalA.php,line=12::ClassA must not depend on ClassB (LayerA on LayerB)%0AClassInheritD::6 ->%0AClassInheritC::5 ->%0AClassInheritB::4 ->%0AClassInheritA::3 ->%0AACME\OriginalB::12\n",
];
}

public function testGithubActionsOutputFormatterIsNotEnabledByDefault(): void
Expand Down