Completed
Pull Request — master (#108)
by Matt
12:20
created

Max   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 50.65 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 39
loc 77
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validateMax() 15 15 4
A validateExclusiveMax() 14 14 3
A validate() 10 10 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\Assert;
6
use League\JsonGuard\ValidationError;
7
use League\JsonGuard\Validator;
8
9
class Max implements Constraint
10
{
11
    const KEYWORD           = 'maximum';
12
    const EXCLUSIVE_KEYWORD = 'exclusiveMaximum';
13
14
    /**
15
     * @var int|null
16
     */
17
    private $precision;
18 16
19
    /**
20 16
     * @param int|null $precision
21
     */
22 14
    public function __construct($precision = 10)
23 6
    {
24
        $this->precision = $precision;
25
    }
26 14
27
    /**
28
     * {@inheritdoc}
29
     */
30 View Code Duplication
    public function validate($value, $parameter, Validator $validator)
0 ignored issues
show
Duplication introduced by
This method 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...
31
    {
32
        Assert::type($parameter, 'number', self::KEYWORD, $validator->getSchemaPath());
33
34
        if (isset($validator->getSchema()->exclusiveMaximum) && $validator->getSchema()->exclusiveMaximum === true) {
35
            return self::validateExclusiveMax($value, $parameter, $validator->getDataPath());
36 14
        }
37
38 14
        return self::validateMax($value, $parameter, $validator->getDataPath());
39 14
    }
40 14
41
    /**
42
     * @param mixed       $value
43 12
     * @param mixed       $parameter
44 12
     * @param string|null $pointer
45 12
     *
46 12
     * @return \League\JsonGuard\ValidationError|null
47 12
     */
48 12 View Code Duplication
    private function validateMax($value, $parameter, $pointer)
0 ignored issues
show
Duplication introduced by
This method 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...
49 12
    {
50
        if (!is_numeric($value) ||
51
            bccomp($value, $parameter, $this->precision) === -1 || bccomp($value, $parameter, $this->precision) === 0) {
52
            return null;
53
        }
54
55
        return new ValidationError(
56
            'Value {value} is not at most {max}',
57
            self::KEYWORD,
58
            $value,
59 6
            $pointer,
60
            ['value' => $value, 'max' => $parameter]
61 6
        );
62 4
    }
63
64
    /**
65 6
     * @param mixed       $value
66 6
     * @param mixed       $parameter
67 6
     * @param string|null $pointer
68 6
     *
69 6
     * @return \League\JsonGuard\ValidationError|null
70 6
     */
71 6 View Code Duplication
    private function validateExclusiveMax($value, $parameter, $pointer)
0 ignored issues
show
Duplication introduced by
This method 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...
72
    {
73
        if (!is_numeric($value) || bccomp($value, $parameter, $this->precision) === -1) {
74
            return null;
75
        }
76
77
        return new ValidationError(
78
            'Value {value} is not less than {exclusive_max}',
79
            self::EXCLUSIVE_KEYWORD,
80
            $value,
81
            $pointer,
82
            ['value' => $value, 'exclusive_max' => $parameter]
83
        );
84
    }
85
}
86