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) { |
||
86 | |||
87 | /** |
||
88 | * Returns true if the word is stored in the tree. |
||
89 | * |
||
90 | * @param string $word The word to check if exists. |
||
91 | * @param bool true if is contained. |
||
92 | */ |
||
93 | public function contains($word) : bool { |
||
119 | |||
120 | /** |
||
121 | * |
||
122 | */ |
||
123 | public function delete($word) { |
||
166 | |||
167 | public function clear() { |
||
176 | |||
177 | private function _clear(TrieNode &$node = null) { |
||
186 | |||
187 | public function getWords() : array { |
||
190 | |||
191 | /** |
||
192 | * Checks if there is any word that starts with a concrete prefix. |
||
193 | * |
||
194 | * @param string $prefix The prefix to look up. |
||
195 | * @return true if there are words that start with the especified prefix. |
||
196 | */ |
||
197 | public function startsWith($prefix) : bool { |
||
200 | |||
201 | /** |
||
202 | * |
||
203 | */ |
||
204 | public function withPrefix($prefix) : array { |
||
216 | |||
217 | private function _traverse(TrieNode $node = null, $words = [], $word) { |
||
231 | |||
232 | /** |
||
233 | * Retrieves the node where ends the prefix especified. |
||
234 | * |
||
235 | * @param string $prefix The prefix to look for. |
||
236 | * @return DataStructures\Trees\Nodes\TrieNode|null null if not found. |
||
237 | */ |
||
238 | private function getNodeFromPrefix($prefix) { |
||
257 | |||
258 | /** |
||
259 | * Gets the number of words stored in the tree. |
||
260 | * |
||
261 | * @return int The word count. |
||
262 | */ |
||
263 | public function wordCount() : int { |
||
266 | |||
267 | /** |
||
268 | * Gets the number of prefixes stored in the tree. |
||
269 | * |
||
270 | * @return int The word count. |
||
271 | */ |
||
272 | public function count() : int { |
||
275 | } |
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.