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 |
||
10 | class ModelManager |
||
11 | { |
||
12 | /** |
||
13 | * @param Estimator $estimator |
||
14 | * @param string $filepath |
||
15 | * |
||
16 | * @return void |
||
17 | * |
||
18 | * @throws FileException |
||
19 | * @throws SerializeException |
||
20 | */ |
||
21 | public function saveToFile(Estimator $estimator, string $filepath): void |
||
37 | |||
38 | /** |
||
39 | * @param string $filePath |
||
40 | * |
||
41 | * @return Estimator |
||
42 | * |
||
43 | * @throws FileException |
||
44 | * @throws SerializeException |
||
45 | */ |
||
46 | public function restoreFromFile(string $filePath): Estimator |
||
59 | |||
60 | /** |
||
61 | * @param string $filePath |
||
62 | * |
||
63 | * @return void |
||
64 | * |
||
65 | * @throws FileException |
||
66 | */ |
||
67 | public function reset(string $filePath): void |
||
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.