CycleDetectCommand::getCommandName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace DependencyAnalyzer\Commands;
5
6
use DependencyAnalyzer\Inspector\CycleDetector;
7
use DependencyAnalyzer\DependencyGraph;
8
use LucidFrame\Console\ConsoleTable;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class CycleDetectCommand extends AnalyzeDependencyCommand
12
{
13
    protected function getCommandName(): string
14
    {
15
        return 'detect-cycle';
16
    }
17
18
    protected function getCommandDescription(): string
19
    {
20
        return 'detect cycle dependency in dependency map';
21
    }
22
23
    protected function inspectDependencyGraph(DependencyGraph $graph, OutputInterface $output): int
24
    {
25
        $response = (new CycleDetector())->inspect($graph);
26
27
        $output->writeln("{$response->count()} cycles detected.");
28
        $output->writeln('');
29
30
        foreach ($response->getCycles() as $cycle) {
31
            $table = (new ConsoleTable())
32
                ->addHeader('class')
33
                ->addHeader('');
34
            foreach ($cycle as $index => $class) {
35
                $haveNextClass = (bool)($index < (count($cycle) - 1));
36
                $table->addRow([$class, $haveNextClass ? '->' : '']);
37
            }
38
39
            $output->write($table->getTable());
40
            $output->writeln('');
41
        }
42
43
        return count($response) > 0 ? 1 : 0;
44
    }
45
}
46