Completed
Push — master ( 05a33e...68150d )
by Basenko
12:53
created

Run::dispatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace MadWeb\Initializer;
4
5
use MadWeb\Initializer\Contracts\Runner;
6
7
class Run implements Runner
8
{
9
    protected $commands = [];
10
11
    public function artisan(string $command, array $arguments = []): Runner
12
    {
13
        $this->pushCommand(__FUNCTION__, $command, $arguments);
14
15
        return $this;
16
    }
17
18
    public function external(string $command, ...$arguments): Runner
19
    {
20
        $this->pushCommand(__FUNCTION__, $command, $arguments);
21
22
        return $this;
23
    }
24
25
    public function callable(callable $function, ...$arguments): Runner
26
    {
27
        $this->pushCommand(__FUNCTION__, $function, $arguments);
28
29
        return $this;
30
    }
31
32
    public function dispatch($job): Runner
33
    {
34
        $this->pushCommand(__FUNCTION__, $job);
35
36
        return $this;
37
    }
38
39
    public function dispatchNow($job): Runner
40
    {
41
        $this->pushCommand(__FUNCTION__, $job);
42
43
        return $this;
44
    }
45
46
    public function publish(array $providers): Runner
47
    {
48
        foreach ($providers as $provider => $tag) {
49
            $arguments['--provider'] = is_numeric($provider) ? $tag : $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...
50
51
            if (! is_numeric($provider) and is_string($tag)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
52
                $arguments['--tag'] = $tag;
53
            }
54
55
            $this->artisan('vendor:publish', $arguments);
56
        }
57
58
        return $this;
59
    }
60
61
    protected function pushCommand(string $type, $command, array $arguments = [])
62
    {
63
        $this->commands[] = compact('type', 'command', 'arguments');
64
    }
65
66
    public function getCommands(): array
67
    {
68
        return $this->commands;
69
    }
70
}
71