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

ConstantFloatType   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 5
dl 0
loc 25
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
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 2
    public function __construct(float $value)
25
    {
26 2
        $this->value = $value;
27 2
    }
28
29 1
    public function describe() : string
30
    {
31 1
        return sprintf('%F', $this->value);
32
    }
33
34
    /**
35
     * @param mixed $value
36
     */
37 1
    public function validate($value) : bool
38
    {
39
        // Due to IEEE 754 float representation, comparisons are inaccurate so we need some level of tolerance.
40 1
        return is_float($value) && abs($value - $this->value) < self::EPSILON;
41
    }
42
}
43