OneOf::validate()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 11
Ratio 47.83 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 6
nop 3
dl 11
loc 23
ccs 15
cts 15
cp 1
crap 4
rs 8.7972
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraint\DraftFour;
4
5
use League\JsonGuard\Assert;
6
use League\JsonGuard\ConstraintInterface;
7
use League\JsonGuard\Validator;
8
use function League\JsonGuard\error;
9
use function League\JsonGuard\pointer_push;
10
11
final class OneOf implements ConstraintInterface
12
{
13
    const KEYWORD = 'oneOf';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 6
    public function validate($value, $parameter, Validator $validator)
19
    {
20 6
        Assert::type($parameter, 'array', self::KEYWORD, $validator->getSchemaPath());
21 4
        Assert::notEmpty($parameter, self::KEYWORD, $validator->getSchemaPath());
22
23 2
        $passed = 0;
24 2 View Code Duplication
        foreach ($parameter as $key => $schema) {
0 ignored issues
show
Duplication introduced by Matt Allan
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...
25 2
            $subValidator = $validator->makeSubSchemaValidator(
26 2
                $value,
27 2
                $schema,
28 2
                $validator->getDataPath(),
29 2
                pointer_push($validator->getSchemaPath(), $key)
30
            );
31 2
            if ($subValidator->passes()) {
32 2
                $passed++;
33
            }
34
        }
35 2
        if ($passed !== 1) {
36 2
            return error('The data must match exactly one of the schemas.', $validator);
37
        }
38
39 2
        return null;
40
    }
41
}
42