Completed
Push — master ( 3718bd...016efe )
by Basenko
03:23
created

PublishTag::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MadWeb\Initializer\Actions;
4
5
use Illuminate\Console\Command;
6
use InvalidArgumentException;
7
8
class PublishTag extends Action
9
{
10
    private const COMMAND = 'vendor:publish';
11
12
    /** @var string|array */
13
    private $tags;
14
15
    /** @var bool */
16
    private $force;
17
18
    /** @var array */
19
    private $arguments = [];
20
21
    private $currentArgument = [];
22
23 24
    public function __construct(Command $artisanCommand, $tags, bool $force = false)
24
    {
25 24
        parent::__construct($artisanCommand);
26
27 24
        $this->tags = $tags;
28 24
        $this->force = $force;
29 24
    }
30
31 24
    public function __invoke(): bool
32
    {
33 24
        if (is_string($this->tags)) {
34 12
            $this->addTag($this->tags);
35 12
        } elseif (is_array($this->tags)) {
36 12
            $this->handleArray();
37
        } else {
38
            throw new InvalidArgumentException('Invalid publishable argument.');
39
        }
40
41 24
        foreach ($this->arguments as $argument) {
42 24
            $this->currentArgument = $argument;
43
44 24
            $errors = [];
45 24
            parent::__invoke();
46
47 24
            if ($this->errorMessage) {
48
                $errors[] = $this->errorMessage;
49
            }
50
        }
51
52 24
        $this->errorMessage = implode(PHP_EOL, $errors);
0 ignored issues
show
Bug introduced by
The variable $errors 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...
53
54 24
        return true;
55
    }
56
57 24
    public function title(): string
58
    {
59 24
        $title = '<comment>Publishing resource:</comment> ';
60
61
        $tagStringCallback = function (string $tag) {
62 24
            return " Tag [$tag]";
63 24
        };
64
65 24
        if (isset($this->currentArgument['--tag'])) {
66 24
            if (is_string($this->currentArgument['--tag'])) {
67 24
                $title .= $tagStringCallback($this->currentArgument['--tag']);
68
            } else {
69
                foreach ($this->currentArgument['--tag'] as $tag) {
70
                    $title .= $tagStringCallback($tag);
71
                }
72
            }
73
        }
74
75 24
        return trim($title);
76
    }
77
78 24
    public function run(): bool
79
    {
80 24
        $action = new Artisan($this->getArtisanCommnad(), self::COMMAND, $this->currentArgument);
81
82 24
        return $action->run();
83
    }
84
85 24
    private function addTag(string $tag)
86
    {
87 24
        $arguments['--tag'] = $tag;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$arguments was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arguments = 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...
88
89 24
        if ($this->force) {
90 12
            $arguments['--force'] = true;
91
        }
92
93 24
        $this->arguments[] = $arguments;
94 24
    }
95
96 12
    private function handleArray(): void
97
    {
98 12
        foreach ($this->tags as $tag) {
0 ignored issues
show
Bug introduced by
The expression $this->tags of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
99 12
            $this->addTag($tag);
100
        }
101 12
    }
102
}
103