AdditionalProperties::getDiff()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 6
nop 2
dl 0
loc 23
ccs 12
cts 12
cp 1
crap 4
rs 8.7972
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraint\DraftFour;
4
5
use League\JsonGuard;
6
use League\JsonGuard\Assert;
7
use League\JsonGuard\ConstraintInterface;
8
use League\JsonGuard\Validator;
9
use function League\JsonGuard\error;
10
use function League\JsonGuard\pointer_push;
11
12
final class AdditionalProperties implements ConstraintInterface
13
{
14
    const KEYWORD = 'additionalProperties';
15
16
    /**
17
     * {@inheritdoc}
18
     */
19 16
    public function validate($value, $parameter, Validator $validator)
20
    {
21 16
        Assert::type($parameter, ['object', 'boolean'], self::KEYWORD, $validator->getSchemaPath());
22
23 14
        if (!is_object($value)) {
24 4
            return null;
25
        }
26
27 14
        $diff = self::getDiff($value, $validator->getSchema());
28
29 14
        if (count($diff) === 0) {
30 10
            return null;
31
        }
32
33 12
        if ($parameter === false) {
34 8
            return error('The object must not contain additional properties ({cause}).', $validator)
35 8
                ->withCause($diff);
36 6
        } elseif (is_object($parameter)) {
37
            // If additionalProperties is an object it's a schema,
38
            // so validate all additional properties against it.
39 6
            $errors = [];
40 6 View Code Duplication
            foreach ($diff as $property) {
0 ignored issues
show
Duplication introduced by
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...
41 6
                $subValidator = $validator->makeSubSchemaValidator(
42 6
                    $value->$property,
43 6
                    $parameter,
44 6
                    pointer_push($validator->getDataPath(), $property)
45
                );
46 6
                $errors = array_merge($errors, $subValidator->errors());
47
            }
48
49 6
            return $errors;
50
        }
51
    }
52
53
    /**
54
     * Get the properties in $value which are not in $schema 'properties' or matching 'patternProperties'.
55
     *
56
     * @param object $value
57
     * @param object $schema
58
     *
59
     * @return array
60
     */
61 14
    private static function getDiff($value, $schema)
62
    {
63 14
        if (property_exists($schema, Properties::KEYWORD)) {
64 10
            $definedProperties = array_keys(get_object_vars($schema->properties));
65
        } else {
66 6
            $definedProperties = [];
67
        }
68
69 14
        $actualProperties = array_keys(get_object_vars($value));
70 14
        $diff             = array_diff($actualProperties, $definedProperties);
71
72
        // The diff doesn't account for patternProperties, so lets filter those out too.
73 14
        if (property_exists($schema, PatternProperties::KEYWORD)) {
74 4
            foreach ($schema->patternProperties as $property => $schema) {
75 4
                $matches = JsonGuard\properties_matching_pattern($property, $diff);
76 4
                $diff    = array_diff($diff, $matches);
77
            }
78
79 4
            return $diff;
80
        }
81
82 12
        return $diff;
83
    }
84
}
85