1 | <?php |
||
7 | class Run implements Runner |
||
8 | { |
||
9 | protected $commands = []; |
||
10 | |||
11 | public function artisan(string $command, array $arguments = []): Runner |
||
17 | |||
18 | public function external(string $command, ...$arguments): Runner |
||
24 | |||
25 | public function callable(callable $function, ...$arguments): Runner |
||
31 | |||
32 | public function dispatch($job): Runner |
||
38 | |||
39 | public function dispatchNow($job): Runner |
||
45 | |||
46 | public function publish(array $providers): Runner |
||
47 | { |
||
48 | foreach ($providers as $provider => $tag) { |
||
49 | $arguments['--provider'] = is_numeric($provider) ? $tag : $provider; |
||
|
|||
50 | |||
51 | if (! is_numeric($provider) and is_string($tag)) { |
||
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 = []) |
||
65 | |||
66 | public function getCommands(): array |
||
70 | } |
||
71 |
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:
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 thebar
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.