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 |
||
9 | class DecisionTreeLeaf |
||
10 | { |
||
11 | /** |
||
12 | * @var string|int |
||
13 | */ |
||
14 | public $value; |
||
15 | |||
16 | /** |
||
17 | * @var float |
||
18 | */ |
||
19 | public $numericValue; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | public $operator; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | public $columnIndex; |
||
30 | |||
31 | /** |
||
32 | * @var DecisionTreeLeaf|null |
||
33 | */ |
||
34 | public $leftLeaf; |
||
35 | |||
36 | /** |
||
37 | * @var DecisionTreeLeaf|null |
||
38 | */ |
||
39 | public $rightLeaf; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | public $records = []; |
||
45 | |||
46 | /** |
||
47 | * Class value represented by the leaf, this value is non-empty |
||
48 | * only for terminal leaves |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | public $classValue = ''; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | public $isTerminal = false; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | public $isContinuous = false; |
||
63 | |||
64 | /** |
||
65 | * @var float |
||
66 | */ |
||
67 | public $giniIndex = 0; |
||
68 | |||
69 | /** |
||
70 | * @var int |
||
71 | */ |
||
72 | public $level = 0; |
||
73 | |||
74 | /** |
||
75 | * HTML representation of the tree without column names |
||
76 | */ |
||
77 | public function __toString(): string |
||
81 | |||
82 | public function evaluate(array $record): bool |
||
92 | |||
93 | /** |
||
94 | * Returns Mean Decrease Impurity (MDI) in the node. |
||
95 | * For terminal nodes, this value is equal to 0 |
||
96 | */ |
||
97 | public function getNodeImpurityDecrease(int $parentRecordCount): float |
||
118 | |||
119 | /** |
||
120 | * Returns HTML representation of the node including children nodes |
||
121 | */ |
||
122 | public function getHTML($columnNames = null): string |
||
165 | } |
||
166 |
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.