Completed
Pull Request — master (#143)
by Simone
01:52
created

MandatoryConditional::check()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 9

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 7.041
cc 9
eloc 12
nc 6
nop 1
crap 9
1
<?php
2
3
/**
4
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sensorario\Resources\Validators\Validators;
13
14
use Sensorario\Resources\Resource;
15
use Sensorario\Resources\Validators\Interfaces\Validator;
16
17
final class MandatoryConditional implements Validator
18
{
19 32
    public function check(Resource $resource)
20
    {
21 32
        foreach ($resource->mandatory() as $key => $value) {
22 19
            if (isset($value['when']['has_value'])) {
23 4
                $name = $value['when']['property'];
24 4
                $value = $value['when']['has_value'];
25
26 4
                if ($isArray = is_array($value)) {
0 ignored issues
show
Unused Code introduced by
$isArray is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
27 3
                    foreach ($value as $value) {
28 3
                        if ($resource->get($name) === $value && $resource->hasNotProperty($key)) {
29 3
                            self::exceptionMessage($key, $value, $key);
30
                        }
31
                    }
32
                } else {
33 1
                    if ($resource->get($name) === $value && $resource->hasNotProperty($key)) {
34 1
                        self::exceptionMessage($name, $value, $key);
35
                    }
36
                }
37
            }
38
        }
39 30
    }
40
41 2
    private static function exceptionMessage($name, $value, $key)
42
    {
43 2
        throw new \Sensorario\Resources\Exceptions\PropertyException(
44 2
            'When property `' . $name . '` '
45 2
            . 'has value ' . '`' . $value . '` '
46 2
            . 'also `' . $key . '` is mandatory'
47
        );
48
    }
49
}
50