AnyOfConstraint   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 34
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A keywords() 0 4 1
A apply() 0 20 4
1
<?php
2
3
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal\Constraint;
11
12
use JVal\Context;
13
use JVal\Walker;
14
use stdClass;
15
16
/**
17
 * Constraint for the "anyOf" keyword.
18
 */
19
class AnyOfConstraint extends AbstractOfConstraint
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 377
    public function keywords()
25
    {
26 377
        return ['anyOf'];
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 16
    public function apply($instance, stdClass $schema, Context $context, Walker $walker)
33
    {
34 16
        $accumulatingContext = $context->duplicate();
35 16
        $hasMatch = false;
36
37 16
        foreach ($schema->anyOf as $subSchema) {
38 16
            $originalCount = $accumulatingContext->countViolations();
39 16
            $walker->applyConstraints($instance, $subSchema, $accumulatingContext);
40
41 16
            if ($accumulatingContext->countViolations() === $originalCount) {
42 12
                $hasMatch = true;
43 12
                break;
44
            }
45 16
        }
46
47 16
        if (!$hasMatch) {
48 4
            $context->mergeViolations($accumulatingContext);
49 4
            $context->addViolation('instance must match at least one of the schemas listed in anyOf');
50 4
        }
51 16
    }
52
}
53