NotConstraint::apply()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 4
crap 2
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\Constraint;
13
use JVal\Context;
14
use JVal\Exception\Constraint\InvalidTypeException;
15
use JVal\Types;
16
use JVal\Walker;
17
use stdClass;
18
19
/**
20
 * Constraint for the "not" keyword.
21
 */
22
class NotConstraint implements Constraint
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 373
    public function keywords()
28
    {
29 373
        return ['not'];
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 357
    public function supports($type)
36
    {
37 357
        return true;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 17
    public function normalize(stdClass $schema, Context $context, Walker $walker)
44
    {
45 17
        $context->enterNode('not');
46
47 17
        if (!is_object($schema->not)) {
48 1
            throw new InvalidTypeException($context, Types::TYPE_OBJECT);
49
        }
50
51 16
        $walker->parseSchema($schema->not, $context);
52 16
        $context->leaveNode();
53 16
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 14 View Code Duplication
    public function apply($instance, stdClass $schema, Context $context, Walker $walker)
59
    {
60 14
        $altContext = $context->duplicate();
61 14
        $walker->applyConstraints($instance, $schema->not, $altContext);
62
63 14
        if ($altContext->countViolations() === $context->countViolations()) {
64 6
            $context->addViolation('should not match schema in "not"');
65 6
        }
66 14
    }
67
}
68