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 |
||
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_enter_misc; |
||
50 | |||
51 | // state for parsing a file |
||
52 | |||
53 | /** |
||
54 | * @var string|null |
||
55 | */ |
||
56 | protected $file_path = null; |
||
57 | |||
58 | /** |
||
59 | * @var string|null |
||
60 | */ |
||
61 | protected $file_content = null; |
||
62 | |||
63 | /** |
||
64 | * This contains the stack of ids were currently in, i.e. the nesting of |
||
65 | * known code blocks we are in. |
||
66 | * |
||
67 | * @var array|null contain ($definition_type, $definition_id) |
||
68 | */ |
||
69 | protected $definition_stack = null; |
||
70 | |||
71 | 37 | public function __construct(Log $log, \PhpParser\Parser $parser, Insert $insert) { |
|
82 | |||
83 | /** |
||
84 | * Index a directory. |
||
85 | * |
||
86 | * @param string $path |
||
87 | * @param array $ignore_paths |
||
88 | * @return null |
||
89 | */ |
||
90 | 7 | public function index_directory($path, array $ignore_paths) { |
|
111 | |||
112 | /** |
||
113 | * Initialize the filesystem abstraction. |
||
114 | * |
||
115 | * @return Flightcontrol |
||
116 | */ |
||
117 | 7 | public function init_flightcontrol($path) { |
|
122 | |||
123 | /** |
||
124 | * @param string $base_dir |
||
125 | * @param string $path |
||
126 | * @return null |
||
127 | */ |
||
128 | public function index_file($base_dir, $path) { |
||
139 | |||
140 | /** |
||
141 | * @param string $path |
||
142 | * @param string $content |
||
143 | * @return null |
||
144 | */ |
||
145 | 37 | public function index_content($path, $content) { |
|
162 | |||
163 | // from ListenerRegistry |
||
164 | |||
165 | /** |
||
166 | * @inheritdoc |
||
167 | */ |
||
168 | public function on_enter_definition($types, \Closure $listener) { |
||
172 | |||
173 | /** |
||
174 | * @inheritdoc |
||
175 | */ |
||
176 | 24 | public function on_enter_misc($classes, \Closure $listener) { |
|
180 | |||
181 | // generalizes over over on_enter |
||
182 | |||
183 | /** |
||
184 | * TODO: Maybe remove this in favour of duplicates. |
||
185 | * |
||
186 | * @param string $what |
||
187 | * @param array|null $things |
||
188 | */ |
||
189 | 24 | protected function on_enter_something($what, $things, \Closure $listener) { |
|
204 | |||
205 | // generalizes over calls to misc listeners |
||
206 | |||
207 | /** |
||
208 | * @param string $which |
||
209 | * @param \PhpParser\Node $node |
||
210 | */ |
||
211 | 27 | View Code Duplication | protected function call_misc_listener($which, \PhpParser\Node $node) { |
223 | |||
224 | /** |
||
225 | * @param string $which |
||
226 | * @param string $type |
||
227 | * @param int $type |
||
228 | * @param \PhpParser\Node|null $node |
||
229 | */ |
||
230 | 36 | View Code Duplication | protected function call_definition_listener($which, $type, $id, \PhpParser\Node $node = null) { |
241 | |||
242 | // from Location |
||
243 | |||
244 | /** |
||
245 | * @inheritdoc |
||
246 | */ |
||
247 | 22 | public function file() { |
|
250 | |||
251 | /** |
||
252 | * @inheritdoc |
||
253 | */ |
||
254 | 22 | public function in_entities() { |
|
257 | |||
258 | // from \PhpParser\NodeVisitor |
||
259 | |||
260 | /** |
||
261 | * @inheritdoc |
||
262 | */ |
||
263 | 37 | public function beforeTraverse(array $nodes) { |
|
268 | |||
269 | /** |
||
270 | * @inheritdoc |
||
271 | */ |
||
272 | 37 | public function afterTraverse(array $nodes) { |
|
276 | |||
277 | /** |
||
278 | * @inheritdoc |
||
279 | */ |
||
280 | 37 | public function enterNode(\PhpParser\Node $node) { |
|
339 | |||
340 | /** |
||
341 | * @inheritdoc |
||
342 | */ |
||
343 | 37 | public function leaveNode(\PhpParser\Node $node) { |
|
353 | } |
||
354 |
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.