Completed
Push — develop ( b0ab23...fb04b5 )
by Arkadiusz
02:58
created

Normalizer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 42.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 1
dl 32
loc 75
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A preprocess() 0 7 2
A normalizeL1() 16 16 4
A normalizeL2() 16 16 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\Preprocessing;
6
7
use Phpml\Exception\NormalizerException;
8
9
class Normalizer implements Preprocessor
10
{
11
    const NORM_L1 = 1;
12
    const NORM_L2 = 2;
13
14
    /**
15
     * @var int
16
     */
17
    private $norm;
18
19
    /**
20
     * @param int $norm
21
     *
22
     * @throws NormalizerException
23
     */
24
    public function __construct(int $norm = self::NORM_L2)
25
    {
26
        if (!in_array($norm, [self::NORM_L1, self::NORM_L2])) {
27
            throw NormalizerException::unknownNorm();
28
        }
29
30
        $this->norm = $norm;
31
    }
32
33
    /**
34
     * @param array $samples
35
     */
36
    public function preprocess(array &$samples)
37
    {
38
        $method = sprintf('normalizeL%s', $this->norm);
39
        foreach ($samples as &$sample) {
40
            $this->$method($sample);
41
        }
42
    }
43
44
    /**
45
     * @param array $sample
46
     */
47 View Code Duplication
    private function normalizeL1(array &$sample)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        $norm1 = 0;
50
        foreach ($sample as $feature) {
51
            $norm1 += abs($feature);
52
        }
53
54
        if (0 == $norm1) {
55
            $count = count($sample);
56
            $sample = array_fill(0, $count, 1.0 / $count);
57
        } else {
58
            foreach ($sample as &$feature) {
59
                $feature = $feature / $norm1;
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param array $sample
66
     */
67 View Code Duplication
    private function normalizeL2(array &$sample)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        $norm2 = 0;
70
        foreach ($sample as $feature) {
71
            $norm2 += $feature * $feature;
72
        }
73
        $norm2 = sqrt($norm2);
74
75
        if (0 == $norm2) {
76
            $sample = array_fill(0, count($sample), 1);
77
        } else {
78
            foreach ($sample as &$feature) {
79
                $feature = $feature / $norm2;
80
            }
81
        }
82
    }
83
}
84