Completed
Push — master ( e24db7...5dd27b )
by Matt
02:58
created

Max   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 62
loc 62
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 8 8 3
A validateMax() 15 15 4
A validateExclusiveMax() 14 14 3

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\Constraints;
4
5
use League\JsonGuard;
6
use League\JsonGuard\ValidationError;
7
8 View Code Duplication
class Max implements ParentSchemaAwarePropertyConstraint
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
9
{
10
    const KEYWORD           = 'max';
11
    const EXCLUSIVE_KEYWORD = 'exclusiveMax';
12
13
    /**
14
     * {@inheritdoc}
15
     */
16 14
    public static function validate($value, $schema, $parameter, $pointer = null)
17
    {
18 14
        if (isset($schema->exclusiveMaximum) && $schema->exclusiveMaximum === true) {
19 6
            return self::validateExclusiveMax($value, $parameter, $pointer);
20
        }
21
22 14
        return self::validateMax($value, $parameter, $pointer);
23
    }
24
25
    /**
26
     * @param mixed       $value
27
     * @param mixed       $parameter
28
     * @param string|null $pointer
29
     *
30
     * @return \League\JsonGuard\ValidationError|null
31
     */
32 14
    public static function validateMax($value, $parameter, $pointer = null)
33
    {
34 14
        if (!is_numeric($value) ||
35 14
            JsonGuard\compare($value, $parameter) === -1 || JsonGuard\compare($value, $parameter) === 0) {
36 14
            return null;
37
        }
38
39 12
        return new ValidationError(
40 12
            'Value {value} is not at most {max}',
41 12
            self::KEYWORD,
42 12
            $value,
43 12
            $pointer,
44 12
            ['value' => $value, 'max' => $parameter]
45 12
        );
46
    }
47
48
    /**
49
     * @param mixed       $value
50
     * @param mixed       $parameter
51
     * @param string|null $pointer
52
     *
53
     * @return \League\JsonGuard\ValidationError|null
54
     */
55 6
    public static function validateExclusiveMax($value, $parameter, $pointer = null)
56
    {
57 6
        if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === -1) {
58 4
            return null;
59
        }
60
61 6
        return new ValidationError(
62 6
            'Value {value} is not less than {exclusive_max}',
63 6
            self::EXCLUSIVE_KEYWORD,
64 6
            $value,
65 6
            $pointer,
66 6
            ['value' => $value, 'exclusive_max' => $parameter]
67 6
        );
68
    }
69
}
70