Completed
Push — master ( 6b5da6...9a1463 )
by Basenko
04:09
created

Publish::addProvider()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
1
<?php
2
3
namespace MadWeb\Initializer\ExecutorActions;
4
5
use InvalidArgumentException;
6
use Illuminate\Console\Command;
7
8
class Publish
9
{
10
    private const COMMAND = 'vendor:publish';
11
12
    private $artisanCommand;
13
14
    /** @var string|array */
15
    private $providers;
16
17
    /** @var bool */
18
    private $force;
19
20
    /** @var array */
21
    private $arguments = [];
22
23 66
    public function __construct(Command $artisanCommand, $providers, bool $force = false)
24
    {
25 66
        $this->artisanCommand = $artisanCommand;
26 66
        $this->providers = $providers;
27 66
        $this->force = $force;
28 66
    }
29
30 66
    public function __invoke()
31
    {
32 66
        if (is_string($this->providers)) {
33 12
            $this->addProvider($this->providers);
34 54
        } elseif (is_array($this->providers)) {
35 48
            $this->handleArray();
36
        } else {
37 6
            throw new InvalidArgumentException('Invalid publishable argument.');
38
        }
39
40 60
        foreach ($this->arguments as $argument) {
41 60
            new Artisan($this->artisanCommand, self::COMMAND, $argument);
42 60
            value(new Artisan($this->artisanCommand, self::COMMAND, $argument))();
43
        }
44 60
    }
45
46 60
    private function addProvider(string $provider, $tag = null)
47
    {
48 60
        $arguments['--provider'] = $provider;
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...
49
50 60
        if ($tag !== null) {
51 30
            $arguments['--tag'] = $tag;
52
        }
53
54 60
        if ($this->force) {
55 12
            $arguments['--force'] = true;
56
        }
57
58 60
        $this->arguments[] = $arguments;
59 60
    }
60
61 48
    private function handleArray(): void
62
    {
63 48
        foreach ($this->providers as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $this->providers 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...
64 48
            if (is_numeric($key)) {
65 18
                $this->addProvider($value);
66
            } else {
67 30
                $this->addProvider($key, $value);
68
            }
69
        }
70 48
    }
71
}
72