Conditions | 7 |
Paths | 12 |
Total Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
16 | public function rules() |
||
17 | { |
||
18 | if($this->segment(3)!="") { |
||
19 | $user = User::find($this->segment(3)); |
||
20 | } |
||
21 | |||
22 | switch($this->method()) |
||
23 | { |
||
24 | case 'GET': |
||
25 | case 'DELETE': |
||
26 | { |
||
27 | return []; |
||
28 | } |
||
29 | case 'POST': |
||
30 | { |
||
31 | return [ |
||
32 | 'name' => 'required|min:3', |
||
33 | 'email' => 'required|email|unique:users,email', |
||
34 | 'password' => 'required', |
||
35 | ]; |
||
36 | } |
||
37 | case 'PUT': |
||
38 | case 'PATCH': |
||
39 | { |
||
40 | return [ |
||
41 | 'name' => 'required|min:3', |
||
42 | 'email' => 'required|email|unique:users,email,'.$user->id, |
||
|
|||
43 | ]; |
||
44 | } |
||
45 | default: |
||
46 | break; |
||
47 | } |
||
48 | } |
||
49 | |||
61 |
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: