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

MandatoryConditional   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 0
cbo 2
dl 0
loc 33
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B check() 0 21 9
A exceptionMessage() 0 8 1
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