Completed
Push — master ( 9e1ab8...e0f7b9 )
by Bill
04:14 queued 02:03
created

ChurnCommand::displayResults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
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;
0 ignored issues
show
Bug introduced by
The property resultsGenerator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
        $this->fileManager = new FileManager;
0 ignored issues
show
Bug introduced by
The property fileManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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