Passed
Push — master ( ea62c3...cd2ef0 )
by Bruno
09:11
created

RequiredWithAll::validate()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
nc 17
nop 3
dl 0
loc 21
ccs 14
cts 14
cp 1
crap 7
rs 8.8333
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Validator;
4
5
use Formularium\Exception\ValidatorException;
6
use Formularium\Field;
7
use Formularium\Model;
8
use Formularium\ValidatorInterface;
9
use Formularium\ValidatorMetadata;
10
11
/**
12
 * The field under validation must be present and not empty only if all of the other specified fields are present.
13
 */
14
class RequiredWithAll implements ValidatorInterface
15 2
{
16
    public function validate($value, array $validators = [], Model $model = null)
17 2
    {
18 2
        if ($validators[self::class] ?? false) {
19 2
            $expectedFields = $validators[self::class];
20 1
            if (!is_array($expectedFields)) {
21
                $expectedFields = [$expectedFields];
22 2
            }
23 2
            $found = true;
24 2
            $data = $model->getData();
0 ignored issues
show
Bug introduced by
The method getData() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
            /** @scrutinizer ignore-call */ 
25
            $data = $model->getData();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25 2
            foreach ($expectedFields as $ef) {
26 1
                if (!array_key_exists($ef, $data)) {
27 2
                    $found = false;
28
                    break;
29
                }
30
            }
31 2
32 1
            if ($found && empty($value)) {
33 1
                throw new ValidatorException("Field is required when all fields " . implode(',', $expectedFields) . ' are present');
34
            }
35
        }
36 1
        return $value;
37
    }
38
39
    public function getMetadata(): ValidatorMetadata
40
    {
41
        return new ValidatorMetadata(
42
            __CLASS__,
43
            "The field under validation must be present and not empty only if all of the other specified fields are present.",
44
            [
45
                new ValidatorArgs(
0 ignored issues
show
Bug introduced by
The type Formularium\Validator\ValidatorArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
                    'fields',
47
                    '[String]',
48
                    'The fields that are required with'
49
                )
50
            ]
51
        );
52
    }
53
}
54