Passed
Push — master ( c9351b...a8a6df )
by Smoren
02:20
created

FloatRule   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 45.16%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 71
ccs 14
cts 31
cp 0.4516
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A nonFractional() 0 6 1
A fractional() 0 6 1
A finite() 0 6 2
A infinite() 0 6 2
A __construct() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Rules;
6
7
use Smoren\Validator\Checks\Check;
8
use Smoren\Validator\Interfaces\FloatRuleInterface;
9
use Smoren\Validator\Structs\CheckErrorName;
10
use Smoren\Validator\Structs\CheckName;
11
12
class FloatRule extends NumericRule implements FloatRuleInterface
13
{
14
    protected const DEFAULT_NAME = 'float';
15
16
    /**
17
     * @param string $name
18
     */
19 8
    public function __construct(string $name)
20
    {
21 8
        Rule::__construct($name);
22 8
        $this->check(new Check(
23 8
            CheckName::FLOAT,
24 8
            CheckErrorName::NOT_FLOAT,
25 8
            fn ($value) => is_float($value),
26 8
            []
27 8
        ), true);
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     *
33
     * @return static
34
     */
35
    public function fractional(): self
36
    {
37
        return $this->check(new Check(
38
            CheckName::FRACTIONAL,
39
            CheckErrorName::NOT_FRACTIONAL,
40
            fn ($value) => \abs($value - \round($value)) >= PHP_FLOAT_EPSILON
41
        ));
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     *
47
     * @return static
48
     */
49 3
    public function nonFractional(): self
50
    {
51 3
        return $this->check(new Check(
52 3
            CheckName::NOT_FRACTIONAL,
53 3
            CheckErrorName::FRACTIONAL,
54 3
            fn ($value) => \abs($value - \round($value)) < PHP_FLOAT_EPSILON
55 3
        ));
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     *
61
     * @return static
62
     */
63
    public function finite(): self
64
    {
65
        return $this->check(new Check(
66
            CheckName::FINITE,
67
            CheckErrorName::NOT_FINITE,
68
            fn ($value) => $value > -INF && $value < INF,
69
        ));
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     *
75
     * @return static
76
     */
77
    public function infinite(): self
78
    {
79
        return$this->check(new Check(
80
            CheckName::INFINITE,
81
            CheckErrorName::NOT_INFINITE,
82
            fn ($value) => $value === -INF || $value === INF,
83
        ));
84
    }
85
}
86