AdditionalItems   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 2

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 10.81 %

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
dl 8
loc 74
ccs 28
cts 29
cp 0.9655
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 20 6
A getItems() 0 4 2
A validateAdditionalItemsAgainstSchema() 8 14 2
A validateAdditionalItemsWhenNotAllowed() 0 6 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
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 AdditionalItems implements ConstraintInterface
12
{
13
    const KEYWORD = 'additionalItems';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 4
    public function validate($value, $parameter, Validator $validator)
19
    {
20 4
        Assert::type($parameter, ['boolean', 'object'], self::KEYWORD, $validator->getSchemaPath());
21
22 2
        if (!is_array($value) || $parameter === true) {
23 2
            return null;
24
        }
25
26 2
        if (!is_array($items = self::getItems($validator->getSchema()))) {
27 2
            return null;
28
        }
29
30 2
        if ($parameter === false) {
31 2
            return self::validateAdditionalItemsWhenNotAllowed($value, $items, $validator);
32 2
        } elseif (is_object($parameter)) {
33 2
            $additionalItems = array_slice($value, count($items));
34
35 2
            return self::validateAdditionalItemsAgainstSchema($additionalItems, $parameter, $validator);
36
        }
37
    }
38
39
    /**
40
     * @param object $schema
41
     *
42
     * @return mixed
43
     */
44 2
    private static function getItems($schema)
45
    {
46 2
        return property_exists($schema, Items::KEYWORD) ? $schema->items : null;
47
    }
48
49
    /**
50
     * @param array     $items
51
     * @param object    $schema
52
     * @param Validator $validator
53
     *
54
     * @return array
55
     */
56 2
    private static function validateAdditionalItemsAgainstSchema($items, $schema, Validator $validator)
57
    {
58 2
        $errors = [];
59 2 View Code Duplication
        foreach ($items as $key => $item) {
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...
60 2
            $subValidator = $validator->makeSubSchemaValidator(
61 2
                $item,
62 2
                $schema,
63 2
                pointer_push($validator->getDataPath(), $key)
64 1
            );
65 2
            $errors = array_merge($errors, $subValidator->errors());
66 1
        }
67
68 2
        return $errors;
69
    }
70
71
    /**
72
     * @param array                       $value
73
     * @param array                       $items
74
     * @param \League\JsonGuard\Validator $validator
75
     *
76
     * @return \League\JsonGuard\ValidationError
77
     */
78 2
    private static function validateAdditionalItemsWhenNotAllowed($value, $items, Validator $validator)
79
    {
80 2
        if (count($value) > count($items)) {
81 2
            return error('The array must not contain additional items.', $validator);
82
        }
83 2
    }
84
}
85