Completed
Push — master ( a439c5...b826d9 )
by Bill
05:53 queued 03:49
created

ChurnCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 7
dl 0
loc 81
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 7 1
A execute() 0 16 3
A getResults() 0 12 1
A displayTable() 0 9 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 RecursiveDirectoryIterator;
10
use RecursiveIteratorIterator;
11
use Churn\Assessors\GitCommitCount\GitCommitCountAssessor;
12
use Churn\Assessors\CyclomaticComplexity\CyclomaticComplexityAssessor;
13
use Churn\Services\CommandService;
14
use Symfony\Component\Console\Helper\Table;
15
use Illuminate\Support\Collection;
16
use SplFileInfo;
17
18
class ChurnCommand extends Command
19
{
20
    /**
21
     * Class constructor.
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
        $this->commitCountAssessor = new GitCommitCountAssessor(new CommandService);
0 ignored issues
show
Bug introduced by
The property commitCountAssessor 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...
27
        $this->complexityAssessor = new CyclomaticComplexityAssessor();
0 ignored issues
show
Bug introduced by
The property complexityAssessor 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...
28
    }
29
30
    /**
31
     * Configure the command
32
     * @return void
33
     */
34
    protected function configure()
35
    {
36
        $this->setName('run')
37
            ->addArgument('path', InputArgument::REQUIRED, 'Path to source to check.')
38
            ->setDescription('Check files')
39
            ->setHelp('Checks the churn on the provided path argument(s).');
40
    }
41
42
    /**
43
     * Exectute the command
44
     * @param  InputInterface  $input  Input.
45
     * @param  OutputInterface $output Output.
46
     * @return void
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $path = $input->getArgument('path');
51
52
        $directoryIterator = new RecursiveDirectoryIterator($path);
53
54
        $results = Collection::make();
55
        foreach (new RecursiveIteratorIterator($directoryIterator) as $file) {
56
            if ($file->getExtension() !== 'php') {
57
                continue;
58
            }
59
            $results->push($this->getResults($file));
60
        }
61
        $results = $results->sortByDesc('score');
62
        $this->displayTable($output, $results);
63
    }
64
65
    /**
66
     * Calculate the results for a file.
67
     * @param  \SplFileInfo $file File.
68
     * @return array
69
     */
70
    protected function getResults(SplFileInfo $file): array
71
    {
72
        $commits    = $this->commitCountAssessor->assess($file->getRealPath());
73
        $complexity = $this->complexityAssessor->assess($file->getRealPath());
74
75
        return [
76
            'file'       => $file->getRealPath(),
77
            'commits'    => $commits,
78
            'complexity' => $complexity,
79
            'score'      => ($commits/ 10) * ($complexity * 8.75),
80
        ];
81
    }
82
83
    /**
84
     * Displays the results in a table.
85
     * @param  OutputInterface                $output  Output.
86
     * @param  \Illuminate\Support\Collection $results Results.
87
     * @return void
88
     */
89
    protected function displayTable(OutputInterface $output, Collection $results)
90
    {
91
        $table = new Table($output);
92
        $table->setHeaders(['File', 'Times Changed', 'Complexity', 'Score']);
93
        foreach ($results as $resultsRow) {
94
            $table->addRow($resultsRow);
95
        }
96
        $table->render();
97
    }
98
}
99