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:
Complex classes like TrieTree often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TrieTree, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class TrieTree implements Countable { |
||
24 | private $root; |
||
25 | private $numWords; |
||
26 | private $size; |
||
27 | |||
28 | public function __construct() { |
||
33 | |||
34 | /** |
||
35 | * Returns true if the tree is empty. Number of prefixes is 0. |
||
36 | * |
||
37 | * @return bool true if empty. |
||
38 | */ |
||
39 | public function empty() : bool { |
||
42 | |||
43 | /** |
||
44 | * Returns the number of prefixes that are contained in the trie. |
||
45 | * |
||
46 | * @return int the num of prefixes. |
||
47 | */ |
||
48 | public function size() : int { |
||
51 | |||
52 | /** |
||
53 | * Inserts a new word in the tree. If the word exists it doesn't do nothing. |
||
54 | * |
||
55 | * @param string $word the word to be added. |
||
56 | */ |
||
57 | public function add($word) { |
||
87 | |||
88 | /** |
||
89 | * Returns true if the word is stored in the tree. |
||
90 | * |
||
91 | * @param string $word The word to check if exists. |
||
92 | * @param bool true if is contained. |
||
93 | */ |
||
94 | public function contains($word) : bool { |
||
120 | |||
121 | /** |
||
122 | * Removes a word from the tree. |
||
123 | * |
||
124 | * @param string $word the word to delete if it is contained in the trie. |
||
125 | */ |
||
126 | public function delete($word) { |
||
169 | |||
170 | /** |
||
171 | * Removes all nodes (and words) in the tree and resets the size |
||
172 | * and word count. |
||
173 | */ |
||
174 | public function clear() { |
||
183 | |||
184 | /** |
||
185 | * Recursive clear that removes all nodes, included leaf, except |
||
186 | * the references to the first children. |
||
187 | * |
||
188 | * @param DataStructures\Trees\Nodes\TrieNode|null the node to traverse. |
||
189 | * @return DataStructures\Trees\Nodes\TrieNode|null the next node to delete. |
||
190 | */ |
||
191 | private function _clear(TrieNode &$node = null) { |
||
199 | |||
200 | /** |
||
201 | * Returns an array containing all words stored in the tree. |
||
202 | * It starts to search from the root that contains an empty string |
||
203 | * using the withPrefix method. |
||
204 | * |
||
205 | * @return array an empty array if there are not words in the tree. |
||
206 | */ |
||
207 | public function getWords() : array { |
||
214 | |||
215 | /** |
||
216 | * Checks if there is any word that starts with a concrete prefix. |
||
217 | * |
||
218 | * @param string $prefix The prefix to look up. |
||
219 | * @return true if there are words that start with the especified prefix. |
||
220 | */ |
||
221 | public function startsWith($prefix) : bool { |
||
224 | |||
225 | /** |
||
226 | * Returns an array with all words that has the especified prefix. |
||
227 | * For example, with prefix 'he' it will retrieve: hell, hello, .... |
||
228 | * |
||
229 | * @param string $prefix The prefix that must have all words. |
||
230 | * @return array All words that contains the prefix. |
||
231 | */ |
||
232 | public function withPrefix($prefix) : array { |
||
246 | |||
247 | /** |
||
248 | * |
||
249 | */ |
||
250 | private function _traverseWithPrefix(TrieNode $node = null, $words = [], $word) { |
||
265 | |||
266 | /** |
||
267 | * Retrieves the node where ends the prefix especified. |
||
268 | * |
||
269 | * @param string $prefix The prefix to look for. |
||
270 | * @return DataStructures\Trees\Nodes\TrieNode|null null if not found. |
||
271 | */ |
||
272 | private function getNodeFromPrefix($prefix) { |
||
291 | |||
292 | /** |
||
293 | * Gets the number of words stored in the tree. |
||
294 | * |
||
295 | * @return int The word count. |
||
296 | */ |
||
297 | public function wordCount() : int { |
||
300 | |||
301 | /** |
||
302 | * Gets the number of prefixes stored in the tree. |
||
303 | * |
||
304 | * @return int The word count. |
||
305 | */ |
||
306 | public function count() : int { |
||
309 | } |
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.