MultipleOf   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 17 3
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
final class MultipleOf implements ConstraintInterface
11
{
12
    const KEYWORD = 'multipleOf';
13
14
    /**
15
     * @var int|null
16
     */
17
    private $precision;
18
19
    /**
20
     * @param int|null $precision
21
     */
22 8
    public function __construct($precision = 10)
23
    {
24 8
        $this->precision = $precision;
25 8
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 8
    public function validate($value, $parameter, Validator $validator)
31
    {
32 8
        Assert::type($parameter, 'number', self::KEYWORD, $validator->getSchemaPath());
33 6
        Assert::nonNegative($parameter, self::KEYWORD, $validator->getSchemaPath());
34
35 4
        if (!is_numeric($value)) {
36 2
            return null;
37
        }
38
39 4
        $quotient = bcdiv($value, $parameter, $this->precision);
40 4
        $mod      = bcsub($quotient, floor($quotient), $this->precision);
41 4
        if (bccomp($mod, 0, $this->precision) === 0) {
42 4
            return null;
43
        }
44
45 2
        return error('The number must be a multiple of {parameter}.', $validator);
46
    }
47
}
48