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); |
|
|
|
|
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; |
|
|
|
|
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) { |
|
|
|
|
99
|
12 |
|
$this->addTag($tag); |
100
|
|
|
} |
101
|
12 |
|
} |
102
|
|
|
} |
103
|
|
|
|
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: