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 |
||
24 | class AVLTree extends BinaryTreeAbstract { |
||
25 | |||
26 | /** |
||
27 | * Creates a AVLNode. |
||
28 | * |
||
29 | * @param int|string $key the key used to store. |
||
30 | * @param mixed $data the data. |
||
31 | * @param DataStructures\Trees\Nodes\AVLNode|null $parent the parent node. |
||
32 | * @param DataStructures\Trees\Nodes\AVLNode|null $left the left child node. |
||
33 | * @param DataStructures\Trees\Nodes\AVLNode|null $right the right child node. |
||
34 | * |
||
35 | * @return DataStructures\Trees\Nodes\AVLNode the new node created. |
||
36 | */ |
||
37 | public function createNode($key, $data, $parent = null, $left = null, $right = null) { |
||
40 | |||
41 | /** |
||
42 | * Does a right rotation. |
||
43 | * Example, rotate Y |
||
44 | * k2 k1 |
||
45 | * / \ / \ |
||
46 | * k1 Z ==> X k2 |
||
47 | * / \ / \ |
||
48 | *X Y Y Z |
||
49 | * |
||
50 | */ |
||
51 | private function rightRotation(AVLNode $node) { |
||
54 | |||
55 | /* Does a right rotation. |
||
56 | * k2 k1 |
||
57 | * / \ / \ |
||
58 | * X k1 ==> k2 Z |
||
59 | * / \ / \ |
||
60 | * Y Z X Y |
||
61 | */ |
||
62 | private function leftRotation(AVLNode $node) { |
||
84 | |||
85 | /** |
||
86 | * Double right rotation does first a left rotation of right child node |
||
87 | * that detects the imbalance and finally does a right rotation |
||
88 | * in the subtree root that detects the imbalance. |
||
89 | * Case Right-Left. |
||
90 | */ |
||
91 | private function doubleRightRotation(AVLNode $node) { |
||
95 | |||
96 | /** |
||
97 | * Double left rotation does first a right rotation of left child node |
||
98 | * that detects the imbalance and finally does a left rotation |
||
99 | * in the subtree root that detects the imbalance. |
||
100 | * Case Left-Right. |
||
101 | */ |
||
102 | private function doubleLeftRotation(AVLNode $node) { |
||
106 | |||
107 | public function put($key, $data, $update = false) { |
||
110 | |||
111 | private function adjustHeight(AVLNode $node) { |
||
114 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.