AbstractOfConstraint   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 25 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 10
loc 40
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 1
B normalize() 10 26 5

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\EmptyArrayException;
15
use JVal\Exception\Constraint\InvalidTypeException;
16
use JVal\Types;
17
use JVal\Walker;
18
use stdClass;
19
20
/**
21
 * Base class for constraints based on a set of sub-schemas.
22
 */
23
abstract class AbstractOfConstraint implements Constraint
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 357
    public function supports($type)
29
    {
30 357
        return true;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 68
    public function normalize(stdClass $schema, Context $context, Walker $walker)
37
    {
38 68
        $keyword = $this->keywords()[0];
39 68
        $context->enterNode($keyword);
40
41 68
        if (!is_array($schema->{$keyword})) {
42 3
            throw new InvalidTypeException($context, Types::TYPE_ARRAY);
43
        }
44
45 65
        if (count($schema->{$keyword}) === 0) {
46 3
            throw new EmptyArrayException($context);
47 1
        }
48
49 62 View Code Duplication
        foreach ($schema->{$keyword} as $index => $subSchema) {
50 62
            $context->enterNode($index);
51
52 62
            if (!is_object($subSchema)) {
53 3
                throw new InvalidTypeException($context, Types::TYPE_OBJECT);
54
            }
55
56 62
            $walker->parseSchema($subSchema, $context);
57 62
            $context->leaveNode();
58 62
        }
59
60 59
        $context->leaveNode();
61 59
    }
62
}
63