Completed
Pull Request — master (#109)
by Simone
02:16
created

MandatoryConditional::check()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 23
Code Lines 12

Duplication

Lines 12
Ratio 52.17 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 23
rs 5.6534
cc 10
eloc 12
nc 8
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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