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 BinarySearchTree extends BinaryTreeAbstract { |
||
| 25 | |||
| 26 | public function __construct() { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Inserts data in the correct position. |
||
| 33 | * |
||
| 34 | * @param integer|string $key the key used to store. |
||
| 35 | * @param mixed $data the data to store. |
||
| 36 | * @param bool $update if false the node isn't updated |
||
| 37 | * else if the key matches is updated. |
||
| 38 | */ |
||
| 39 | public function put($key, $data, $update = false) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Creates a new node or updates it if already exists. |
||
| 76 | * |
||
| 77 | * @param int|string $key the key. |
||
| 78 | * @param mixed $data the data to be stored. |
||
| 79 | */ |
||
| 80 | public function putOrUpdate($key, $data) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Deletes the node with the maximum key and returns it. The most right and more bottom. |
||
| 86 | * |
||
| 87 | * @param DataStructures\Trees\Nodes\BSTNode|null if null takes the root. |
||
| 88 | * @return DataStructures\Trees\Nodes\BSTNode|null the maximum node or |
||
| 89 | * null if the tree is empty. |
||
| 90 | */ |
||
| 91 | public function delete($key) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Deletes the selected node if is not null and returns the node |
||
| 103 | * that replaces the deleted node. Also decrease the size of tree. |
||
| 104 | * |
||
| 105 | * @param DataStructures\Trees\Nodes\BSTNode|null The node to be deleted. |
||
| 106 | * @return the node that replaces the deleted. |
||
| 107 | */ |
||
| 108 | protected function _delete(BinaryNodeInterface &$node) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Traverse in preorder. This is: first visit the root, then |
||
| 138 | * the left subtree and finally the right subtree. |
||
| 139 | * |
||
| 140 | * @param Callable|null $callback the callback function to apply to each |
||
| 141 | * node. |
||
| 142 | */ |
||
| 143 | public function preorder(Callable $callback = null) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Private preorder traverse method that is recursive and is called by |
||
| 149 | * the public preorder method. |
||
| 150 | * |
||
| 151 | * @param DataStructures\Trees\Nodes\BSTNode|null $node. |
||
| 152 | * @param Callable|null $callback the callback function to apply to each |
||
| 153 | * node. |
||
| 154 | */ |
||
| 155 | View Code Duplication | private function _preorder($node, Callable $callback = null) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Traverse in inorder. This is: first visit the left subtree, |
||
| 168 | * then the root and finally the right subtree. |
||
| 169 | * |
||
| 170 | * @param Callable|null $callback the callback function to apply to each |
||
| 171 | * node. |
||
| 172 | */ |
||
| 173 | public function inorder(Callable $callback = null) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Private inorder traverse method that is recursive and is called by |
||
| 179 | * the public inorder method. |
||
| 180 | * |
||
| 181 | * @param DataStructures\Trees\Nodes\BSTNode|null $node. |
||
| 182 | * @param Callable|null $callback the callback function to apply to each |
||
| 183 | * node. |
||
| 184 | */ |
||
| 185 | View Code Duplication | private function _inorder($node, Callable $callback = null) { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Traverse in postorder. This is: first visit the left subtree, |
||
| 199 | * then the right subtree and finally the root. |
||
| 200 | * |
||
| 201 | * @param Callable|null $callback the callback function to apply to each |
||
| 202 | * node. |
||
| 203 | */ |
||
| 204 | public function postorder(Callable $callback = null) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Private postorder traverse method that is recursive and is called by |
||
| 210 | * the public postorder method. |
||
| 211 | * |
||
| 212 | * @param DataStructures\Trees\Nodes\BSTNode|null $node. |
||
| 213 | * @param Callable|null $callback the callback function to apply to each |
||
| 214 | * node. |
||
| 215 | */ |
||
| 216 | View Code Duplication | private function _postorder($node, Callable $callback = null) { |
|
| 226 | } |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: