Conditions | 7 |
Paths | 4 |
Total Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
27 | public function getDataCellValue($model, $key, $index) |
||
28 | { |
||
29 | $prc = mt_rand(1, 100); |
||
30 | switch ($prc) { |
||
31 | case $prc >= 0 && $prc <= 40: |
||
32 | $level = 'progress-bar-success'; |
||
33 | break; |
||
34 | case $prc >= 41 && $prc <= 70: |
||
35 | $level = 'progress-bar-warning'; |
||
36 | break; |
||
37 | case $prc >= 71 && $prc <= 100: |
||
38 | $level = 'progress-bar-danger'; |
||
39 | break; |
||
40 | } |
||
41 | |||
42 | return Progress::widget([ |
||
43 | 'percent' => $prc, |
||
44 | 'label' => $prc . '%', |
||
45 | 'barOptions' => ['class' => $level], |
||
|
|||
46 | 'options' => ['style' => 'background-color: grey;'], |
||
47 | ]); |
||
48 | } |
||
49 | } |
||
50 |
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: