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 | abstract class AbstractMap implements MapInterface |
||
17 | { |
||
18 | /** |
||
19 | * Adds all the mappings from specified map to this map |
||
20 | * |
||
21 | * @param MapInterface $map |
||
22 | * @return void |
||
23 | */ |
||
24 | 1 | public function putAll(MapInterface $map) |
|
31 | |||
32 | /** |
||
33 | * Find the first [@see MapEntry] in map |
||
34 | * |
||
35 | * The closure will get passed a MapEntry. Returning true will end the |
||
36 | * loop and return that MapEntry |
||
37 | * |
||
38 | * @param callable $find |
||
39 | * @return MapEntry|null |
||
40 | */ |
||
41 | 2 | View Code Duplication | public function find(callable $find) |
52 | |||
53 | /** |
||
54 | * Use a closure to determine existence in the map |
||
55 | * |
||
56 | * The closure will get passed a [@see MapEntry]. Returning true from the |
||
57 | * closure will return true from this method. |
||
58 | * |
||
59 | * @param callable $exists |
||
60 | * @return bool |
||
61 | */ |
||
62 | 1 | View Code Duplication | public function exists(callable $exists): bool |
73 | } |
||
74 |
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.