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 |
||
18 | class ExceptionConversion extends Gateway |
||
19 | { |
||
20 | /** |
||
21 | * The wrapped gateway. |
||
22 | * |
||
23 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway |
||
24 | */ |
||
25 | protected $innerGateway; |
||
26 | |||
27 | /** |
||
28 | * Creates a new exception conversion gateway around $innerGateway. |
||
29 | * |
||
30 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $innerGateway |
||
31 | */ |
||
32 | public function __construct(Gateway $innerGateway) |
||
33 | { |
||
34 | $this->innerGateway = $innerGateway; |
||
35 | } |
||
36 | |||
37 | View Code Duplication | public function setTable($name) |
|
38 | { |
||
39 | try { |
||
40 | return $this->innerGateway->setTable($name); |
||
41 | } catch (DBALException $e) { |
||
42 | throw new \RuntimeException('Database error', 0, $e); |
||
43 | } catch (PDOException $e) { |
||
44 | throw new \RuntimeException('Database error', 0, $e); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Loads list of aliases by given $locationId. |
||
50 | * |
||
51 | * @param mixed $locationId |
||
52 | * @param bool $custom |
||
53 | * @param mixed $languageId |
||
54 | * |
||
55 | * @return array |
||
56 | */ |
||
57 | public function loadLocationEntries($locationId, $custom = false, $languageId = false) |
||
58 | { |
||
59 | try { |
||
60 | return $this->innerGateway->loadLocationEntries($locationId, $custom); |
||
61 | } catch (DBALException $e) { |
||
62 | throw new \RuntimeException('Database error', 0, $e); |
||
63 | } catch (PDOException $e) { |
||
64 | throw new \RuntimeException('Database error', 0, $e); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Returns boolean indicating if the row with given $id is special root entry. |
||
70 | * |
||
71 | * Special root entry entry will have parentId=0 and text=''. |
||
72 | * In standard installation this entry will point to location with id=2. |
||
73 | * |
||
74 | * @param mixed $id |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function isRootEntry($id) |
||
79 | { |
||
80 | try { |
||
81 | return $this->innerGateway->isRootEntry($id); |
||
82 | } catch (DBALException $e) { |
||
83 | throw new \RuntimeException('Database error', 0, $e); |
||
84 | } catch (PDOException $e) { |
||
85 | throw new \RuntimeException('Database error', 0, $e); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Downgrades autogenerated entry matched by given $action and $languageId and negatively matched by |
||
91 | * composite primary key. |
||
92 | * |
||
93 | * If language mask of the found entry is composite (meaning it consists of multiple language ids) given |
||
94 | * $languageId will be removed from mask. Otherwise entry will be marked as history. |
||
95 | * |
||
96 | * @param string $action |
||
97 | * @param mixed $languageId |
||
98 | * @param mixed $newId |
||
99 | * @param mixed $parentId |
||
100 | * @param string $textMD5 |
||
101 | */ |
||
102 | View Code Duplication | public function cleanupAfterPublish($action, $languageId, $newId, $parentId, $textMD5) |
|
103 | { |
||
104 | try { |
||
105 | $this->innerGateway->cleanupAfterPublish($action, $languageId, $newId, $parentId, $textMD5); |
||
106 | } catch (DBALException $e) { |
||
107 | throw new \RuntimeException('Database error', 0, $e); |
||
108 | } catch (PDOException $e) { |
||
109 | throw new \RuntimeException('Database error', 0, $e); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | View Code Duplication | public function historizeBeforeSwap($action, $languageMask) |
|
114 | { |
||
115 | try { |
||
116 | $this->innerGateway->historizeBeforeSwap($action, $languageMask); |
||
117 | } catch (DBALException $e) { |
||
118 | throw new \RuntimeException('Database error', 0, $e); |
||
119 | } catch (PDOException $e) { |
||
120 | throw new \RuntimeException('Database error', 0, $e); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Marks all entries with given $id as history entries. |
||
126 | * |
||
127 | * This method is used by Handler::locationMoved(). Each row is separately historized |
||
128 | * because future publishing needs to be able to take over history entries safely. |
||
129 | * |
||
130 | * @param mixed $id |
||
131 | * @param mixed $link |
||
132 | */ |
||
133 | public function historizeId($id, $link) |
||
143 | |||
144 | /** |
||
145 | * Updates parent id of autogenerated entries. |
||
146 | * |
||
147 | * Update includes history entries. |
||
148 | * |
||
149 | * @param mixed $oldParentId |
||
150 | * @param mixed $newParentId |
||
151 | */ |
||
152 | public function reparent($oldParentId, $newParentId) |
||
162 | |||
163 | /** |
||
164 | * Updates single row data matched by composite primary key. |
||
165 | * |
||
166 | * Use optional parameter $languageMaskMatch to additionally limit the query match with languages |
||
167 | * |
||
168 | * @param mixed $parentId |
||
169 | * @param string $textMD5 |
||
170 | * @param array $values associative array with column names as keys and column values as values |
||
171 | */ |
||
172 | View Code Duplication | public function updateRow($parentId, $textMD5, array $values) |
|
182 | |||
183 | /** |
||
184 | * Inserts new row in urlalias_ml table. |
||
185 | * |
||
186 | * @param array $values |
||
187 | * |
||
188 | * @return mixed |
||
189 | */ |
||
190 | public function insertRow(array $values) |
||
200 | |||
201 | /** |
||
202 | * Loads single row data matched by composite primary key. |
||
203 | * |
||
204 | * @param mixed $parentId |
||
205 | * @param string $textMD5 |
||
206 | * |
||
207 | * @return array |
||
208 | */ |
||
209 | public function loadRow($parentId, $textMD5) |
||
219 | |||
220 | /** |
||
221 | * Loads autogenerated entry id by given $action and optionally $parentId. |
||
222 | * |
||
223 | * @param string $action |
||
224 | * @param mixed|null $parentId |
||
225 | * |
||
226 | * @return array |
||
227 | */ |
||
228 | public function loadAutogeneratedEntry($action, $parentId = null) |
||
238 | |||
239 | /** |
||
240 | * Deletes all rows with given $action and optionally $id. |
||
241 | * |
||
242 | * If $id is set only autogenerated entries will be removed. |
||
243 | * |
||
244 | * @param string $action |
||
245 | * @param mixed|null $id |
||
246 | */ |
||
247 | View Code Duplication | public function remove($action, $id = null) |
|
257 | |||
258 | /** |
||
259 | * Loads paged list of global aliases. |
||
260 | * |
||
261 | * @param string|null $languageCode |
||
262 | * @param int $offset |
||
263 | * @param int $limit |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | public function listGlobalEntries($languageCode = null, $offset = 0, $limit = -1) |
||
277 | |||
278 | /** |
||
279 | * Deletes single custom alias row matched by composite primary key. |
||
280 | * |
||
281 | * If $id is set only autogenerated entries will be removed. |
||
282 | * |
||
283 | * @param mixed $parentId |
||
284 | * @param string $textMD5 |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | public function removeCustomAlias($parentId, $textMD5) |
||
298 | |||
299 | /** |
||
300 | * Loads complete URL alias data by given array of path hashes. |
||
301 | * |
||
302 | * @param string[] $urlHashes URL string hashes |
||
303 | * |
||
304 | * @return array |
||
305 | */ |
||
306 | public function loadUrlAliasData(array $urlHashes) |
||
316 | |||
317 | /** |
||
318 | * Loads all data for the path identified by given $id. |
||
319 | * |
||
320 | * @param mixed $id |
||
321 | * |
||
322 | * @return array |
||
323 | */ |
||
324 | public function loadPathData($id) |
||
334 | |||
335 | /** |
||
336 | * Loads path data identified by given ordered array of hierarchy data. |
||
337 | * |
||
338 | * The first entry in $hierarchyData corresponds to the top-most path element in the path, the second entry the |
||
339 | * child of the first path element and so on. |
||
340 | * This method is faster than self::getPath() since it can fetch all elements using only one query, but can be used |
||
341 | * only for autogenerated paths. |
||
342 | * |
||
343 | * @param array $hierarchyData |
||
344 | * |
||
345 | * @return array |
||
346 | */ |
||
347 | public function loadPathDataByHierarchy(array $hierarchyData) |
||
357 | |||
358 | /** |
||
359 | * Loads all autogenerated entries with given $parentId with optionally included history entries. |
||
360 | * |
||
361 | * @param mixed $parentId |
||
362 | * @param bool $includeHistory |
||
363 | * |
||
364 | * @return array |
||
365 | */ |
||
366 | public function loadAutogeneratedEntries($parentId, $includeHistory = false) |
||
376 | |||
377 | /** |
||
378 | * Returns next value for "id" column. |
||
379 | * |
||
380 | * @return mixed |
||
381 | */ |
||
382 | public function getNextId() |
||
392 | |||
393 | View Code Duplication | public function getLocationContentMainLanguageId($locationId) |
|
394 | { |
||
395 | try { |
||
396 | return $this->innerGateway->getLocationContentMainLanguageId($locationId); |
||
397 | } catch (DBALException $e) { |
||
398 | throw new \RuntimeException('Database error', 0, $e); |
||
403 | } |
||
404 |