Minimum::validate()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 3
nop 3
dl 15
loc 15
ccs 8
cts 8
cp 1
crap 6
rs 8.8571
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\error;
9
10 View Code Duplication
final class Minimum implements ConstraintInterface
0 ignored issues
show
Duplication introduced by Matt Allan
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...
11
{
12
    const KEYWORD = 'minimum';
13
14
    /**
15
     * @var int|null
16
     */
17
    private $precision;
18
19
    /**
20
     * @param int|null $precision
21
     */
22 18
    public function __construct($precision = 10)
23
    {
24 18
        $this->precision = $precision;
25 18
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 18
    public function validate($value, $parameter, Validator $validator)
31
    {
32 18
        Assert::type($parameter, 'number', self::KEYWORD, $validator->getSchemaPath());
33
34 16
        if (isset($validator->getSchema()->exclusiveMinimum) && $validator->getSchema()->exclusiveMinimum === true) {
35 6
            return null;
36
        }
37
38 14
        if (!is_numeric($value) ||
39 14
            bccomp($value, $parameter, $this->precision) === 1 || bccomp($value, $parameter, $this->precision) === 0) {
40 14
            return null;
41
        }
42
43 8
        return error('The number must be at least {parameter}.', $validator);
44
    }
45
}
46