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 TraitForSharedMethods 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 TraitForSharedMethods, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | View Code Duplication | trait TraitForSharedMethods |
|
|
|||
31 | { |
||
32 | |||
33 | /** |
||
34 | * |
||
35 | * {@inheritDoc} |
||
36 | * |
||
37 | */ |
||
38 | 490 | protected function setCwd($path = false, $validate_dir_name = false) |
|
56 | |||
57 | /** |
||
58 | * |
||
59 | * {@inheritDoc} |
||
60 | * |
||
61 | */ |
||
62 | 490 | public function getCwd() |
|
69 | |||
70 | /** |
||
71 | * |
||
72 | * {@inheritDoc} |
||
73 | * |
||
74 | */ |
||
75 | 14 | public function getSize($convert = false, $filename = false) |
|
122 | |||
123 | /** |
||
124 | * |
||
125 | * {@inheritDoc} |
||
126 | * |
||
127 | */ |
||
128 | 12 | public function getATime($timeformat = false, $pathname = false) |
|
146 | |||
147 | /** |
||
148 | * |
||
149 | * {@inheritDoc} |
||
150 | * |
||
151 | */ |
||
152 | 12 | public function getCTime($timeformat = false, $pathname = false) |
|
170 | |||
171 | /** |
||
172 | * |
||
173 | * {@inheritDoc} |
||
174 | * |
||
175 | */ |
||
176 | 12 | public function getMTime($timeformat = false, $pathname = false) |
|
194 | |||
195 | /** |
||
196 | * |
||
197 | * {@inheritDoc} |
||
198 | * |
||
199 | */ |
||
200 | 36 | public function ct_ago($timestamp, $date_format = 'y,m,d,h,i,s', $sfx = true, $ommit_zero = true, $ago_single = true, $clocale = []) |
|
264 | |||
265 | /** |
||
266 | * |
||
267 | * {@inheritDoc} |
||
268 | * |
||
269 | */ |
||
270 | 8 | public function openFile($open_mode = "r", $use_include_path = false, $context = null) |
|
278 | |||
279 | /** |
||
280 | * |
||
281 | * {@inheritDoc} |
||
282 | * |
||
283 | */ |
||
284 | 290 | public function setFileClass($class_name = "\HOWI3\libhowi\Filesystem\php5\Objects\FileObject") |
|
295 | |||
296 | /** |
||
297 | * |
||
298 | * {@inheritDoc} |
||
299 | * |
||
300 | */ |
||
301 | 290 | public function setInfoClass($class_name = "\HOWI3\libhowi\Filesystem\php5\Objects\InfoObject") |
|
312 | |||
313 | /** |
||
314 | * |
||
315 | * {@inheritDoc} |
||
316 | * |
||
317 | */ |
||
318 | 8 | public function getOwnerName($filename = false) |
|
325 | |||
326 | /** |
||
327 | * |
||
328 | * {@inheritDoc} |
||
329 | * |
||
330 | */ |
||
331 | 8 | public function getGroupName($filename = false) |
|
338 | |||
339 | /** |
||
340 | * |
||
341 | * {@inheritDoc} |
||
342 | * |
||
343 | */ |
||
344 | 10 | public function getRealpath($filename = false) |
|
349 | |||
350 | /** |
||
351 | * |
||
352 | * {@inheritDoc} |
||
353 | * |
||
354 | */ |
||
355 | 8 | public function getPathInfo($filename = false, $flags = false, |
|
362 | |||
363 | /** |
||
364 | * |
||
365 | * {@inheritDoc} |
||
366 | * |
||
367 | */ |
||
368 | 52 | public function getPath() |
|
373 | |||
374 | /** |
||
375 | * |
||
376 | * {@inheritDoc} |
||
377 | * |
||
378 | */ |
||
379 | 16 | public function getPathname() |
|
383 | |||
384 | /** |
||
385 | * |
||
386 | * {@inheritDoc} |
||
387 | * |
||
388 | */ |
||
389 | 8 | public function getGroup($filename = false) |
|
394 | |||
395 | /** |
||
396 | * |
||
397 | * {@inheritDoc} |
||
398 | * |
||
399 | */ |
||
400 | 8 | public function getOwner($filename = false) |
|
405 | |||
406 | /** |
||
407 | * |
||
408 | * {@inheritDoc} |
||
409 | * |
||
410 | */ |
||
411 | 8 | public function getInode($filename = false) |
|
416 | |||
417 | /** |
||
418 | * |
||
419 | * {@inheritDoc} |
||
420 | * |
||
421 | */ |
||
422 | 12 | public function getBasename($suffix = false, $pathname = false) |
|
429 | |||
430 | /** |
||
431 | * |
||
432 | * {@inheritDoc} |
||
433 | * |
||
434 | */ |
||
435 | 10 | public function getPerms($filename = false) |
|
441 | |||
442 | /** |
||
443 | * |
||
444 | * {@inheritDoc} |
||
445 | * |
||
446 | */ |
||
447 | 12 | public function getType($filename = false) |
|
452 | |||
453 | /** |
||
454 | * |
||
455 | * {@inheritDoc} |
||
456 | * |
||
457 | */ |
||
458 | 20 | public function isDot() |
|
462 | |||
463 | /** |
||
464 | * |
||
465 | * {@inheritDoc} |
||
466 | * |
||
467 | */ |
||
468 | 490 | public function isAbsolute($path = false) |
|
474 | |||
475 | /** |
||
476 | * |
||
477 | * {@inheritDoc} |
||
478 | * |
||
479 | */ |
||
480 | 490 | public function isRelative($path = false) |
|
485 | |||
486 | /** |
||
487 | * |
||
488 | * {@inheritDoc} |
||
489 | * |
||
490 | */ |
||
491 | 490 | public function isWritable($path = false) |
|
496 | |||
497 | /** |
||
498 | * |
||
499 | * {@inheritDoc} |
||
500 | * |
||
501 | */ |
||
502 | 10 | public function isReadable($path = false) |
|
507 | |||
508 | /** |
||
509 | * |
||
510 | * {@inheritDoc} |
||
511 | * |
||
512 | */ |
||
513 | 132 | public function isDir($filename = false) |
|
518 | |||
519 | /** |
||
520 | * |
||
521 | * {@inheritDoc} |
||
522 | * |
||
523 | */ |
||
524 | 8 | public function isExecutable($filename = false) |
|
529 | |||
530 | /** |
||
531 | * |
||
532 | * {@inheritDoc} |
||
533 | * |
||
534 | */ |
||
535 | 12 | public function isFile($filename = false) |
|
540 | |||
541 | /** |
||
542 | * |
||
543 | * {@inheritDoc} |
||
544 | * |
||
545 | */ |
||
546 | 14 | public function isLink($filename = false) |
|
551 | |||
552 | /** |
||
553 | * |
||
554 | * {@inheritDoc} |
||
555 | * |
||
556 | */ |
||
557 | 490 | public function makeAbsolute($path = false) |
|
574 | |||
575 | /** |
||
576 | * |
||
577 | * {@inheritDoc} |
||
578 | * |
||
579 | */ |
||
580 | 202 | public function exists($pathname = false) |
|
589 | |||
590 | /** |
||
591 | * |
||
592 | * {@inheritDoc} |
||
593 | * |
||
594 | */ |
||
595 | 18 | public function getFilename($filename = false) |
|
600 | |||
601 | /** |
||
602 | * |
||
603 | * {@inheritDoc} |
||
604 | * |
||
605 | */ |
||
606 | 8 | public function getExtension($filename = false) |
|
611 | } |
||
612 |
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.