1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Churn\Commands; |
4
|
|
|
|
5
|
|
|
use Churn\Collections\FileCollection; |
6
|
|
|
use Churn\Factories\ResultsRendererFactory; |
7
|
|
|
use Churn\Logic\ResultsLogic; |
8
|
|
|
use Churn\Managers\ProcessManager; |
9
|
|
|
use Churn\Factories\ProcessFactory; |
10
|
|
|
use Churn\Managers\FileManager; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Churn\Configuration\Config; |
17
|
|
|
use Symfony\Component\Yaml\Yaml; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
22
|
|
|
*/ |
23
|
|
|
class ChurnCommand extends Command |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The results logic. |
27
|
|
|
* @var ResultsLogic |
28
|
|
|
*/ |
29
|
|
|
private $resultsLogic; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The process manager. |
33
|
|
|
* @var ProcessManager |
34
|
|
|
*/ |
35
|
|
|
private $processManager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The renderer factory. |
39
|
|
|
* @var ResultsRendererFactory |
40
|
|
|
*/ |
41
|
|
|
private $renderFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* ChurnCommand constructor. |
45
|
|
|
* @param ResultsLogic $resultsLogic The results logic. |
46
|
|
|
* @param ProcessManager $processManager The process manager. |
47
|
|
|
* @param ResultsRendererFactory $renderFactory The Results Renderer Factory. |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
ResultsLogic $resultsLogic, |
51
|
|
|
ProcessManager $processManager, |
52
|
|
|
ResultsRendererFactory $renderFactory |
53
|
|
|
) { |
54
|
|
|
parent::__construct(); |
55
|
|
|
$this->resultsLogic = $resultsLogic; |
56
|
|
|
$this->processManager = $processManager; |
57
|
|
|
$this->renderFactory = $renderFactory; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Configure the command |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
protected function configure(): void |
65
|
|
|
{ |
66
|
|
|
$this->setName('run') |
67
|
|
|
->addArgument('paths', InputArgument::IS_ARRAY, 'Path to source to check.') |
68
|
|
|
->addOption('configuration', 'c', InputOption::VALUE_OPTIONAL, 'Path to the configuration file', 'churn.yml') // @codingStandardsIgnoreLine |
69
|
|
|
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format to use', 'text') |
70
|
|
|
->setDescription('Check files') |
71
|
|
|
->setHelp('Checks the churn on the provided path argument(s).'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Execute the command |
76
|
|
|
* @param InputInterface $input Input. |
77
|
|
|
* @param OutputInterface $output Output. |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
81
|
|
|
{ |
82
|
|
|
$content = (string) @file_get_contents($input->getOption('configuration')); |
83
|
|
|
$config = Config::create(Yaml::parse($content) ?? []); |
84
|
|
|
|
85
|
|
|
$paths = $this->getDirectoriesToScan($input, $config->getDirectoriesToScan()); |
86
|
|
|
|
87
|
|
|
$completedProcesses = $this->processManager->process( |
88
|
|
|
$this->createFileCollection($config, $paths), |
89
|
|
|
new ProcessFactory($config->getCommitsSince()), |
90
|
|
|
$config->getParallelJobs() |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$resultCollection = $this->resultsLogic->process( |
94
|
|
|
$completedProcesses, |
95
|
|
|
$config->getMinScoreToShow(), |
96
|
|
|
$config->getFilesToShow() |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$renderer = $this->renderFactory->getRenderer($input->getOption('format')); |
100
|
|
|
$renderer->render($output, $resultCollection); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the directories to scan. |
105
|
|
|
* @param InputInterface $input Input Interface. |
106
|
|
|
* @param array $dirsConfigured The directories configured to scan. |
107
|
|
|
* @throws InvalidArgumentException If paths argument invalid. |
108
|
|
|
* @return array When no directories to scan found. |
109
|
|
|
*/ |
110
|
|
|
private function getDirectoriesToScan(InputInterface $input, array $dirsConfigured): array |
111
|
|
|
{ |
112
|
|
|
$dirsProvidedAsArgs = $input->getArgument('paths'); |
113
|
|
|
if (count($dirsProvidedAsArgs) > 0) { |
114
|
|
|
return $dirsProvidedAsArgs; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (count($dirsConfigured) > 0) { |
118
|
|
|
return $dirsConfigured; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
throw new InvalidArgumentException( |
122
|
|
|
'Provide the directories you want to scan as arguments, ' . |
123
|
|
|
'or configure them under "directoriesToScan" in your churn.yml file.' |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Creates a file collection. |
129
|
|
|
* @param Config $config The configuration. |
130
|
|
|
* @param string[] $paths An array of paths. |
131
|
|
|
* @return FileCollection |
132
|
|
|
*/ |
133
|
|
|
private function createFileCollection(Config $config, array $paths): FileCollection |
134
|
|
|
{ |
135
|
|
|
$fileManager = new FileManager( |
136
|
|
|
$config->getFileExtensions(), |
137
|
|
|
$config->getFilesToIgnore() |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
return $fileManager->getPhpFiles($paths); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|