Passed
Push — master ( cd2ef0...b8142e )
by Bruno
07:52
created

RequiredWith::validate()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 16
ccs 12
cts 12
cp 1
crap 6
rs 9.2222
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 any of the other specified fields are present.
13
 */
14
class RequiredWith implements ValidatorInterface
15 2
{
16
    public function validate($value, array $options = [], Model $model = null)
17 2
    {
18 2
        $expectedFields = $options['fields'];
19 2
20 2
        $found = false;
21
        $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

21
        /** @scrutinizer ignore-call */ 
22
        $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...
22 2
        foreach ($expectedFields as $ef) {
23 2
            if (array_key_exists($ef, $data) && !empty($data[$ef])) {
24 2
                $found = true;
25 2
                break;
26 2
            }
27 2
        }
28
        if ($found && empty($value)) {
29
            throw new ValidatorException("Field is required when at least one of fields " . implode(',', $expectedFields) . ' are present');
30 2
        }
31 1
        return $value;
32 1
    }
33
34
    public function getMetadata(): ValidatorMetadata
35 1
    {
36
        return new ValidatorMetadata(
37
            __CLASS__,
38
            "The field under validation must be present and not empty only if any of the other specified fields are present.",
39
            [
40
                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...
41
                    'fields',
42
                    '[String]',
43
                    'The fields that are required with'
44
                )
45
            ]
46
        );
47
    }
48
}
49