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 |
||
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 | public function delete($word) { |
||
146 | |||
147 | public function getWords() : array { |
||
150 | |||
151 | /** |
||
152 | * Checks if there is any word that starts with a concrete prefix. |
||
153 | * |
||
154 | * @param string $prefix The prefix to look up. |
||
155 | * @return true if there are words that start with the especified prefix. |
||
156 | */ |
||
157 | public function startsWith($prefix) : bool { |
||
160 | |||
161 | /** |
||
162 | * |
||
163 | */ |
||
164 | public function withPrefix($prefix) : array { |
||
176 | |||
177 | private function _traverse(TrieNode $node = null, $words = [], $word) { |
||
192 | |||
193 | /** |
||
194 | * Retrieves the node where ends the prefix especified. |
||
195 | * |
||
196 | * @param string $prefix The prefix to look for. |
||
197 | * @return DataStructures\Trees\Nodes\TrieNode|null null if not found. |
||
198 | */ |
||
199 | private function getNodeFromPrefix($prefix) { |
||
218 | |||
219 | /** |
||
220 | * Gets the number of words stored in the tree. |
||
221 | * |
||
222 | * @return int The word count. |
||
223 | */ |
||
224 | public function wordCount() : int { |
||
227 | |||
228 | /** |
||
229 | * Gets the number of prefixes stored in the tree. |
||
230 | * |
||
231 | * @return int The word count. |
||
232 | */ |
||
233 | public function count() : int { |
||
236 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.