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\Graph\Vertex; |
10
|
|
|
use Koriit\PHPDeps\Helpers\InputHelper; |
11
|
|
|
use Koriit\PHPDeps\Helpers\ModulesHelper; |
12
|
|
|
use Koriit\PHPDeps\Modules\Module; |
13
|
|
|
use Koriit\PHPDeps\Modules\ModuleReader; |
14
|
|
|
use Koriit\PHPDeps\Tokenizer\Exceptions\MalformedFile; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
21
|
|
|
|
22
|
|
|
class DependCommand extends Command |
23
|
|
|
{ |
24
|
|
|
/** @var ModuleReader */ |
25
|
|
|
private $modulesReader; |
26
|
|
|
|
27
|
|
|
/** @var ModulesHelper */ |
28
|
|
|
private $modulesHelper; |
29
|
|
|
|
30
|
|
|
/** @var InputHelper */ |
31
|
|
|
private $inputHelper; |
32
|
|
|
|
33
|
|
View Code Duplication |
public function __construct(ModulesHelper $modulesHelper, InputHelper $inputHelper, ModuleReader $modulesReader) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
parent::__construct(); |
36
|
|
|
|
37
|
|
|
$this->modulesReader = $modulesReader; |
38
|
|
|
$this->modulesHelper = $modulesHelper; |
39
|
|
|
$this->inputHelper = $inputHelper; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function configure() |
43
|
|
|
{ |
44
|
|
|
$this |
45
|
|
|
->setName('depend') |
46
|
|
|
->setDescription('Lists modules which depend on provided module') |
47
|
|
|
->addArgument('module', InputArgument::REQUIRED, 'Name of module to search for') |
48
|
|
|
->addOption('filter', 'f', InputOption::VALUE_OPTIONAL, 'Comma separated list of module names to show', '') |
49
|
|
|
->addOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Custom location of configuration file', './phpdeps.xml'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param InputInterface $input |
54
|
|
|
* @param OutputInterface $output |
55
|
|
|
* |
56
|
|
|
* @throws InvalidConfig |
57
|
|
|
* @throws InvalidSchema |
58
|
|
|
* @throws MalformedFile |
59
|
|
|
* |
60
|
|
|
* @return int Exit code |
61
|
|
|
*/ |
62
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
63
|
|
|
{ |
64
|
|
|
$io = new SymfonyStyle($input, $output); |
65
|
|
|
|
66
|
|
|
$config = $this->inputHelper->readConfig($input); |
67
|
|
|
$filters = $this->inputHelper->readFilters($input); |
68
|
|
|
$moduleName = $input->getArgument('module'); |
69
|
|
|
|
70
|
|
|
$modules = $this->modulesHelper->findModules($config); |
71
|
|
|
if (!$this->modulesHelper->validateModules($modules, $io) || !$this->checkModule($modules, $moduleName, $io)) { |
72
|
|
|
return ExitCodes::UNEXPECTED_ERROR; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$dependenciesGraph = $this->modulesReader->generateDependenciesGraph($modules); |
76
|
|
|
$this->displayDependantModules($dependenciesGraph, $io, $filters, $moduleName); |
77
|
|
|
|
78
|
|
|
return ExitCodes::OK; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param DirectedGraph $dependenciesGraph |
83
|
|
|
* @param SymfonyStyle $io |
84
|
|
|
* @param string[] $filters Filtered module names |
85
|
|
|
* @param string $moduleName |
86
|
|
|
*/ |
87
|
|
|
private function displayDependantModules(DirectedGraph $dependenciesGraph, SymfonyStyle $io, array $filters, $moduleName) |
88
|
|
|
{ |
89
|
|
View Code Duplication |
if ($io->isVerbose()) { |
|
|
|
|
90
|
|
|
if (empty($filters)) { |
91
|
|
|
$io->writeln('No filters applied.'); |
92
|
|
|
} else { |
93
|
|
|
$io->writeln('Filters applied: ' . \implode(', ', $filters)); |
94
|
|
|
} |
95
|
|
|
$io->newLine(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$vertices = $this->modulesHelper->filterVerticesByModuleName($dependenciesGraph->getVertices(), $filters); |
99
|
|
|
$vertices = $this->filterVerticesByDependencyName($vertices, $moduleName); |
100
|
|
|
|
101
|
|
|
$io->writeln(\count($vertices) . ' modules depend on "' . $moduleName . '"'); |
102
|
|
|
|
103
|
|
|
$i = 1; |
104
|
|
|
foreach ($vertices as $vertex) { |
105
|
|
|
$this->modulesHelper->renderModuleDependencies($io, $vertex, $i++); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Module[] $modules |
111
|
|
|
* @param string $moduleName |
112
|
|
|
* @param SymfonyStyle $io |
113
|
|
|
* |
114
|
|
|
* @return bool Whether a module with provided name exists in the array |
115
|
|
|
*/ |
116
|
|
|
private function checkModule(array $modules, $moduleName, SymfonyStyle $io) |
117
|
|
|
{ |
118
|
|
|
foreach ($modules as $module) { |
119
|
|
|
if ($module->getName() == $moduleName) { |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$io->error('Module "' . $moduleName . '" is not a properly configured module'); |
125
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param Vertex[] $vertices Module vertices to filter |
131
|
|
|
* @param string $dependencyName Name of required dependency |
132
|
|
|
* |
133
|
|
|
* @return Vertex[] Filtered vertices array |
134
|
|
|
*/ |
135
|
|
|
private function filterVerticesByDependencyName(array $vertices, $dependencyName) |
136
|
|
|
{ |
137
|
|
|
$filterExpression = function (Vertex $v) use ($dependencyName) { |
138
|
|
|
foreach ($v->getNeighbours() as $neighbour) { |
139
|
|
|
if ($neighbour->getValue()->getName() == $dependencyName) { |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return false; |
145
|
|
|
}; |
146
|
|
|
|
147
|
|
|
return \array_filter($vertices, $filterExpression); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.