Completed
Push — master ( a795aa...2533ab )
by Bill
16s
created

ChurnCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Commands;
4
5
use Churn\Processes\CyclomaticComplexityProcess;
6
use Churn\Processes\GitCommitCountProcess;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Helper\Table;
12
use Churn\Managers\FileManager;
13
use Churn\Results\ResultCollection;
14
use Illuminate\Support\Collection;
15
use Churn\Results\ResultsParser;
16
17
class ChurnCommand extends Command
18
{
19
    /**
20
     * The file manager.
21
     * @var FileManager
22
     */
23
    private $fileManager;
24
25
    /**
26
     * Th results parser.
27
     * @var ResultsParser
28
     */
29
    private $resultsParser;
30
31
    /**
32
     * Collection of files to run the processes on.
33
     * @var Collection
34
     */
35
    private $files;
36
37
    /**
38
     * Collection of processes currently running.
39
     * @var Collection
40
     */
41
    private $runningProcesses;
42
43
    /**
44
     * Array of completed processes.
45
     * @var array
46
     */
47
    private $completedProcessesArray;
48
49
    /**
50
     * Class constructor.
51
     */
52
    public function __construct()
53
    {
54
        parent::__construct();
55
        $this->fileManager = new FileManager;
56
        $this->resultsParser = new ResultsParser;
57
    }
58
59
    /**
60
     * Configure the command
61
     * @return void
62
     */
63
    protected function configure()
64
    {
65
        $this->setName('run')
66
            ->addArgument('path', InputArgument::REQUIRED, 'Path to source to check.')
67
            ->setDescription('Check files')
68
            ->setHelp('Checks the churn on the provided path argument(s).');
69
    }
70
71
    /**
72
     * Exectute the command
73
     * @param  InputInterface  $input  Input.
74
     * @param  OutputInterface $output Output.
75
     * @return void
76
     */
77
    protected function execute(InputInterface $input, OutputInterface $output)
78
    {
79
        $path = $input->getArgument('path');
80
        $this->files = $this->fileManager->getPhpFiles($path);
81
82
        $this->runningProcesses = new Collection;
83
        $this->completedProcessesArray = [];
84
        while ($this->files->count() || $this->runningProcesses->count()) {
85
            $this->getProcessResults();
86
        }
87
        $completedProcesses = new Collection($this->completedProcessesArray);
88
89
        $results = $this->resultsParser->parse($completedProcesses);
90
        $this->displayResults($output, $results);
91
    }
92
93
    /**
94
     * Gets the output from processes and stores them in the completedProcessArray member.
95
     * @return void
96
     */
97
    private function getProcessResults()
98
    {
99
        for ($index = $this->runningProcesses->count(); $this->files->count() > 0 && $index < 10; $index++) {
100
            $file = $this->files->shift();
101
102
            $process = new GitCommitCountProcess($file);
103
            $process->start();
104
            $this->runningProcesses->put($process->getKey(), $process);
105
            $process = new CyclomaticComplexityProcess($file);
106
            $process->start();
107
            $this->runningProcesses->put($process->getKey(), $process);
108
        }
109
110
        foreach ($this->runningProcesses as $file => $process) {
111
            if ($process->isSuccessful()) {
112
                $this->runningProcesses->forget($process->getKey());
113
                $this->completedProcessesArray[$process->getFileName()][$process->getType()] = $process;
114
            }
115
        }
116
    }
117
118
    /**
119
     * Displays the results in a table.
120
     * @param  OutputInterface                $output  Output.
121
     * @param  Churn\Results\ResultCollection $results Results Collection.
122
     * @return void
123
     */
124
    protected function displayResults(OutputInterface $output, ResultCollection $results)
125
    {
126
        echo "\n
127
  ___  _   _  __  __  ____  _  _     ____  _   _  ____ 
128
 / __)( )_( )(  )(  )(  _ \( \( )___(  _ \( )_( )(  _ \
129
( (__  ) _ (  )(__)(  )   / )  ((___))___/ ) _ (  )___/
130
 \___)(_) (_)(______)(_)\_)(_)\_)   (__)  (_) (_)(__)      https://github.com/bmitch/churn-php
131
 
132
";
133
        $table = new Table($output);
134
        $table->setHeaders(['File', 'Times Changed', 'Complexity', 'Score']);
135
136
        foreach ($results->orderByScoreDesc()->take(10) as $result) {
137
            $table->addRow($result->toArray());
138
        }
139
        $table->render();
140
    }
141
}
142