Failed Conditions
Pull Request — master (#256)
by Michael
02:09
created

ConstantFloatType   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 6
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 3 1
A __construct() 0 3 1
A describe() 0 3 1
A validate() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Type\Constant;
6
7
use Doctrine\Annotations\Type\ConstantScalarType;
8
use Doctrine\Annotations\Type\FloatType;
9
use function abs;
10
use function is_float;
11
use function sprintf;
12
13
/**
14
 * @internal
15
 */
16
final class ConstantFloatType extends FloatType implements ConstantScalarType
17
{
18
    /** Maximum tolerance boundary for float value accuracy. */
19
    private const EPSILON = 1e-15;
20
21
    /** @var float */
22
    private $value;
23
24 3
    public function __construct(float $value)
25
    {
26 3
        $this->value = $value;
27 3
    }
28
29 1
    public function getValue() : float
30
    {
31 1
        return $this->value;
32
    }
33
34 1
    public function describe() : string
35
    {
36 1
        return sprintf('%F', $this->value);
37
    }
38
39
    /**
40
     * @param mixed $value
41
     */
42 1
    public function validate($value) : bool
43
    {
44
        // Due to IEEE 754 float representation, comparisons are inaccurate so we need some level of tolerance.
45 1
        return is_float($value) && abs($value - $this->value) < self::EPSILON;
46
    }
47
}
48