Properties::validate()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 13
Ratio 50 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 5
nop 3
dl 13
loc 26
ccs 15
cts 15
cp 1
crap 6
rs 8.439
c 0
b 0
f 0
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\pointer_push;
9
10
final class Properties implements ConstraintInterface
11
{
12
    const KEYWORD = 'properties';
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 38
    public function validate($value, $parameter, Validator $validator)
18
    {
19 38
        Assert::type($parameter, ['array', 'object'], self::KEYWORD, $validator->getSchemaPath());
20
21 36
        if (!is_object($value)) {
22 8
            return null;
23
        }
24
25
        // Iterate through the properties and create a new validator for that property's schema and data.
26 36
        $errors = [];
27 36 View Code Duplication
        foreach ($parameter as $property => $schema) {
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...
28 36
            if (is_object($value) && property_exists($value, $property)) {
29 36
                $subValidator = $validator->makeSubSchemaValidator(
30 36
                    $value->$property,
31 36
                    $schema,
32 36
                    pointer_push($validator->getDataPath(), $property),
33 36
                    pointer_push($validator->getSchemaPath(), $property)
34
                );
35 36
                if ($subValidator->fails()) {
36 34
                    $errors = array_merge($errors, $subValidator->errors());
37
                }
38
            }
39
        }
40
41 34
        return $errors;
42
    }
43
}
44