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

Max::validateExclusiveMax()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 3
dl 14
loc 14
ccs 1
cts 1
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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