Conditions | 4 |
Paths | 3 |
Total Lines | 27 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
15 | public function handle() |
||
16 | { |
||
17 | $this->warn("Let's create your new uptime monitor!"); |
||
18 | |||
19 | $url = $this->ask("Which url to you want to monitor? Should start with either 'http://' or 'https://'"); |
||
20 | |||
21 | $url = Url::fromString($url); |
||
22 | |||
23 | if (! in_array($url->getScheme(), ['http', 'https'])) { |
||
24 | $this->error('The given url did not start with `http://` or `https://`.'); |
||
25 | |||
26 | return; |
||
27 | } |
||
28 | |||
29 | if ($this->confirm('Should we look for a specific string on the response?')) { |
||
30 | $lookForString = $this->ask('Which string?'); |
||
31 | } |
||
32 | |||
33 | $site = Site::create([ |
||
34 | 'url' => $url, |
||
35 | 'look_for_string' => $lookForString ?? '', |
||
36 | 'uptime_check_method' => $lookForString === '' ? 'head' : 'get', |
||
|
|||
37 | 'check_ssl_certificate' => $url->getScheme() === 'https', |
||
38 | ]); |
||
39 | |||
40 | $this->warn("A new uptime monitor for {$site->url} was created!"); |
||
41 | } |
||
42 | } |
||
43 |
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: