SyncerCommand   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 212
Duplicated Lines 7.55 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 25
c 2
b 0
f 0
lcom 1
cbo 3
dl 16
loc 212
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B handle() 0 26 4
A getEnvironmentVariables() 0 14 1
B getResults() 0 15 5
A mergeEnvs() 0 8 1
A checkForCiBuild() 8 8 4
A checkForDeploy() 8 8 4
B argumentsValid() 0 26 5

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 Bmitch\Envsync;
4
5
use Bmitch\Envsync\Collectors\FileCollector;
6
use Bmitch\Envsync\Finders\EnvironmentFinder;
7
use Bmitch\Envsync\Builders\TableBuilder;
8
9
class SyncerCommand
10
{
11
12
    /**
13
     * Will be returned when program exits.
14
     * @var integer
15
     */
16
    protected $exitCode = 0;
17
18
    /**
19
     * The mode the program is running in.
20
     * @var string
21
     */
22
    protected $mode;
23
24
    /**
25
     * Instance of the FileCollector class.
26
     * @var FileCollector
27
     */
28
    protected $fileCollector;
29
30
    /**
31
     * Instance of the EnvironmentFilder class.
32
     * @var EnvironmentFinder
33
     */
34
    protected $envFinder;
35
36
    /**
37
     * Instance of the TableBuilder class.
38
     * @var TableBuilder
39
     */
40
    protected $tableBuilder;
41
42
    /**
43
     * The source colde folder that will be inspected.
44
     * @var string
45
     */
46
    protected $folder;
47
48
    /**
49
     * Holds an error message if command runs into an error.
50
     * @var string
51
     */
52
    protected $error;
53
54
    /**
55
     * Creates a new instance of the SyncerCommand.
56
     * @param FileCollector     $fileCollector File Collector.
57
     * @param EnvironmentFinder $envFinder     Environment Variable Filder.
58
     * @param TableBuilder      $tableBuilder  Table Builder.
59
     */
60
    public function __construct(FileCollector $fileCollector, EnvironmentFinder $envFinder, TableBuilder $tableBuilder)
61
    {
62
        $this->fileCollector = $fileCollector;
63
        $this->envFinder = $envFinder;
64
        $this->tableBuilder = $tableBuilder;
65
    }
66
67
    /**
68
     * Runs the command.
69
     * @param  array $arguments Command line arguments.
70
     * @return void
71
     */
72
    public function handle(array $arguments)
73
    {
74
        if (! $this->argumentsValid($arguments)) {
75
            echo $this->error;
76
            return;
77
        }
78
79
        print "\n\nEnvSyncer Report - https://github.com/bmitch/envsync\n";
80
81
        $envData = $this->getEnvironmentVariables();
82
83
        $results = $this->getResults($envData);
84
85
        if ($this->mode == 'ci') {
86
            $this->checkForCiBuild($results);
87
        }
88
89
        if ($this->mode == 'deploy') {
90
            $this->checkForDeploy($results);
91
        }
92
93
        $this->tableBuilder->outputTable($results, $this->mode);
94
95
96
        exit($this->exitCode);
97
    }
98
99
    /**
100
     * Gets a list of the environment variables defined in the
101
     * source code, .env and .env.example file.
102
     * @return array
103
     */
104
    protected function getEnvironmentVariables()
105
    {
106
        $files = $this->fileCollector
107
                    ->get('.php')
108
                    ->from($this->folder);
109
110
        $env            = [];
111
        $env['source']  = $this->envFinder->getFromFiles($files);
112
        $env['example'] = $this->envFinder->getFromFile('.env.example');
113
        $env['env']     = $this->envFinder->getFromFile('.env');
114
        $env['all']     = $this->mergeEnvs($env);
115
116
        return $env;
117
    }
118
119
    /**
120
     * Takes the list of environment variables defined and creates
121
     * a results array showing each variable and where it is or is not
122
     * defined.
123
     * @param  array $envData Environment Variable Data.
124
     * @return array
125
     */
126
    protected function getResults(array $envData)
127
    {
128
        $results = [];
129
130
        foreach ($envData['all'] as $variable) {
131
            $results[] = [
132
                'variable'     => $variable,
133
                'insource'     => in_array($variable, $envData['source']) ? 'Yes' : 'No',
134
                'inenvexample' => in_array($variable, $envData['example']) ? 'Yes' : 'No',
135
                'inenv'        => in_array($variable, $envData['env']) ? 'Yes' : 'No',
136
            ];
137
        }
138
139
        return $results;
140
    }
141
142
    /**
143
     * Looks through all the current env variables from all
144
     * sources and makes a master list of all of them.
145
     * @param  array $currentEnvs Current Env Variables.
146
     * @return array
147
     */
148
    protected function mergeEnvs(array $currentEnvs)
149
    {
150
        $allEnvs = [];
0 ignored issues
show
Unused Code introduced by
$allEnvs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
151
        $allEnvs     = array_unique(array_merge($currentEnvs['env'], $currentEnvs['example']));
152
        $allEnvs     = array_unique(array_merge($allEnvs, $currentEnvs['source']));
153
154
        return $allEnvs;
155
    }
156
157
    /**
158
     * If a variable is defined in the source code but
159
     * not defined in the .env.example file we write
160
     * to the message and set response to 1
161
     * @param  array $results Environment Variable Results.
162
     * @return void
163
     */
164 View Code Duplication
    protected function checkForCiBuild(array $results)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
165
    {
166
        foreach ($results as $row) {
167
            if ($row['insource'] == 'Yes' && $row['inenvexample'] == 'No') {
168
                $this->exitCode = 1;
169
            }
170
        }
171
    }
172
173
    /**
174
     * If a variable is defined in the source code but
175
     * not defined in the .env file we write
176
     * to the message and set response to 1
177
     * @param  array $results Environment Variable Results.
178
     * @return void
179
     */
180 View Code Duplication
    protected function checkForDeploy(array $results)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
181
    {
182
        foreach ($results as $row) {
183
            if ($row['insource'] == 'Yes' && $row['inenv'] == 'No') {
184
                $this->exitCode = 1;
185
            }
186
        }
187
    }
188
189
    /**
190
     * Collects the command line arguments.
191
     * @param  array $arguments Command line arguments.
192
     * @return boolean
193
     */
194
    protected function argumentsValid(array $arguments)
195
    {
196
        // Default value
197
        $this->folder = '.';
198
199
        if (isset($arguments[1])) {
200
            $this->folder = $arguments[1];
201
            if (! file_exists($this->folder)) {
202
                $this->error = "Error: Folder {$this->folder} does not exist.";
203
                return false;
204
            }
205
        }
206
207
        if (isset($arguments[2])) {
208
            $this->mode = strtolower($arguments[2]);
209
            if (!in_array($this->mode, ['ci', 'deploy', 'default'])) {
210
                $this->error = "Error: Invalid argument {$this->mode}";
211
                return false;
212
            }
213
            return true;
214
        }
215
216
        $this->mode = 'default';
217
218
        return true;
219
    }
220
}
221