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  | 
            ||
| 19 | class ExceptionConversion extends Gateway  | 
            ||
| 20 | { | 
            ||
| 21 | /**  | 
            ||
| 22 | * The wrapped gateway.  | 
            ||
| 23 | *  | 
            ||
| 24 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway  | 
            ||
| 25 | */  | 
            ||
| 26 | protected $innerGateway;  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * Creates a new exception conversion gateway around $innerGateway.  | 
            ||
| 30 | *  | 
            ||
| 31 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $innerGateway  | 
            ||
| 32 | */  | 
            ||
| 33 | public function __construct(Gateway $innerGateway)  | 
            ||
| 37 | |||
| 38 | View Code Duplication | public function setTable($name)  | 
            |
| 48 | |||
| 49 | /**  | 
            ||
| 50 | * Loads list of aliases by given $locationId.  | 
            ||
| 51 | *  | 
            ||
| 52 | * @param mixed $locationId  | 
            ||
| 53 | * @param bool $custom  | 
            ||
| 54 | * @param mixed $languageId  | 
            ||
| 55 | *  | 
            ||
| 56 | * @return array  | 
            ||
| 57 | */  | 
            ||
| 58 | public function loadLocationEntries($locationId, $custom = false, $languageId = false)  | 
            ||
| 68 | |||
| 69 | /**  | 
            ||
| 70 | * Returns boolean indicating if the row with given $id is special root entry.  | 
            ||
| 71 | *  | 
            ||
| 72 | * Special root entry entry will have parentId=0 and text=''.  | 
            ||
| 73 | * In standard installation this entry will point to location with id=2.  | 
            ||
| 74 | *  | 
            ||
| 75 | * @param mixed $id  | 
            ||
| 76 | *  | 
            ||
| 77 | * @return bool  | 
            ||
| 78 | */  | 
            ||
| 79 | public function isRootEntry($id)  | 
            ||
| 89 | |||
| 90 | /**  | 
            ||
| 91 | * Downgrades autogenerated entry matched by given $action and $languageId and negatively matched by  | 
            ||
| 92 | * composite primary key.  | 
            ||
| 93 | *  | 
            ||
| 94 | * If language mask of the found entry is composite (meaning it consists of multiple language ids) given  | 
            ||
| 95 | * $languageId will be removed from mask. Otherwise entry will be marked as history.  | 
            ||
| 96 | *  | 
            ||
| 97 | * @param string $action  | 
            ||
| 98 | * @param mixed $languageId  | 
            ||
| 99 | * @param mixed $newId  | 
            ||
| 100 | * @param mixed $parentId  | 
            ||
| 101 | * @param string $textMD5  | 
            ||
| 102 | */  | 
            ||
| 103 | View Code Duplication | public function cleanupAfterPublish($action, $languageId, $newId, $parentId, $textMD5)  | 
            |
| 113 | |||
| 114 | View Code Duplication | public function historizeBeforeSwap($action, $languageMask)  | 
            |
| 124 | |||
| 125 | /**  | 
            ||
| 126 | * Marks all entries with given $id as history entries.  | 
            ||
| 127 | *  | 
            ||
| 128 | * This method is used by Handler::locationMoved(). Each row is separately historized  | 
            ||
| 129 | * because future publishing needs to be able to take over history entries safely.  | 
            ||
| 130 | *  | 
            ||
| 131 | * @param mixed $id  | 
            ||
| 132 | * @param mixed $link  | 
            ||
| 133 | */  | 
            ||
| 134 | public function historizeId($id, $link)  | 
            ||
| 144 | |||
| 145 | /**  | 
            ||
| 146 | * Updates parent id of autogenerated entries.  | 
            ||
| 147 | *  | 
            ||
| 148 | * Update includes history entries.  | 
            ||
| 149 | *  | 
            ||
| 150 | * @param mixed $oldParentId  | 
            ||
| 151 | * @param mixed $newParentId  | 
            ||
| 152 | */  | 
            ||
| 153 | public function reparent($oldParentId, $newParentId)  | 
            ||
| 163 | |||
| 164 | /**  | 
            ||
| 165 | * Updates single row data matched by composite primary key.  | 
            ||
| 166 | *  | 
            ||
| 167 | * Use optional parameter $languageMaskMatch to additionally limit the query match with languages  | 
            ||
| 168 | *  | 
            ||
| 169 | * @param mixed $parentId  | 
            ||
| 170 | * @param string $textMD5  | 
            ||
| 171 | * @param array $values associative array with column names as keys and column values as values  | 
            ||
| 172 | */  | 
            ||
| 173 | View Code Duplication | public function updateRow($parentId, $textMD5, array $values)  | 
            |
| 183 | |||
| 184 | /**  | 
            ||
| 185 | * Inserts new row in urlalias_ml table.  | 
            ||
| 186 | *  | 
            ||
| 187 | * @param array $values  | 
            ||
| 188 | *  | 
            ||
| 189 | * @return mixed  | 
            ||
| 190 | */  | 
            ||
| 191 | public function insertRow(array $values)  | 
            ||
| 201 | |||
| 202 | /**  | 
            ||
| 203 | * Loads single row data matched by composite primary key.  | 
            ||
| 204 | *  | 
            ||
| 205 | * @param mixed $parentId  | 
            ||
| 206 | * @param string $textMD5  | 
            ||
| 207 | *  | 
            ||
| 208 | * @return array  | 
            ||
| 209 | */  | 
            ||
| 210 | public function loadRow($parentId, $textMD5)  | 
            ||
| 220 | |||
| 221 | /**  | 
            ||
| 222 | * Loads autogenerated entry id by given $action and optionally $parentId.  | 
            ||
| 223 | *  | 
            ||
| 224 | * @param string $action  | 
            ||
| 225 | * @param mixed|null $parentId  | 
            ||
| 226 | *  | 
            ||
| 227 | * @return array  | 
            ||
| 228 | */  | 
            ||
| 229 | public function loadAutogeneratedEntry($action, $parentId = null)  | 
            ||
| 239 | |||
| 240 | /**  | 
            ||
| 241 | * Deletes all rows with given $action and optionally $id.  | 
            ||
| 242 | *  | 
            ||
| 243 | * If $id is set only autogenerated entries will be removed.  | 
            ||
| 244 | *  | 
            ||
| 245 | * @param string $action  | 
            ||
| 246 | * @param mixed|null $id  | 
            ||
| 247 | */  | 
            ||
| 248 | View Code Duplication | public function remove($action, $id = null)  | 
            |
| 258 | |||
| 259 | /**  | 
            ||
| 260 | * Loads paged list of global aliases.  | 
            ||
| 261 | *  | 
            ||
| 262 | * @param string|null $languageCode  | 
            ||
| 263 | * @param int $offset  | 
            ||
| 264 | * @param int $limit  | 
            ||
| 265 | *  | 
            ||
| 266 | * @return array  | 
            ||
| 267 | */  | 
            ||
| 268 | public function listGlobalEntries($languageCode = null, $offset = 0, $limit = -1)  | 
            ||
| 278 | |||
| 279 | /**  | 
            ||
| 280 | * Deletes single custom alias row matched by composite primary key.  | 
            ||
| 281 | *  | 
            ||
| 282 | * If $id is set only autogenerated entries will be removed.  | 
            ||
| 283 | *  | 
            ||
| 284 | * @param mixed $parentId  | 
            ||
| 285 | * @param string $textMD5  | 
            ||
| 286 | *  | 
            ||
| 287 | * @return bool  | 
            ||
| 288 | */  | 
            ||
| 289 | public function removeCustomAlias($parentId, $textMD5)  | 
            ||
| 299 | |||
| 300 | /**  | 
            ||
| 301 | * Loads complete URL alias data by given array of path hashes.  | 
            ||
| 302 | *  | 
            ||
| 303 | * @param string[] $urlHashes URL string hashes  | 
            ||
| 304 | *  | 
            ||
| 305 | * @return array  | 
            ||
| 306 | */  | 
            ||
| 307 | public function loadUrlAliasData(array $urlHashes)  | 
            ||
| 317 | |||
| 318 | /**  | 
            ||
| 319 | * Loads all data for the path identified by given $id.  | 
            ||
| 320 | *  | 
            ||
| 321 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException  | 
            ||
| 322 | *  | 
            ||
| 323 | * @param mixed $id  | 
            ||
| 324 | *  | 
            ||
| 325 | * @return array  | 
            ||
| 326 | */  | 
            ||
| 327 | public function loadPathData($id)  | 
            ||
| 337 | |||
| 338 | /**  | 
            ||
| 339 | * Loads path data identified by given ordered array of hierarchy data.  | 
            ||
| 340 | *  | 
            ||
| 341 | * The first entry in $hierarchyData corresponds to the top-most path element in the path, the second entry the  | 
            ||
| 342 | * child of the first path element and so on.  | 
            ||
| 343 | * This method is faster than self::getPath() since it can fetch all elements using only one query, but can be used  | 
            ||
| 344 | * only for autogenerated paths.  | 
            ||
| 345 | *  | 
            ||
| 346 | * @param array $hierarchyData  | 
            ||
| 347 | *  | 
            ||
| 348 | * @return array  | 
            ||
| 349 | */  | 
            ||
| 350 | public function loadPathDataByHierarchy(array $hierarchyData)  | 
            ||
| 360 | |||
| 361 | /**  | 
            ||
| 362 | * Loads all autogenerated entries with given $parentId with optionally included history entries.  | 
            ||
| 363 | *  | 
            ||
| 364 | * @param mixed $parentId  | 
            ||
| 365 | * @param bool $includeHistory  | 
            ||
| 366 | *  | 
            ||
| 367 | * @return array  | 
            ||
| 368 | */  | 
            ||
| 369 | public function loadAutogeneratedEntries($parentId, $includeHistory = false)  | 
            ||
| 379 | |||
| 380 | /**  | 
            ||
| 381 | * Returns next value for "id" column.  | 
            ||
| 382 | *  | 
            ||
| 383 | * @return mixed  | 
            ||
| 384 | */  | 
            ||
| 385 | public function getNextId()  | 
            ||
| 395 | |||
| 396 | View Code Duplication | public function getLocationContentMainLanguageId($locationId)  | 
            |
| 406 | |||
| 407 | /**  | 
            ||
| 408 |      * {@inheritdoc} | 
            ||
| 409 | */  | 
            ||
| 410 | View Code Duplication | public function bulkRemoveTranslation($languageId, $actions)  | 
            |
| 420 | |||
| 421 | /**  | 
            ||
| 422 |      * {@inheritdoc} | 
            ||
| 423 | */  | 
            ||
| 424 | View Code Duplication | public function archiveUrlAliasesForDeletedTranslations($locationId, $parentId, array $languageIds)  | 
            |
| 434 | |||
| 435 | /**  | 
            ||
| 436 |      * {@inheritdoc} | 
            ||
| 437 | */  | 
            ||
| 438 | View Code Duplication | public function deleteUrlAliasesWithoutLocation()  | 
            |
| 448 | |||
| 449 | /**  | 
            ||
| 450 |      * {@inheritdoc} | 
            ||
| 451 | */  | 
            ||
| 452 | View Code Duplication | public function deleteUrlAliasesWithoutParent()  | 
            |
| 462 | |||
| 463 | /**  | 
            ||
| 464 |      * {@inheritdoc} | 
            ||
| 465 | */  | 
            ||
| 466 | View Code Duplication | public function deleteUrlAliasesWithBrokenLink()  | 
            |
| 476 | |||
| 477 | /**  | 
            ||
| 478 |      * {@inheritdoc} | 
            ||
| 479 | */  | 
            ||
| 480 | View Code Duplication | public function repairBrokenUrlAliasesForLocation($locationId)  | 
            |
| 490 | }  | 
            ||
| 491 |