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 |
||
13 | class LocationMatcher extends AbstractMatcher |
||
14 | { |
||
15 | const MATCH_CONTENT_ID = 'content_id'; |
||
16 | const MATCH_LOCATION_ID = 'location_id'; |
||
17 | const MATCH_CONTENT_REMOTE_ID = 'content_remote_id'; |
||
18 | const MATCH_LOCATION_REMOTE_ID = 'location_remote_id'; |
||
19 | const MATCH_PARENT_LOCATION_ID = 'parent_location_id'; |
||
20 | const MATCH_PARENT_LOCATION_REMOTE_ID = 'parent_location_remote_id'; |
||
21 | |||
22 | protected $allowedConditions = array( |
||
23 | self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, |
||
24 | self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID |
||
25 | ); |
||
26 | protected $returns = 'Location'; |
||
27 | |||
28 | /** |
||
29 | * @param array $conditions key: condition, value: int / string / int[] / string[] |
||
30 | * @return LocationCollection |
||
31 | */ |
||
32 | public function match(array $conditions) |
||
36 | |||
37 | /** |
||
38 | * @param array $conditions key: condition, value: value: int / string / int[] / string[] |
||
39 | * @return LocationCollection |
||
40 | */ |
||
41 | public function matchLocation(array $conditions) |
||
70 | |||
71 | /** |
||
72 | * Returns all locations of a set of objects |
||
73 | * |
||
74 | * @param int[] $contentIds |
||
75 | * @return Location[] |
||
76 | */ |
||
77 | View Code Duplication | protected function findLocationsByContentIds(array $contentIds) |
|
88 | |||
89 | /** |
||
90 | * Returns all locations of a set of objects |
||
91 | * |
||
92 | * @param int[] $remoteContentIds |
||
93 | * @return Location[] |
||
94 | */ |
||
95 | View Code Duplication | protected function findLocationsByContentRemoteIds(array $remoteContentIds) |
|
106 | |||
107 | /** |
||
108 | * @param int[] $locationIds |
||
109 | * @return Location[] |
||
110 | */ |
||
111 | protected function findLocationsByLocationIds(array $locationIds) |
||
121 | |||
122 | /** |
||
123 | * @param int[] $locationRemoteIds |
||
124 | * @return Location[] |
||
125 | */ |
||
126 | protected function findLocationsByLocationRemoteIds($locationRemoteIds) |
||
136 | |||
137 | /** |
||
138 | * @param int[] $parentLocationIds |
||
139 | * @return Location[] |
||
140 | */ |
||
141 | View Code Duplication | protected function findLocationsByParentLocationIds($parentLocationIds) |
|
157 | |||
158 | /** |
||
159 | * @param int[] $parentLocationRemoteIds |
||
160 | * @return Location[] |
||
161 | */ |
||
162 | View Code Duplication | protected function findLocationsByParentLocationRemoteIds($parentLocationRemoteIds) |
|
173 | } |
||
174 |
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.