Passed
Push — master ( 0a4514...70449d )
by Vincent
04:28
created

FloatElement::toPhp()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Bdf\Form\Leaf;
4
5
use TypeError;
6
7
/**
8
 * Element for a float value
9
 *
10
 * @see FloatElementBuilder for build the element
11
 *
12
 * @extends LeafElement<float>
13
 */
14
class FloatElement extends LeafElement
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 32
    protected function toPhp($httpValue): ?float
20
    {
21 32
        return $httpValue === null || $httpValue === '' ? null : (float) $httpValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $httpValue === nu...ll : (double)$httpValue also could return the type double which is incompatible with the return type mandated by Bdf\Form\Leaf\LeafElement::toPhp() of Bdf\Form\Leaf\T|null.
Loading history...
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 9
    protected function toHttp($phpValue): ?string
28
    {
29 9
        return $phpValue === null ? null : (string) $phpValue;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 19
    protected function tryCast($value): ?float
36
    {
37 19
        if ($value === null) {
38 1
            return null;
39
        }
40
41 18
        if (!is_numeric($value)) {
42 7
            throw new TypeError('The import()\'ed value of a '.static::class.' must be numeric or null');
43
        }
44
45 11
        return (float) $value;
46
    }
47
}
48