IntegerElement::tryCast()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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