| Conditions | 3 |
| Paths | 3 |
| Total Lines | 24 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 8 |
| CRAP Score | 3 |
| Changes | 0 | ||
| 1 | <?php |
||
| 18 | 38 | public function validate(Resource $resource) |
|
| 19 | { |
||
| 20 | 38 | $container = ValidatorContainer::load(); |
|
| 21 | |||
| 22 | $validators = [ |
||
| 23 | 38 | 'right.type', |
|
| 24 | 'mandatory.conditional', |
||
| 25 | 'mandatory.property', |
||
| 26 | 'mandatory.without.default', |
||
| 27 | 'allowed.properties', |
||
| 28 | 'allowed.values', |
||
| 29 | 'rewrite.values', |
||
| 30 | 'allowed.ranges', |
||
| 31 | 'right.type', |
||
| 32 | ]; |
||
| 33 | |||
| 34 | 38 | foreach ($validators as $name) { |
|
| 35 | 38 | if ($container->contains($name)) { |
|
| 36 | 38 | $validator = $container->get($name); |
|
| 37 | } |
||
| 38 | |||
| 39 | 38 | $validator->check($resource); |
|
|
|
|||
| 40 | } |
||
| 41 | 24 | } |
|
| 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: