Test Setup Failed
Pull Request — master (#375)
by Arkadiusz
02:14
created

DecisionNode   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 8.08 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 2
dl 8
loc 99
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 3
A column() 0 4 1
A value() 0 4 1
A groups() 0 4 1
A impurity() 0 4 1
A samplesCount() 0 4 1
A purityIncrease() 8 16 3
A cleanup() 0 4 1

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\Tree\Node;
6
7
use Phpml\Exception\InvalidArgumentException;
8
9
class DecisionNode extends BinaryNode implements PurityNode
10
{
11
    /**
12
     * @var int
13
     */
14
    private $column;
15
16
    /**
17
     * @var mixed
18
     */
19
    private $value;
20
21
    /**
22
     * @var array
23
     */
24
    private $groups = [];
25
26
    /**
27
     * @var float
28
     */
29
    private $impurity;
30
31
    /**
32
     * @var int
33
     */
34
    private $samplesCount;
35
36
    /**
37
     * @param mixed $value
38
     */
39
    public function __construct(int $column, $value, array $groups, float $impurity)
40
    {
41
        if (count($groups) !== 2) {
42
            throw new InvalidArgumentException('The number of groups must be exactly two.');
43
        }
44
45
        if ($impurity < 0.) {
46
            throw new InvalidArgumentException('Impurity cannot be less than 0.');
47
        }
48
49
        $this->column = $column;
50
        $this->value = $value;
51
        $this->groups = $groups;
52
        $this->impurity = $impurity;
53
        $this->samplesCount = (int) array_sum(array_map(function (array $group) {
54
            return count($group[0]);
55
        }, $groups));
56
    }
57
58
    public function column(): int
59
    {
60
        return $this->column;
61
    }
62
63
    /**
64
     * @return mixed
65
     */
66
    public function value()
67
    {
68
        return $this->value;
69
    }
70
71
    public function groups(): array
72
    {
73
        return $this->groups;
74
    }
75
76
    public function impurity(): float
77
    {
78
        return $this->impurity;
79
    }
80
81
    public function samplesCount(): int
82
    {
83
        return $this->samplesCount;
84
    }
85
86
    public function purityIncrease(): float
87
    {
88
        $impurity = $this->impurity;
89
90 View Code Duplication
        if ($this->left() instanceof PurityNode) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91
            $impurity -= $this->left()->impurity()
92
                * ($this->left()->samplesCount() / $this->samplesCount);
93
        }
94
95 View Code Duplication
        if ($this->right() instanceof PurityNode) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96
            $impurity -= $this->right()->impurity()
97
                * ($this->right()->samplesCount() / $this->samplesCount);
98
        }
99
100
        return $impurity;
101
    }
102
103
    public function cleanup(): void
104
    {
105
        $this->groups = [[], []];
106
    }
107
}
108