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

IntegerElement   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 8
c 1
b 0
f 0
dl 0
loc 32
ccs 10
cts 10
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toHttp() 0 3 2
A toPhp() 0 3 3
A tryCast() 0 11 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 75
    protected function toPhp($httpValue): ?int
20
    {
21 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...
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 22
    protected function toHttp($phpValue): ?string
28
    {
29 22
        return $phpValue === null ? null : (string) $phpValue;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 31
    protected function tryCast($value): ?int
36
    {
37 31
        if ($value === null) {
38 4
            return null;
39
        }
40
41 30
        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 23
        return (int) $value;
46
    }
47
}
48