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 |
||
25 | class Indexer implements Location, ListenerRegistry, \PhpParser\NodeVisitor { |
||
26 | /** |
||
27 | * @var Log |
||
28 | */ |
||
29 | protected $log; |
||
30 | |||
31 | /** |
||
32 | * @var Insert |
||
33 | */ |
||
34 | protected $insert; |
||
35 | |||
36 | /** |
||
37 | * @var \PhpParser\Parser |
||
38 | */ |
||
39 | protected $parser; |
||
40 | |||
41 | /** |
||
42 | * @var array string => array() |
||
43 | */ |
||
44 | protected $listeners_enter_definition; |
||
45 | |||
46 | /** |
||
47 | * @var array string => array() |
||
48 | */ |
||
49 | protected $listeners_leave_definition; |
||
50 | |||
51 | /** |
||
52 | * @var array string => array() |
||
53 | */ |
||
54 | protected $listeners_enter_misc; |
||
55 | |||
56 | /** |
||
57 | * @var array string => array() |
||
58 | */ |
||
59 | protected $listeners_leave_misc; |
||
60 | |||
61 | // state for parsing a file |
||
62 | |||
63 | /** |
||
64 | * @var string|null |
||
65 | */ |
||
66 | protected $file_path = null; |
||
67 | |||
68 | /** |
||
69 | * @var string|null |
||
70 | */ |
||
71 | protected $file_content = null; |
||
72 | |||
73 | /** |
||
74 | * This contains the stack of ids were currently in, i.e. the nesting of |
||
75 | * known code blocks we are in. |
||
76 | * |
||
77 | * @var array|null contain ($definition_type, $definition_id) |
||
78 | */ |
||
79 | protected $definition_stack = null; |
||
80 | |||
81 | 10 | public function __construct(Log $log, \PhpParser\Parser $parser, Insert $insert) { |
|
82 | 10 | $this->log = $log; |
|
83 | 10 | $this->parser = $parser; |
|
84 | 10 | $this->insert = $insert; |
|
85 | 10 | $this->listeners_enter_definition = array |
|
86 | 10 | ( 0 => array() |
|
87 | 10 | ); |
|
88 | 10 | $this->listeners_leave_definition = array |
|
89 | 10 | ( 0 => array() |
|
90 | 10 | ); |
|
91 | 10 | $this->listeners_enter_misc = array |
|
92 | 10 | ( 0 => array() |
|
93 | 10 | ); |
|
94 | 10 | $this->listeners_leave_misc = array |
|
95 | 10 | ( 0 => array() |
|
96 | 10 | ); |
|
97 | 10 | } |
|
98 | |||
99 | /** |
||
100 | * Index a directory. |
||
101 | * |
||
102 | * @param string $path |
||
103 | * @param array $ignore_paths |
||
104 | * @return null |
||
105 | */ |
||
106 | 3 | public function index_directory($path, array $ignore_paths) { |
|
107 | 3 | $fc = $this->init_flightcontrol($path); |
|
108 | 3 | $fc->directory("/") |
|
109 | 3 | ->recurseOn() |
|
110 | ->filter(function(FSObject $obj) use (&$ignore_paths) { |
||
111 | 3 | foreach ($ignore_paths as $pattern) { |
|
112 | 3 | if (preg_match("%$pattern%", $obj->path()) !== 0) { |
|
113 | 3 | return false; |
|
114 | } |
||
115 | 3 | } |
|
116 | 3 | return true; |
|
117 | 3 | }) |
|
118 | 3 | ->foldFiles(null, function($_, File $file) use ($path) { |
|
119 | try { |
||
120 | 3 | $this->index_file($path, $file->path()); |
|
121 | } |
||
122 | 3 | catch (\PhpParser\Error $e) { |
|
123 | $this->log->error("in ".$file->path().": ".$e->getMessage()); |
||
124 | } |
||
125 | 3 | }); |
|
126 | |||
127 | 3 | } |
|
128 | |||
129 | /** |
||
130 | * Initialize the filesystem abstraction. |
||
131 | * |
||
132 | * @return Flightcontrol |
||
133 | */ |
||
134 | 3 | public function init_flightcontrol($path) { |
|
139 | |||
140 | /** |
||
141 | * @param string $base_dir |
||
142 | * @param string $path |
||
143 | * @return null |
||
144 | */ |
||
145 | public function index_file($base_dir, $path) { |
||
156 | |||
157 | /** |
||
158 | * @param string $path |
||
159 | * @param string $content |
||
160 | * @return null |
||
161 | */ |
||
162 | 10 | public function index_content($path, $content) { |
|
179 | |||
180 | // from ListenerRegistry |
||
181 | |||
182 | /** |
||
183 | * @inheritdoc |
||
184 | */ |
||
185 | public function on_enter_definition($types, \Closure $listener) { |
||
189 | |||
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | public function on_leave_definition($types, \Closure $listener) { |
||
197 | |||
198 | /** |
||
199 | * @inheritdoc |
||
200 | */ |
||
201 | 10 | public function on_enter_misc($classes, \Closure $listener) { |
|
205 | |||
206 | /** |
||
207 | * @inheritdoc |
||
208 | */ |
||
209 | public function on_leave_misc($classes, \Closure $listener) { |
||
213 | |||
214 | // generalizes over over on_enter/leave_xx |
||
215 | |||
216 | /** |
||
217 | * @param string $what |
||
218 | * @param array|null $things |
||
219 | */ |
||
220 | 10 | protected function on_enter_or_leave_something($what, $things, \Closure $listener) { |
|
235 | |||
236 | // generalizes over calls to misc listeners |
||
237 | |||
238 | /** |
||
239 | * @param string $which |
||
240 | * @param \PhpParser\Node $node |
||
241 | */ |
||
242 | 7 | View Code Duplication | protected function call_misc_listener($which, \PhpParser\Node $node) { |
254 | |||
255 | /** |
||
256 | * @param string $which |
||
257 | * @param string $type |
||
258 | * @param int $type |
||
259 | * @param \PhpParser\Node|null $node |
||
260 | */ |
||
261 | 9 | View Code Duplication | protected function call_definition_listener($which, $type, $id, \PhpParser\Node $node = null) { |
272 | |||
273 | // from Location |
||
274 | |||
275 | /** |
||
276 | * @inheritdoc |
||
277 | */ |
||
278 | 6 | public function file_path() { |
|
281 | |||
282 | /** |
||
283 | * @inheritdoc |
||
284 | */ |
||
285 | 6 | public function in_entities() { |
|
288 | |||
289 | // from \PhpParser\NodeVisitor |
||
290 | |||
291 | /** |
||
292 | * @inheritdoc |
||
293 | */ |
||
294 | 10 | public function beforeTraverse(array $nodes) { |
|
299 | |||
300 | /** |
||
301 | * @inheritdoc |
||
302 | */ |
||
303 | 10 | public function afterTraverse(array $nodes) { |
|
308 | |||
309 | /** |
||
310 | * @inheritdoc |
||
311 | */ |
||
312 | 10 | public function enterNode(\PhpParser\Node $node) { |
|
332 | |||
333 | 9 | protected function get_type_of(\PhpParser\Node $node) { |
|
347 | |||
348 | 10 | protected function is_definition(\PhpParser\Node $node) { |
|
353 | |||
354 | /** |
||
355 | * @inheritdoc |
||
356 | */ |
||
357 | 10 | public function leaveNode(\PhpParser\Node $node) { |
|
367 | } |
||
368 |
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.