Completed
Push — master ( 2e21dc...b663e1 )
by Bill
04:37 queued 02:23
created

SyncerCommand::handleArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Bmitch\Envsync;
4
5
use Symfony\Component\Console\Helper\Table;
6
use Symfony\Component\Console\Output\ConsoleOutput;
7
use Bmitch\Envsync\Collectors\FileCollector;
8
use Bmitch\Envsync\Finders\EnvironmentFinder;
9
10
class SyncerCommand
11
{
12
13
    /**
14
     * Creates a new instance of the SyncerCommand.
15
     * @param FileCollector     $fileCollector File Collector.
16
     * @param EnvironmentFinder $envFinder     Environment Variable Filder.
17
     */
18
    public function __construct(FileCollector $fileCollector, EnvironmentFinder $envFinder)
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
    }
23
24
    /**
25
     * Runs the command.
26
     * Supressing this for now.
27
     * @SuppressWarnings(PHPMD.NPathComplexity)
28
     * @param  array $arguments Command line arguments.
29
     * @return void
30
     */
31
    public function handle(array $arguments)
32
    {
33
        $this->handleArguments($arguments);
34
35
        $files = $this->fileCollector
36
                    ->get('.php')
37
                    ->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...
38
39
        $env['source']  = $this->envFinder->getFromFiles($files);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$env was never initialized. Although not strictly required by PHP, it is generally a good practice to add $env = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
40
        $env['example'] = $this->envFinder->getFromFile('.env.example');
41
        $env['env']     = $this->envFinder->getFromFile('.env');
42
        $env['all']     = $this->getAllEnvs($env);
43
44
        $results = [];
45
46
        foreach ($env['all'] as $variable) {
47
            $results[$variable] = [];
48
            $results[$variable]['inSource']     = in_array($variable, $env['source']) ? 'Yes' : 'No';
49
            $results[$variable]['inSource']     = in_array($variable, $env['source']) ? 'Yes' : 'No';
50
            $results[$variable]['inEnvExample'] = in_array($variable, $env['example']) ? 'Yes' : 'No';
51
            $results[$variable]['inEnv']        = in_array($variable, $env['env']) ? 'Yes' : 'No';
52
        }
53
54
        $data = [];
55
56
        foreach ($results as $variable => $result) {
57
            $data[] = [
58
                'variable'     => $variable,
59
                'insource'     => $result['inSource'],
60
                'inenvexample' => $result['inEnvExample'],
61
                'inenv'        => $result['inEnv'],
62
            ];
63
        }
64
65
        $headers = ['Variable', 'In Source', 'In .env.example', 'In .env'];
66
67
        $table = new Table(new ConsoleOutput);
68
69
        $table->setHeaders($headers)->setRows($data)->render();
70
    }
71
72
    /**
73
     * Looks through all the current env variables from all
74
     * sources and makes a master list of all of them.
75
     * @param  array $currentEnvs Current Env Variables.
76
     * @return array
77
     */
78
    protected function getAllEnvs(array $currentEnvs)
79
    {
80
        $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...
81
        $allEnvs     = array_unique(array_merge($currentEnvs['env'], $currentEnvs['example']));
82
        $allEnvs     = array_unique(array_merge($allEnvs, $currentEnvs['source']));
83
84
        return $allEnvs;
85
    }
86
87
    /**
88
     * Collects the command line arguments.
89
     * @param  array $arguments Command line arguments.
90
     * @return void
91
     */
92
    protected function handleArguments(array $arguments)
93
    {
94
        // Default value
95
        $this->folder = '.';
96
97
        if (isset($arguments[1])) {
98
            $this->folder = $arguments[1];
99
        }
100
    }
101
}
102