ReportCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 23.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 20
loc 85
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 20 33 5
A getArguments() 0 4 1
A getOptions() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Reallyli\AB\Commands;
4
5
use Reallyli\AB\Models\Goal;
6
use Illuminate\Console\Command;
7
use Reallyli\AB\Models\Experiment;
8
use Symfony\Component\Console\Helper\Table;
9
10
class ReportCommand extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'ab:report';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Print the A/B testing report.';
25
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        $experiments = Experiment::active()->get();
0 ignored issues
show
Bug introduced by
The method active() does not exist on Reallyli\AB\Models\Experiment. Did you maybe mean scopeActive()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
44
        $goals = array_unique(Goal::active()->orderBy('name')->pluck('name')->toArray());
0 ignored issues
show
Bug introduced by
The method active() does not exist on Reallyli\AB\Models\Goal. Did you maybe mean scopeActive()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
45
46
        $columns = array_merge(['Experiment', 'Visitors', 'Engagement'], array_map('ucfirst', $goals));
47
48
        $table = new Table($this->output);
49
        $table->setHeaders($columns);
50
51 View Code Duplication
        foreach ($experiments as $experiment) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
            $engagement = $experiment->visitors ? ($experiment->engagement / $experiment->visitors * 100) : 0;
53
54
            $row = [
55
                $experiment->name,
56
                $experiment->visitors,
57
                number_format($engagement, 2).' % ('.$experiment->engagement.')',
58
            ];
59
60
            $results = $experiment->goals()->pluck('count', 'name');
61
62
            foreach ($goals as $column) {
63
                $count = array_get($results, $column, 0);
64
                $percentage = $experiment->visitors ? ($count / $experiment->visitors * 100) : 0;
65
66
                $row[] = number_format($percentage, 2)." % ($count)";
67
            }
68
69
            $table->addRow($row);
70
        }
71
72
        $table->render();
73
    }
74
75
    /**
76
     * Get the console command arguments.
77
     *
78
     * @return array
79
     */
80
    protected function getArguments()
81
    {
82
        return [];
83
    }
84
85
    /**
86
     * Get the console command options.
87
     *
88
     * @return array
89
     */
90
    protected function getOptions()
91
    {
92
        return [];
93
    }
94
}
95