MultipleOf::check()   B
last analyzed

Complexity

Conditions 11
Paths 18

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 14
c 2
b 0
f 0
nc 18
nop 1
dl 0
loc 29
ccs 0
cts 8
cp 0
crap 132
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Rules;
13
14
class MultipleOf extends AbstractRule
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $fillableParams = ['value'];
20
21
    /**
22
     * {@inheritDoc}
23
     */
24
    public function check($value): bool
25
    {
26
        $this->requireParameters($this->fillableParams);
27
28
        if (is_int($value) || is_float($value)) {
29
            $value = (string) $value;
30
        }
31
32
        if (! is_string($value)) {
33
            return false;
34
        }
35
36
        if (! is_numeric($compare = $this->parameter('value'))) {
37
            $compare = $this->getAttribute()->getValue($compare);
38
        }
39
40
        if (! is_numeric($value) || ! is_numeric($compare) || ((int) $compare === 0 && (int) $value === 0)) {
41
            return false;
42
        }
43
44
        if ((int) $value === 0) {
45
            return true;
46
        }
47
48
        if ((int) $compare === 0) {
49
            return false;
50
        }
51
52
        return $value % $compare === 0;
53
    }
54
}
55