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 |
||
16 | View Code Duplication | class FileService |
|
|
|||
17 | { |
||
18 | private static $instance; |
||
19 | /** |
||
20 | * @var Storage |
||
21 | */ |
||
22 | protected $storage; |
||
23 | |||
24 | /** |
||
25 | * FileService constructor. |
||
26 | */ |
||
27 | protected function __construct() |
||
29 | |||
30 | /** |
||
31 | * @return FileService |
||
32 | */ |
||
33 | public static function getInstance() |
||
40 | |||
41 | /** |
||
42 | * @param $filePath |
||
43 | * @return File |
||
44 | */ |
||
45 | public static function get($filePath) |
||
50 | |||
51 | /** |
||
52 | * @return string |
||
53 | */ |
||
54 | public function __toString() |
||
58 | |||
59 | /** |
||
60 | * @param $filePath |
||
61 | * @return File |
||
62 | */ |
||
63 | protected function getFileByPath($filePath) |
||
68 | |||
69 | /** |
||
70 | * @param Storage $storage |
||
71 | */ |
||
72 | public function init(Storage $storage) |
||
76 | |||
77 | |||
78 | } |
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.