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 FileLoader extends AbstractLoader |
||
24 | { |
||
25 | /** |
||
26 | * File Loader Default settings |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | public static $defaults = [ |
||
31 | 'vars' => [] |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * File Loader Options |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $options; |
||
40 | |||
41 | /** |
||
42 | * FileLoader constructor. |
||
43 | * |
||
44 | * @param RouterInterface $router Routing Component |
||
45 | * @param array $options File Loader Options. |
||
46 | */ |
||
47 | 7 | public function __construct(RouterInterface $router, array $options = []) |
|
52 | |||
53 | /** |
||
54 | * @inheritdoc |
||
55 | */ |
||
56 | 3 | View Code Duplication | public function loadFiles(array $files) |
66 | |||
67 | /** |
||
68 | * @inheritdoc |
||
69 | */ |
||
70 | 6 | public function loadFile(string $file) |
|
93 | } |
||
94 |
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.