1 | <?php declare(strict_types = 1); |
||
21 | class ChurnCommand extends Command |
||
22 | { |
||
23 | const FORMAT_JSON = 'json'; |
||
24 | const FORMAT_TEXT = 'text'; |
||
25 | |||
26 | /** |
||
27 | * The config values. |
||
28 | * @var Config |
||
29 | */ |
||
30 | private $config; |
||
31 | |||
32 | /** |
||
33 | * The process factory. |
||
34 | * @var ProcessFactory |
||
35 | */ |
||
36 | private $processFactory; |
||
37 | |||
38 | /** |
||
39 | * Th results parser. |
||
40 | * @var ResultsParser |
||
41 | */ |
||
42 | private $resultsParser; |
||
43 | |||
44 | /** |
||
45 | * Collection of files to run the processes on. |
||
46 | * @var FileCollection |
||
47 | */ |
||
48 | private $filesCollection; |
||
49 | |||
50 | /** |
||
51 | * Collection of processes currently running. |
||
52 | * @var Collection |
||
53 | */ |
||
54 | private $runningProcesses; |
||
55 | |||
56 | /** |
||
57 | * Array of completed processes. |
||
58 | * @var array |
||
59 | */ |
||
60 | private $completedProcessesArray; |
||
61 | |||
62 | /** |
||
63 | * The start time. |
||
64 | * @var float |
||
65 | */ |
||
66 | private $startTime; |
||
67 | |||
68 | /** |
||
69 | * Keeps track of how many files were processed. |
||
70 | * @var integer |
||
71 | */ |
||
72 | private $filesCount; |
||
73 | |||
74 | /** |
||
75 | * ChurnCommand constructor. |
||
76 | */ |
||
77 | public function __construct() |
||
78 | { |
||
79 | parent::__construct(); |
||
80 | |||
81 | $this->resultsParser = new ResultsParser(); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Configure the command |
||
86 | * @return void |
||
87 | */ |
||
88 | protected function configure() |
||
89 | { |
||
90 | $this->setName('run') |
||
91 | ->addArgument('paths', InputArgument::IS_ARRAY, 'Path to source to check.') |
||
92 | ->addOption('configuration', 'c', InputOption::VALUE_OPTIONAL, 'Path to the configuration file', 'churn.yml') // @codingStandardsIgnoreLine |
||
93 | ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format to use', 'text') |
||
94 | ->setDescription('Check files') |
||
95 | ->setHelp('Checks the churn on the provided path argument(s).'); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Execute the command |
||
100 | * @param InputInterface $input Input. |
||
101 | * @param OutputInterface $output Output. |
||
102 | * @return void |
||
103 | */ |
||
104 | protected function execute(InputInterface $input, OutputInterface $output) |
||
105 | { |
||
106 | $this->startTime = microtime(true); |
||
107 | $this->setupProcessor($input->getOption('configuration')); |
||
108 | |||
109 | $this->filesCollection = $this->getPhpFiles($input->getArgument('paths')); |
||
110 | $this->filesCount = $this->filesCollection->count(); |
||
111 | $this->runningProcesses = new Collection; |
||
112 | $this->completedProcessesArray = []; |
||
113 | while ($this->filesCollection->hasFiles() || $this->runningProcesses->count()) { |
||
114 | $this->getProcessResults(); |
||
115 | } |
||
116 | $completedProcesses = new Collection($this->completedProcessesArray); |
||
117 | |||
118 | $results = $this->resultsParser |
||
119 | ->parse($completedProcesses) |
||
120 | ->normalizeAgainst($this->config); |
||
121 | $this->displayResults($input->getOption('format'), $output, $results); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Gets the output from processes and stores them in the completedProcessArray member. |
||
126 | * @return void |
||
127 | */ |
||
128 | private function getProcessResults() |
||
149 | |||
150 | /** |
||
151 | * Displays the results in a table. |
||
152 | * @param OutputInterface $output Output. |
||
153 | * @param ResultCollection $results Results Collection. |
||
154 | * @return void |
||
155 | */ |
||
156 | protected function displayResults(string $format, OutputInterface $output, ResultCollection $results) |
||
166 | |||
167 | /** |
||
168 | * @param string $configFile Relative path churn.yml configuration file. |
||
169 | * @return void |
||
170 | */ |
||
171 | private function setupProcessor(string $configFile) |
||
176 | |||
177 | /** |
||
178 | * @param array $directory Directories. |
||
179 | * @return FileCollection |
||
180 | */ |
||
181 | private function getPhpFiles(array $directory): FileCollection |
||
187 | |||
188 | /** |
||
189 | * @param OutputInterface $output |
||
190 | * @param ResultCollection $results |
||
191 | */ |
||
192 | private function displayResultsText(OutputInterface $output, ResultCollection $results) |
||
213 | |||
214 | private function displayResultsJson(OutputInterface $output, ResultCollection $results) |
||
227 | } |
||
228 |