Passed
Push — master ( 25c805...999286 )
by Andy
02:56 queued 11s
created

NumberNormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
dl 0
loc 30
ccs 9
cts 12
cp 0.75
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getNormalizedValue() 0 13 3
A setDecimals() 0 5 1
A getDecimals() 0 3 1
1
<?php
2
3
namespace Palmtree\Csv\Normalizer;
4
5
/**
6
 * NumberNormalizer converts numeric strings to integers and floats.
7
 */
8
class NumberNormalizer extends AbstractNormalizer
9
{
10
    /** @var int|null */
11
    private $decimals;
12
13 1
    public function setDecimals(?int $decimals = null): self
14
    {
15 1
        $this->decimals = $decimals;
16
17 1
        return $this;
18
    }
19
20
    public function getDecimals(): ?int
21
    {
22
        return $this->decimals;
23
    }
24
25 6
    protected function getNormalizedValue(string $value)
26
    {
27 6
        if (!\is_numeric($value)) {
28
            return 0;
29
        }
30
31 6
        $numberValue = \trim($value) * 1;
32
33 6
        if ($this->decimals !== null) {
34 1
            $numberValue = \round($numberValue, $this->decimals);
35
        }
36
37 6
        return $numberValue;
38
    }
39
}
40