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 | /** |
||
| 105 | * Marks all entries with given $id as history entries. |
||
| 106 | * |
||
| 107 | * This method is used by Handler::locationMoved(). For this reason rows are not updated with next id value as |
||
| 108 | * all entries with given id are being marked as history and there is no need for id separation. |
||
| 109 | * Thus only "link" and "is_original" columns are updated. |
||
| 110 | * |
||
| 111 | * @param mixed $id |
||
| 112 | * @param mixed $link |
||
| 113 | */ |
||
| 114 | public function historizeId($id, $link) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Updates parent id of autogenerated entries. |
||
| 127 | * |
||
| 128 | * Update includes history entries. |
||
| 129 | * |
||
| 130 | * @param mixed $oldParentId |
||
| 131 | * @param mixed $newParentId |
||
| 132 | */ |
||
| 133 | public function reparent($oldParentId, $newParentId) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Updates single row data matched by composite primary key. |
||
| 146 | * |
||
| 147 | * Use optional parameter $languageMaskMatch to additionally limit the query match with languages |
||
| 148 | * |
||
| 149 | * @param mixed $parentId |
||
| 150 | * @param string $textMD5 |
||
| 151 | * @param array $values associative array with column names as keys and column values as values |
||
| 152 | */ |
||
| 153 | View Code Duplication | public function updateRow($parentId, $textMD5, array $values) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Inserts new row in urlalias_ml table. |
||
| 166 | * |
||
| 167 | * @param array $values |
||
| 168 | * |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | public function insertRow(array $values) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Loads single row data matched by composite primary key. |
||
| 184 | * |
||
| 185 | * @param mixed $parentId |
||
| 186 | * @param string $textMD5 |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public function loadRow($parentId, $textMD5) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Loads autogenerated entry id by given $action and optionally $parentId. |
||
| 203 | * |
||
| 204 | * @param string $action |
||
| 205 | * @param mixed|null $parentId |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | public function loadAutogeneratedEntry($action, $parentId = null) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Deletes all rows with given $action and optionally $id. |
||
| 222 | * |
||
| 223 | * If $id is set only autogenerated entries will be removed. |
||
| 224 | * |
||
| 225 | * @param string $action |
||
| 226 | * @param mixed|null $id |
||
| 227 | */ |
||
| 228 | View Code Duplication | public function remove($action, $id = null) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Loads paged list of global aliases. |
||
| 241 | * |
||
| 242 | * @param string|null $languageCode |
||
| 243 | * @param int $offset |
||
| 244 | * @param int $limit |
||
| 245 | * |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | public function listGlobalEntries($languageCode = null, $offset = 0, $limit = -1) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Deletes single custom alias row matched by composite primary key. |
||
| 261 | * |
||
| 262 | * If $id is set only autogenerated entries will be removed. |
||
| 263 | * |
||
| 264 | * @param mixed $parentId |
||
| 265 | * @param string $textMD5 |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function removeCustomAlias($parentId, $textMD5) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Loads complete URL alias data by given array of path hashes. |
||
| 282 | * |
||
| 283 | * @param string[] $urlHashes URL string hashes |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | public function loadUrlAliasData(array $urlHashes) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Loads all data for the path identified by given $id. |
||
| 300 | * |
||
| 301 | * @param mixed $id |
||
| 302 | * |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | public function loadPathData($id) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Loads path data identified by given ordered array of hierarchy data. |
||
| 318 | * |
||
| 319 | * The first entry in $hierarchyData corresponds to the top-most path element in the path, the second entry the |
||
| 320 | * child of the first path element and so on. |
||
| 321 | * This method is faster than self::getPath() since it can fetch all elements using only one query, but can be used |
||
| 322 | * only for autogenerated paths. |
||
| 323 | * |
||
| 324 | * @param array $hierarchyData |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | public function loadPathDataByHierarchy(array $hierarchyData) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Loads all autogenerated entries with given $parentId with optionally included history entries. |
||
| 341 | * |
||
| 342 | * @param mixed $parentId |
||
| 343 | * @param bool $includeHistory |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | public function loadAutogeneratedEntries($parentId, $includeHistory = false) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Returns next value for "id" column. |
||
| 360 | * |
||
| 361 | * @return mixed |
||
| 362 | */ |
||
| 363 | public function getNextId() |
||
| 373 | } |
||
| 374 |