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 | public function setTable($name) |
||
40 | { |
||
41 | try { |
||
42 | return $this->innerGateway->setTable($name); |
||
43 | } catch (DBALException $e) { |
||
44 | throw new \RuntimeException('Database error', 0, $e); |
||
45 | } catch (PDOException $e) { |
||
46 | throw new \RuntimeException('Database error', 0, $e); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Loads list of aliases by given $locationId. |
||
52 | * |
||
53 | * @param mixed $locationId |
||
54 | * @param bool $custom |
||
55 | * @param mixed $languageId |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | public function loadLocationEntries($locationId, $custom = false, $languageId = false) |
||
69 | |||
70 | /** |
||
71 | * Returns boolean indicating if the row with given $id is special root entry. |
||
72 | * |
||
73 | * Special root entry entry will have parentId=0 and text=''. |
||
74 | * In standard installation this entry will point to location with id=2. |
||
75 | * |
||
76 | * @param mixed $id |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function isRootEntry($id) |
||
90 | |||
91 | /** |
||
92 | * Downgrades autogenerated entry matched by given $action and $languageId and negatively matched by |
||
93 | * composite primary key. |
||
94 | * |
||
95 | * If language mask of the found entry is composite (meaning it consists of multiple language ids) given |
||
96 | * $languageId will be removed from mask. Otherwise entry will be marked as history. |
||
97 | * |
||
98 | * @param string $action |
||
99 | * @param mixed $languageId |
||
100 | * @param mixed $newId |
||
101 | * @param mixed $parentId |
||
102 | * @param string $textMD5 |
||
103 | */ |
||
104 | View Code Duplication | public function cleanupAfterPublish($action, $languageId, $newId, $parentId, $textMD5) |
|
114 | |||
115 | public function historizeBeforeSwap($action, $languageMask) |
||
125 | |||
126 | /** |
||
127 | * Marks all entries with given $id as history entries. |
||
128 | * |
||
129 | * This method is used by Handler::locationMoved(). Each row is separately historized |
||
130 | * because future publishing needs to be able to take over history entries safely. |
||
131 | * |
||
132 | * @param mixed $id |
||
133 | * @param mixed $link |
||
134 | */ |
||
135 | public function historizeId($id, $link) |
||
145 | |||
146 | /** |
||
147 | * Updates parent id of autogenerated entries. |
||
148 | * |
||
149 | * Update includes history entries. |
||
150 | * |
||
151 | * @param mixed $oldParentId |
||
152 | * @param mixed $newParentId |
||
153 | */ |
||
154 | public function reparent($oldParentId, $newParentId) |
||
164 | |||
165 | /** |
||
166 | * Updates single row data matched by composite primary key. |
||
167 | * |
||
168 | * Use optional parameter $languageMaskMatch to additionally limit the query match with languages |
||
169 | * |
||
170 | * @param mixed $parentId |
||
171 | * @param string $textMD5 |
||
172 | * @param array $values associative array with column names as keys and column values as values |
||
173 | */ |
||
174 | View Code Duplication | public function updateRow($parentId, $textMD5, array $values) |
|
184 | |||
185 | /** |
||
186 | * Inserts new row in urlalias_ml table. |
||
187 | * |
||
188 | * @param array $values |
||
189 | * |
||
190 | * @return mixed |
||
191 | */ |
||
192 | public function insertRow(array $values) |
||
202 | |||
203 | /** |
||
204 | * Loads single row data matched by composite primary key. |
||
205 | * |
||
206 | * @param mixed $parentId |
||
207 | * @param string $textMD5 |
||
208 | * |
||
209 | * @return array |
||
210 | */ |
||
211 | public function loadRow($parentId, $textMD5) |
||
221 | |||
222 | /** |
||
223 | * Loads autogenerated entry id by given $action and optionally $parentId. |
||
224 | * |
||
225 | * @param string $action |
||
226 | * @param mixed|null $parentId |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | public function loadAutogeneratedEntry($action, $parentId = null) |
||
240 | |||
241 | /** |
||
242 | * Deletes all rows with given $action and optionally $id. |
||
243 | * |
||
244 | * If $id is set only autogenerated entries will be removed. |
||
245 | * |
||
246 | * @param string $action |
||
247 | * @param mixed|null $id |
||
248 | */ |
||
249 | View Code Duplication | public function remove($action, $id = null) |
|
259 | |||
260 | /** |
||
261 | * Loads paged list of global aliases. |
||
262 | * |
||
263 | * @param string|null $languageCode |
||
264 | * @param int $offset |
||
265 | * @param int $limit |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | public function listGlobalEntries($languageCode = null, $offset = 0, $limit = -1) |
||
279 | |||
280 | /** |
||
281 | * Deletes single custom alias row matched by composite primary key. |
||
282 | * |
||
283 | * If $id is set only autogenerated entries will be removed. |
||
284 | * |
||
285 | * @param mixed $parentId |
||
286 | * @param string $textMD5 |
||
287 | * |
||
288 | * @return bool |
||
289 | */ |
||
290 | public function removeCustomAlias($parentId, $textMD5) |
||
300 | |||
301 | /** |
||
302 | * Loads complete URL alias data by given array of path hashes. |
||
303 | * |
||
304 | * @param string[] $urlHashes URL string hashes |
||
305 | * |
||
306 | * @return array |
||
307 | */ |
||
308 | public function loadUrlAliasData(array $urlHashes) |
||
318 | |||
319 | /** |
||
320 | * Loads all data for the path identified by given $id. |
||
321 | * |
||
322 | * @param mixed $id |
||
323 | * |
||
324 | * @return array |
||
325 | */ |
||
326 | public function loadPathData($id) |
||
336 | |||
337 | /** |
||
338 | * Loads path data identified by given ordered array of hierarchy data. |
||
339 | * |
||
340 | * The first entry in $hierarchyData corresponds to the top-most path element in the path, the second entry the |
||
341 | * child of the first path element and so on. |
||
342 | * This method is faster than self::getPath() since it can fetch all elements using only one query, but can be used |
||
343 | * only for autogenerated paths. |
||
344 | * |
||
345 | * @param array $hierarchyData |
||
346 | * |
||
347 | * @return array |
||
348 | */ |
||
349 | public function loadPathDataByHierarchy(array $hierarchyData) |
||
359 | |||
360 | /** |
||
361 | * Loads all autogenerated entries with given $parentId with optionally included history entries. |
||
362 | * |
||
363 | * @param mixed $parentId |
||
364 | * @param bool $includeHistory |
||
365 | * |
||
366 | * @return array |
||
367 | */ |
||
368 | public function loadAutogeneratedEntries($parentId, $includeHistory = false) |
||
378 | |||
379 | /** |
||
380 | * Returns next value for "id" column. |
||
381 | * |
||
382 | * @return mixed |
||
383 | */ |
||
384 | public function getNextId() |
||
394 | |||
395 | public function getLocationContentMainLanguageId($locationId) |
||
405 | } |
||
406 |