1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koriit\PHPDeps\Commands; |
4
|
|
|
|
5
|
|
|
use Koriit\PHPDeps\Config\Exceptions\InvalidConfig; |
6
|
|
|
use Koriit\PHPDeps\Config\Exceptions\InvalidSchema; |
7
|
|
|
use Koriit\PHPDeps\ExitCodes; |
8
|
|
|
use Koriit\PHPDeps\Graph\DirectedGraph; |
9
|
|
|
use Koriit\PHPDeps\Helpers\InputHelper; |
10
|
|
|
use Koriit\PHPDeps\Helpers\ModulesHelper; |
11
|
|
|
use Koriit\PHPDeps\Modules\ModuleReader; |
12
|
|
|
use Koriit\PHPDeps\Tokenizer\Exceptions\MalformedFile; |
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
18
|
|
|
|
19
|
|
|
class DependenciesCommand extends Command |
20
|
|
|
{ |
21
|
|
|
/** @var ModuleReader */ |
22
|
|
|
private $modulesReader; |
23
|
|
|
|
24
|
|
|
/** @var ModulesHelper */ |
25
|
|
|
private $modulesHelper; |
26
|
|
|
|
27
|
|
|
/** @var InputHelper */ |
28
|
|
|
private $inputHelper; |
29
|
|
|
|
30
|
|
View Code Duplication |
public function __construct(ModulesHelper $modulesHelper, InputHelper $inputHelper, ModuleReader $modulesReader) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
|
34
|
|
|
$this->modulesReader = $modulesReader; |
35
|
|
|
$this->modulesHelper = $modulesHelper; |
36
|
|
|
$this->inputHelper = $inputHelper; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function configure() |
40
|
|
|
{ |
41
|
|
|
$this |
42
|
|
|
->setName('dependencies') |
43
|
|
|
->setDescription('Lists module dependencies') |
44
|
|
|
->addOption('filter', 'f', InputOption::VALUE_OPTIONAL, 'Comma separated list of module names to show', '') |
45
|
|
|
->addOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Custom location of configuration file', './phpdeps.xml'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param InputInterface $input |
50
|
|
|
* @param OutputInterface $output |
51
|
|
|
* |
52
|
|
|
* @throws InvalidConfig |
53
|
|
|
* @throws InvalidSchema |
54
|
|
|
* @throws MalformedFile |
55
|
|
|
* |
56
|
|
|
* @return int |
57
|
|
|
*/ |
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
|
|
$io = new SymfonyStyle($input, $output); |
61
|
|
|
|
62
|
|
|
$config = $this->inputHelper->readConfig($input); |
63
|
|
|
$filters = $this->inputHelper->readFilters($input); |
64
|
|
|
|
65
|
|
|
$modules = $this->modulesHelper->findModules($config); |
66
|
|
|
if (!$this->modulesHelper->validateModules($modules, $io)) { |
67
|
|
|
return ExitCodes::UNEXPECTED_ERROR; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$dependenciesGraph = $this->modulesReader->generateDependenciesGraph($modules); |
71
|
|
|
$this->displayModules($dependenciesGraph, $io, $filters); |
72
|
|
|
|
73
|
|
|
return ExitCodes::OK; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param DirectedGraph $dependenciesGraph |
78
|
|
|
* @param SymfonyStyle $io |
79
|
|
|
* @param string[] $filters Filtered module names |
80
|
|
|
*/ |
81
|
|
|
private function displayModules(DirectedGraph $dependenciesGraph, SymfonyStyle $io, array $filters) |
82
|
|
|
{ |
83
|
|
View Code Duplication |
if ($io->isVerbose()) { |
|
|
|
|
84
|
|
|
if (empty($filters)) { |
85
|
|
|
$io->writeln('No filters applied.'); |
86
|
|
|
} else { |
87
|
|
|
$io->writeln('Filters applied: ' . \implode(', ', $filters)); |
88
|
|
|
} |
89
|
|
|
$io->newLine(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$vertices = $this->modulesHelper->filterVerticesByModuleName($dependenciesGraph->getVertices(), $filters); |
93
|
|
|
|
94
|
|
|
$i = 1; |
95
|
|
|
foreach ($vertices as $vertex) { |
96
|
|
|
$this->modulesHelper->renderModuleDependencies($io, $vertex, $i++); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.