1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Churn\Commands; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Helper\Table; |
10
|
|
|
use Churn\Results\ResultsGenerator; |
11
|
|
|
use Churn\Managers\FileManager; |
12
|
|
|
use Churn\Results\ResultCollection; |
13
|
|
|
|
14
|
|
|
class ChurnCommand extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Class constructor. |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
parent::__construct(); |
22
|
|
|
$this->resultsGenerator = new ResultsGenerator; |
|
|
|
|
23
|
|
|
$this->fileManager = new FileManager; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Configure the command |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this->setName('run') |
33
|
|
|
->addArgument('path', InputArgument::REQUIRED, 'Path to source to check.') |
34
|
|
|
->setDescription('Check files') |
35
|
|
|
->setHelp('Checks the churn on the provided path argument(s).'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Exectute the command |
40
|
|
|
* @param InputInterface $input Input. |
41
|
|
|
* @param OutputInterface $output Output. |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
$path = $input->getArgument('path'); |
47
|
|
|
$phpFiles = $this->fileManager->getPhpFiles($path); |
48
|
|
|
$results = $this->resultsGenerator->getResults($phpFiles); |
49
|
|
|
$this->displayResults($output, $results); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Displays the results in a table. |
54
|
|
|
* @param OutputInterface $output Output. |
55
|
|
|
* @param Churn\Results\ResultCollection $results Results Collection. |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
protected function displayResults(OutputInterface $output, ResultCollection $results) |
59
|
|
|
{ |
60
|
|
|
$table = new Table($output); |
61
|
|
|
$table->setHeaders(['File', 'Times Changed', 'Complexity', 'Score']); |
62
|
|
|
foreach ($results->orderByScoreDesc() as $result) { |
63
|
|
|
$table->addRow($result->toArray()); |
64
|
|
|
} |
65
|
|
|
$table->render(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: