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:
Complex classes like ExceptionConversion often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExceptionConversion, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ExceptionConversion extends Gateway |
||
21 | { |
||
22 | /** |
||
23 | * The wrapped gateway. |
||
24 | * |
||
25 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway |
||
26 | */ |
||
27 | protected $innerGateway; |
||
28 | |||
29 | /** |
||
30 | * Creates a new exception conversion gateway around $innerGateway. |
||
31 | * |
||
32 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $innerGateway |
||
33 | */ |
||
34 | public function __construct(Gateway $innerGateway) |
||
38 | |||
39 | /** |
||
40 | * Loads list of aliases by given $locationId. |
||
41 | * |
||
42 | * @param mixed $locationId |
||
43 | * @param bool $custom |
||
44 | * @param mixed $languageId |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | public function loadLocationEntries($locationId, $custom = false, $languageId = false) |
||
58 | |||
59 | /** |
||
60 | * Returns boolean indicating if the row with given $id is special root entry. |
||
61 | * |
||
62 | * Special root entry entry will have parentId=0 and text=''. |
||
63 | * In standard installation this entry will point to location with id=2. |
||
64 | * |
||
65 | * @param mixed $id |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function isRootEntry($id) |
||
79 | |||
80 | /** |
||
81 | * Downgrades autogenerated entry matched by given $action and $languageId and negatively matched by |
||
82 | * composite primary key. |
||
83 | * |
||
84 | * If language mask of the found entry is composite (meaning it consists of multiple language ids) given |
||
85 | * $languageId will be removed from mask. Otherwise entry will be marked as history. |
||
86 | * |
||
87 | * @param string $action |
||
88 | * @param mixed $languageId |
||
89 | * @param mixed $newId |
||
90 | * @param mixed $parentId |
||
91 | * @param string $textMD5 |
||
92 | */ |
||
93 | View Code Duplication | public function cleanupAfterPublish($action, $languageId, $newId, $parentId, $textMD5) |
|
103 | |||
104 | public function historizeBeforeSwap($action, $languageMask) |
||
105 | { |
||
106 | try { |
||
107 | $this->innerGateway->historizeBeforeSwap($action, $languageMask); |
||
108 | } catch (DBALException $e) { |
||
109 | throw new \RuntimeException('Database error', 0, $e); |
||
110 | } catch (PDOException $e) { |
||
111 | throw new \RuntimeException('Database error', 0, $e); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Marks all entries with given $id as history entries. |
||
117 | * |
||
118 | * This method is used by Handler::locationMoved(). For this reason rows are not updated with next id value as |
||
119 | * all entries with given id are being marked as history and there is no need for id separation. |
||
120 | * Thus only "link" and "is_original" columns are updated. |
||
121 | * |
||
122 | * @param mixed $id |
||
123 | * @param mixed $link |
||
124 | */ |
||
125 | public function historizeId($id, $link) |
||
135 | |||
136 | /** |
||
137 | * Updates parent id of autogenerated entries. |
||
138 | * |
||
139 | * Update includes history entries. |
||
140 | * |
||
141 | * @param mixed $oldParentId |
||
142 | * @param mixed $newParentId |
||
143 | */ |
||
144 | public function reparent($oldParentId, $newParentId) |
||
154 | |||
155 | /** |
||
156 | * Updates single row data matched by composite primary key. |
||
157 | * |
||
158 | * Use optional parameter $languageMaskMatch to additionally limit the query match with languages |
||
159 | * |
||
160 | * @param mixed $parentId |
||
161 | * @param string $textMD5 |
||
162 | * @param array $values associative array with column names as keys and column values as values |
||
163 | */ |
||
164 | View Code Duplication | public function updateRow($parentId, $textMD5, array $values) |
|
174 | |||
175 | /** |
||
176 | * Inserts new row in urlalias_ml table. |
||
177 | * |
||
178 | * @param array $values |
||
179 | * |
||
180 | * @return mixed |
||
181 | */ |
||
182 | public function insertRow(array $values) |
||
192 | |||
193 | /** |
||
194 | * Loads single row data matched by composite primary key. |
||
195 | * |
||
196 | * @param mixed $parentId |
||
197 | * @param string $textMD5 |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | public function loadRow($parentId, $textMD5) |
||
211 | |||
212 | /** |
||
213 | * Loads autogenerated entry id by given $action and optionally $parentId. |
||
214 | * |
||
215 | * @param string $action |
||
216 | * @param mixed|null $parentId |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | public function loadAutogeneratedEntry($action, $parentId = null) |
||
230 | |||
231 | /** |
||
232 | * Deletes all rows with given $action and optionally $id. |
||
233 | * |
||
234 | * If $id is set only autogenerated entries will be removed. |
||
235 | * |
||
236 | * @param string $action |
||
237 | * @param mixed|null $id |
||
238 | */ |
||
239 | View Code Duplication | public function remove($action, $id = null) |
|
249 | |||
250 | /** |
||
251 | * Loads paged list of global aliases. |
||
252 | * |
||
253 | * @param string|null $languageCode |
||
254 | * @param int $offset |
||
255 | * @param int $limit |
||
256 | * |
||
257 | * @return array |
||
258 | */ |
||
259 | public function listGlobalEntries($languageCode = null, $offset = 0, $limit = -1) |
||
269 | |||
270 | /** |
||
271 | * Deletes single custom alias row matched by composite primary key. |
||
272 | * |
||
273 | * If $id is set only autogenerated entries will be removed. |
||
274 | * |
||
275 | * @param mixed $parentId |
||
276 | * @param string $textMD5 |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | public function removeCustomAlias($parentId, $textMD5) |
||
290 | |||
291 | /** |
||
292 | * Loads complete URL alias data by given array of path hashes. |
||
293 | * |
||
294 | * @param string[] $urlHashes URL string hashes |
||
295 | * |
||
296 | * @return array |
||
297 | */ |
||
298 | public function loadUrlAliasData(array $urlHashes) |
||
308 | |||
309 | /** |
||
310 | * Loads all data for the path identified by given $id. |
||
311 | * |
||
312 | * @param mixed $id |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | public function loadPathData($id) |
||
326 | |||
327 | /** |
||
328 | * Loads path data identified by given ordered array of hierarchy data. |
||
329 | * |
||
330 | * The first entry in $hierarchyData corresponds to the top-most path element in the path, the second entry the |
||
331 | * child of the first path element and so on. |
||
332 | * This method is faster than self::getPath() since it can fetch all elements using only one query, but can be used |
||
333 | * only for autogenerated paths. |
||
334 | * |
||
335 | * @param array $hierarchyData |
||
336 | * |
||
337 | * @return array |
||
338 | */ |
||
339 | public function loadPathDataByHierarchy(array $hierarchyData) |
||
349 | |||
350 | /** |
||
351 | * Loads all autogenerated entries with given $parentId with optionally included history entries. |
||
352 | * |
||
353 | * @param mixed $parentId |
||
354 | * @param bool $includeHistory |
||
355 | * |
||
356 | * @return array |
||
357 | */ |
||
358 | public function loadAutogeneratedEntries($parentId, $includeHistory = false) |
||
368 | |||
369 | /** |
||
370 | * Returns next value for "id" column. |
||
371 | * |
||
372 | * @return mixed |
||
373 | */ |
||
374 | public function getNextId() |
||
384 | } |
||
385 |