Completed
Push — master ( 148650...0fb3ed )
by Bill
10s
created

SyncerCommand::getResults()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 9
nop 1
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
     * Creates a new instance of the SyncerCommand.
14
     * @param FileCollector     $fileCollector File Collector.
15
     * @param EnvironmentFinder $envFinder     Environment Variable Filder.
16
     * @param TableBuilder      $tableBuilder  Table Builder.
17
     */
18
    public function __construct(FileCollector $fileCollector, EnvironmentFinder $envFinder, TableBuilder $tableBuilder)
19
    {
20
        $this->fileCollector = $fileCollector;
0 ignored issues
show
Bug introduced by
The property fileCollector 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...
21
        $this->envFinder = $envFinder;
0 ignored issues
show
Bug introduced by
The property envFinder 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...
22
        $this->tableBuilder = $tableBuilder;
0 ignored issues
show
Bug introduced by
The property tableBuilder 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
    }
24
25
    /**
26
     * Runs the command.
27
     * Supressing this for now.
28
     * @SuppressWarnings(PHPMD.NPathComplexity)
29
     * @param  array $arguments Command line arguments.
30
     * @return void
31
     */
32
    public function handle(array $arguments)
33
    {
34
        $this->handleArguments($arguments);
35
36
        $envData = $this->getEnvironmentVariables();
37
38
        $results = $this->getResults($envData);
39
40
        $this->tableBuilder->outputTable($results);
41
    }
42
43
    /**
44
     * Gets a list of the environment variables defined in the
45
     * source code, .env and .env.example file.
46
     * @return array
47
     */
48
    protected function getEnvironmentVariables()
49
    {
50
        $files = $this->fileCollector
51
                    ->get('.php')
52
                    ->from($this->folder);
0 ignored issues
show
Bug introduced by
The property folder 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...
53
54
        $env            = [];
55
        $env['source']  = $this->envFinder->getFromFiles($files);
56
        $env['example'] = $this->envFinder->getFromFile('.env.example');
57
        $env['env']     = $this->envFinder->getFromFile('.env');
58
        $env['all']     = $this->mergeEnvs($env);
59
60
        return $env;
61
    }
62
63
    /**
64
     * Takes the list of environment variables defined and creates
65
     * a results array showing each variable and where it is or is not
66
     * defined.
67
     * @param  array $envData Environment Variable Data.
68
     * @return array
69
     */
70
    protected function getResults(array $envData)
71
    {
72
        $results = [];
73
74
        foreach ($envData['all'] as $variable) {
75
            $results[] = [
76
                'variable'     => $variable,
77
                'insource'     => in_array($variable, $envData['source']) ? 'Yes' : 'No',
78
                'inenvexample' => in_array($variable, $envData['example']) ? 'Yes' : 'No',
79
                'inenv'        => in_array($variable, $envData['env']) ? 'Yes' : 'No',
80
            ];
81
        }
82
83
        return $results;
84
    }
85
86
    /**
87
     * Looks through all the current env variables from all
88
     * sources and makes a master list of all of them.
89
     * @param  array $currentEnvs Current Env Variables.
90
     * @return array
91
     */
92
    protected function mergeEnvs(array $currentEnvs)
93
    {
94
        $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...
95
        $allEnvs     = array_unique(array_merge($currentEnvs['env'], $currentEnvs['example']));
96
        $allEnvs     = array_unique(array_merge($allEnvs, $currentEnvs['source']));
97
98
        return $allEnvs;
99
    }
100
101
    /**
102
     * Collects the command line arguments.
103
     * @param  array $arguments Command line arguments.
104
     * @return void
105
     */
106
    protected function handleArguments(array $arguments)
107
    {
108
        // Default value
109
        $this->folder = '.';
110
111
        if (isset($arguments[1])) {
112
            $this->folder = $arguments[1];
113
        }
114
    }
115
}
116