NumberNormalizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 12
c 1
b 0
f 0
dl 0
loc 33
ccs 11
cts 12
cp 0.9167
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getNormalizedValue() 0 16 3
A scale() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Palmtree\Csv\Normalizer;
6
7
/**
8
 * NumberNormalizer converts numeric strings to integers and floats.
9
 */
10
class NumberNormalizer extends AbstractNormalizer
11
{
12
    private ?int $scale = null;
13
14
    /**
15
     * Sets the amount of decimal places to round to. Defaults to null which performs no rounding.
16
     */
17 1
    public function scale(?int $scale = null): self
18
    {
19 1
        $this->scale = $scale;
20
21 1
        return $this;
22
    }
23
24
    /**
25
     * @return float|int
26
     */
27 6
    protected function getNormalizedValue(string $value)
28
    {
29 6
        if (!is_numeric($value)) {
30
            return 0;
31
        }
32
33 6
        $value = trim($value);
34 6
        $value = ltrim($value, '0');
35
36 6
        $numberValue = json_decode($value);
37
38 6
        if ($this->scale !== null) {
39 1
            $numberValue = round($numberValue, $this->scale);
40
        }
41
42 6
        return $numberValue;
43
    }
44
}
45