Test Setup Failed
Push — master ( 1e1d79...717f23 )
by Arkadiusz
02:13
created

NumberConverter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fit() 0 4 1
B transform() 0 14 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpml\Preprocessing;
6
7
final class NumberConverter implements Preprocessor
8
{
9
    /**
10
     * @var bool
11
     */
12
    private $transformTargets;
13
14
    /**
15
     * @var mixed
16
     */
17
    private $nonNumericPlaceholder;
18
19
    /**
20
     * @param mixed $nonNumericPlaceholder
21
     */
22
    public function __construct(bool $transformTargets = false, $nonNumericPlaceholder = null)
23
    {
24
        $this->transformTargets = $transformTargets;
25
        $this->nonNumericPlaceholder = $nonNumericPlaceholder;
26
    }
27
28
    public function fit(array $samples, ?array $targets = null): void
29
    {
30
        //nothing to do
31
    }
32
33
    public function transform(array &$samples, ?array &$targets = null): void
34
    {
35
        foreach ($samples as &$sample) {
36
            foreach ($sample as &$feature) {
37
                $feature = is_numeric($feature) ? (float) $feature : $this->nonNumericPlaceholder;
38
            }
39
        }
40
41
        if ($this->transformTargets && is_array($targets)) {
42
            foreach ($targets as &$target) {
43
                $target = is_numeric($target) ? (float) $target : $this->nonNumericPlaceholder;
44
            }
45
        }
46
    }
47
}
48