ExportCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 21.05 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 20
loc 95
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handle() 20 41 6
A getArguments() 0 6 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 League\Csv\Writer;
6
use SplTempFileObject;
7
use Reallyli\AB\Models\Goal;
8
use Illuminate\Console\Command;
9
use Reallyli\AB\Models\Experiment;
10
use Illuminate\Support\Facades\File;
11
use Symfony\Component\Console\Input\InputArgument;
12
13
class ExportCommand extends Command
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'ab:export';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Export the A/B testing repor to a CSV file.';
28
29
    /**
30
     * Create a new command instance.
31
     *
32
     * @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...
33
     */
34
    public function __construct()
35
    {
36
        parent::__construct();
37
    }
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return mixed
43
     */
44
    public function handle()
45
    {
46
        $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...
47
        $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...
48
49
        $columns = array_merge(['Experiment', 'Visitors', 'Engagement'], array_map('ucfirst', $goals));
50
51
        $writer = new Writer(new SplTempFileObject);
52
        $writer->insertOne($columns);
53
54 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...
55
            $engagement = $experiment->visitors ? ($experiment->engagement / $experiment->visitors * 100) : 0;
56
57
            $row = [
58
                $experiment->name,
59
                $experiment->visitors,
60
                number_format($engagement, 2).' % ('.$experiment->engagement.')',
61
            ];
62
63
            $results = $experiment->goals()->pluck('count', 'name');
64
65
            foreach ($goals as $column) {
66
                $count = array_get($results, $column, 0);
67
                $percentage = $experiment->visitors ? ($count / $experiment->visitors * 100) : 0;
68
69
                $row[] = number_format($percentage, 2)." % ($count)";
70
            }
71
72
            $writer->insertOne($row);
73
        }
74
75
        $output = (string) $writer;
76
77
        if ($file = $this->argument('file')) {
78
            $this->info("Creating $file");
79
80
            File::put($file, $output);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $this->argument('file') on line 77 can also be of type array; however, Illuminate\Support\Facades\File::put() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
81
        } else {
82
            $this->line($output);
83
        }
84
    }
85
86
    /**
87
     * Get the console command arguments.
88
     *
89
     * @return array
90
     */
91
    protected function getArguments()
92
    {
93
        return [
94
            ['file', InputArgument::OPTIONAL, 'The target CSV file to write the output to.'],
95
        ];
96
    }
97
98
    /**
99
     * Get the console command options.
100
     *
101
     * @return array
102
     */
103
    protected function getOptions()
104
    {
105
        return [];
106
    }
107
}
108