Import::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
namespace Parking\Console\Command;
3
4
class Import implements Command
5
{
6
7
    public function execute($input, $output)
8
    {
9
        $this->validate($input);
10
        $commands = $this->parse($input);
11
        if (!$commands) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $commands of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
12
            throw new \Exception("Commands not found");
13
        }
14
15
        $path = getcwd() . "/tmp/data";
16
        $repository = new \Parking\Infrastructure\Repository\ParkingRepository($path);
17
        foreach ($commands as $command) {
18
            $argv = $this->generateArgv($command);
19
20
            $handler = new \Parking\Console\Handler($argv);
21
            $handler->addCommand(new \Parking\Console\Command\CreateParkingLot($repository));
22
            $handler->addCommand(new \Parking\Console\Command\Leave($repository));
23
            $handler->addCommand(new \Parking\Console\Command\Park($repository));
24
            $handler->addCommand(new \Parking\Console\Command\RegistrationNumbersForCarsWithColour($repository));
25
            $handler->addCommand(new \Parking\Console\Command\SlotNumbersForCarsWithColour($repository));
26
            $handler->addCommand(new \Parking\Console\Command\SlotNumberForRegistrationNumber($repository));
27
            $handler->addCommand(new \Parking\Console\Command\Status($repository));
28
29
            $handler->dispatch();
30
        }
31
    }
32
33
    public function generateArgv($command)
34
    {
35
        return array_merge(['parking'], explode(' ', $command));
36
    }
37
38
    /**
39
     *
40
     * @param $input
41
     * @return array
42
     */
43
    protected function parse($input)
44
    {
45
        $fp = fopen($input[0], 'r');
46
        while ($command = fgets($fp, 4096)) {
47
            $commands[] = trim($command);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$commands was never initialized. Although not strictly required by PHP, it is generally a good practice to add $commands = 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...
48
        }
49
        fclose($fp);
50
51
        return $commands;
0 ignored issues
show
Bug introduced by
The variable $commands does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
52
    }
53
54
    /**
55
     * @param $input
56
     * @throws \Exception
57
     */
58
    protected function validate($input)
59
    {
60
        if (!isset ($input[0])) {
61
            throw new \Exception('Param required');
62
        }
63
        if (!is_file ($input[0])) {
64
            throw new \Exception('File invalid');
65
        }
66
    }
67
}