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 Indexer 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 Indexer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Indexer implements ListenerRegistry, \PhpParser\NodeVisitor { |
||
27 | /** |
||
28 | * @var Log |
||
29 | */ |
||
30 | protected $log; |
||
31 | |||
32 | /** |
||
33 | * @var Insert |
||
34 | */ |
||
35 | protected $insert; |
||
36 | |||
37 | /** |
||
38 | * @var \PhpParser\Parser |
||
39 | */ |
||
40 | protected $parser; |
||
41 | |||
42 | /** |
||
43 | * @var array string => array() |
||
44 | */ |
||
45 | protected $listeners_enter_definition; |
||
46 | |||
47 | /** |
||
48 | * @var array string => array() |
||
49 | */ |
||
50 | protected $listeners_enter_misc; |
||
51 | |||
52 | /** |
||
53 | * @var LocationImpl|null |
||
54 | */ |
||
55 | protected $location = null; |
||
56 | |||
57 | 52 | public function __construct(Log $log, \PhpParser\Parser $parser, Insert $insert) { |
|
68 | |||
69 | /** |
||
70 | * Index a directory. |
||
71 | * |
||
72 | * @param string $path |
||
73 | * @param array $ignore_paths |
||
74 | * @return null |
||
75 | */ |
||
76 | 7 | public function index_directory($path, array $ignore_paths) { |
|
100 | |||
101 | /** |
||
102 | * Initialize the filesystem abstraction. |
||
103 | * |
||
104 | * @return Flightcontrol |
||
105 | */ |
||
106 | 7 | public function init_flightcontrol($path) { |
|
111 | |||
112 | /** |
||
113 | * @param string $base_dir |
||
114 | * @param string $path |
||
115 | * @return null |
||
116 | */ |
||
117 | public function index_file($base_dir, $path) { |
||
128 | |||
129 | /** |
||
130 | * @param string $path |
||
131 | * @param string $content |
||
132 | * @return null |
||
133 | */ |
||
134 | 52 | public function index_content($path, $content) { |
|
150 | |||
151 | // from ListenerRegistry |
||
152 | |||
153 | /** |
||
154 | * @inheritdoc |
||
155 | */ |
||
156 | public function on_enter_definition($types, \Closure $listener) { |
||
160 | |||
161 | /** |
||
162 | * @inheritdoc |
||
163 | */ |
||
164 | 25 | public function on_enter_misc($classes, \Closure $listener) { |
|
168 | |||
169 | // generalizes over over on_enter |
||
170 | |||
171 | /** |
||
172 | * TODO: Maybe remove this in favour of duplicates. |
||
173 | * |
||
174 | * @param string $what |
||
175 | * @param array|null $things |
||
176 | */ |
||
177 | 25 | protected function on_enter_something($what, $things, \Closure $listener) { |
|
192 | |||
193 | // generalizes over calls to misc listeners |
||
194 | |||
195 | /** |
||
196 | * @param string $which |
||
197 | */ |
||
198 | 37 | View Code Duplication | protected function call_misc_listener($which) { |
211 | |||
212 | /** |
||
213 | * @param string $which |
||
214 | * @param string $type |
||
215 | * @param int $type |
||
216 | */ |
||
217 | 50 | View Code Duplication | protected function call_definition_listener($which, $type, $id) { |
229 | |||
230 | // from \PhpParser\NodeVisitor |
||
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | */ |
||
235 | 52 | public function beforeTraverse(array $nodes) { |
|
239 | |||
240 | /** |
||
241 | * @inheritdoc |
||
242 | */ |
||
243 | 52 | public function afterTraverse(array $nodes) { |
|
245 | |||
246 | /** |
||
247 | * @inheritdoc |
||
248 | */ |
||
249 | 52 | public function enterNode(\PhpParser\Node $node) { |
|
327 | |||
328 | /** |
||
329 | * @inheritdoc |
||
330 | */ |
||
331 | 52 | public function leaveNode(\PhpParser\Node $node) { |
|
341 | } |
||
342 |
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.