Properties   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 38.24 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 13
loc 34
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 13 26 6

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\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