Test Setup Failed
Branch master (c77351)
by Simone
02:28
created

MandatoryConditional::buildException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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 RuntimeException;
15
use Sensorario\Resources\Resource;
16
use Sensorario\Resources\Validators\Interfaces\Validator;
17
18
final class MandatoryConditional implements Validator
19
{
20
    public function check(Resource $resource)
21
    {
22
        foreach ($resource->mandatory() as $key => $value) {
23
            if (isset($value['when']['has_value'])) {
24
                $name = $value['when']['property'];
25
                $value = $value['when']['has_value'];
26
27 View Code Duplication
                if (is_array($value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
                    foreach ($value as $value) {
29
                        if ($resource->get($name) === $value && $resource->hasNotProperty($key)) {
30
                            static::exceptionMessage($key, $value, $key);
1 ignored issue
show
Comprehensibility introduced by
Since Sensorario\Resources\Val...rs\MandatoryConditional is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
31
                        }
32
                    }
33
                }
34
35 View Code Duplication
                if (!is_array($value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
                    if ($resource->get($name) === $value && $resource->hasNotProperty($key)) {
37
                        self::exceptionMessage($name, $value, $key);
38
                    }
39
                }
40
            }
41
        }
42
    }
43
44
    private static function exceptionMessage($name, $value, $key)
45
    {
46
        throw new RuntimeException(
47
            'When property `' . $name . '` '
48
            . 'has value ' . '`' . $value . '` '
49
            . 'also `' . $key . '` is mandatory'
50
        );
51
    }
52
}
53