Completed
Pull Request — master (#132)
by Simone
04:43 queued 02:02
created

ResourcesValidator::validate()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 8
cts 8
cp 1
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]> *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Sensorario\Resources\Validators;
12
13
use Sensorario\Container\Container;
14
use Sensorario\Resources\Resource;
15
16
final class ResourcesValidator
17
{
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);
0 ignored issues
show
Bug introduced by
The variable $validator does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
40
        }
41 24
    }
42
}
43