NotConstraint   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 19.57 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 9
loc 46
c 0
b 0
f 0
wmc 6
lcom 0
cbo 3
ccs 18
cts 18
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A keywords() 0 4 1
A supports() 0 4 1
A normalize() 0 11 2
A apply() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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