Completed
Push — master ( 5927eb...1e13db )
by
unknown
04:04
created

MultipleOf   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 32
ccs 19
cts 19
cp 1
rs 10
wmc 3
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 26 3
1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard;
6
use League\JsonGuard\ErrorCode;
7
use League\JsonGuard\ValidationError;
8
9
class MultipleOf implements PropertyConstraint
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 2
    public static function validate($value, $multiple, $pointer = null)
15
    {
16 2
        if (!is_numeric($value)) {
17 2
            return null;
18
        }
19
20
        // for some reason fmod does not return 0 for cases like fmod(0.0075,0.0001) so I'm doing this manually.
21 2
        $quotient = $value / $multiple;
22 2
        $mod      = $quotient - floor($quotient);
23 2
        if ($mod == 0) {
24 2
            return null;
25
        }
26
27 2
        $message = sprintf(
28 2
            'Number "%d" is not a multiple of "%d"',
29 2
            JsonGuard\asString($value),
30 2
            JsonGuard\asString($multiple)
31 2
        );
32 2
        return new ValidationError(
33 2
            $message,
34 2
            ErrorCode::INVALID_MULTIPLE,
35 2
            $value,
36 2
            $pointer,
37 2
            ['multiple_of' => $multiple]
38 2
        );
39
    }
40
}
41