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 |
||
| 21 | class ExceptionConversion extends Gateway |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * The wrapped gateway. |
||
| 25 | * |
||
| 26 | * @var Gateway |
||
| 27 | */ |
||
| 28 | protected $innerGateway; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Creates a new exception conversion gateway around $innerGateway. |
||
| 32 | * |
||
| 33 | * @param Gateway $innerGateway |
||
| 34 | */ |
||
| 35 | public function __construct(Gateway $innerGateway) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Loads data for an object state. |
||
| 42 | * |
||
| 43 | * @param mixed $stateId |
||
| 44 | * |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | View Code Duplication | public function loadObjectStateData($stateId) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Loads data for an object state by identifier. |
||
| 60 | * |
||
| 61 | * @param string $identifier |
||
| 62 | * @param mixed $groupId |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function loadObjectStateDataByIdentifier($identifier, $groupId) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Loads data for all object states belonging to group with $groupId ID. |
||
| 79 | * |
||
| 80 | * @param mixed $groupId |
||
| 81 | * |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function loadObjectStateListData($groupId) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Loads data for an object state group. |
||
| 97 | * |
||
| 98 | * @param mixed $groupId |
||
| 99 | * |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | View Code Duplication | public function loadObjectStateGroupData($groupId) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Loads data for an object state group by identifier. |
||
| 115 | * |
||
| 116 | * @param string $identifier |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | View Code Duplication | public function loadObjectStateGroupDataByIdentifier($identifier) |
|
| 121 | { |
||
| 122 | try { |
||
| 123 | return $this->innerGateway->loadObjectStateGroupDataByIdentifier($identifier); |
||
| 124 | } catch (DBALException $e) { |
||
| 125 | throw new RuntimeException('Database error', 0, $e); |
||
| 126 | } catch (PDOException $e) { |
||
| 127 | throw new RuntimeException('Database error', 0, $e); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Loads data for all object state groups, filtered by $offset and $limit. |
||
| 133 | * |
||
| 134 | * @param int $offset |
||
| 135 | * @param int $limit |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | View Code Duplication | public function loadObjectStateGroupListData($offset, $limit) |
|
| 140 | { |
||
| 141 | try { |
||
| 142 | return $this->innerGateway->loadObjectStateGroupListData($offset, $limit); |
||
| 143 | } catch (DBALException $e) { |
||
| 144 | throw new RuntimeException('Database error', 0, $e); |
||
| 145 | } catch (PDOException $e) { |
||
| 146 | throw new RuntimeException('Database error', 0, $e); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Inserts a new object state into database. |
||
| 152 | * |
||
| 153 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $objectState |
||
| 154 | * @param int $groupId |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function insertObjectState(ObjectState $objectState, $groupId) |
|
| 157 | { |
||
| 158 | try { |
||
| 159 | return $this->innerGateway->insertObjectState($objectState, $groupId); |
||
| 160 | } catch (DBALException $e) { |
||
| 161 | throw new RuntimeException('Database error', 0, $e); |
||
| 162 | } catch (PDOException $e) { |
||
| 163 | throw new RuntimeException('Database error', 0, $e); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Updates the stored object state with provided data. |
||
| 169 | * |
||
| 170 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $objectState |
||
| 171 | */ |
||
| 172 | View Code Duplication | public function updateObjectState(ObjectState $objectState) |
|
| 173 | { |
||
| 174 | try { |
||
| 175 | return $this->innerGateway->updateObjectState($objectState); |
||
| 176 | } catch (DBALException $e) { |
||
| 177 | throw new RuntimeException('Database error', 0, $e); |
||
| 178 | } catch (PDOException $e) { |
||
| 179 | throw new RuntimeException('Database error', 0, $e); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Deletes object state identified by $stateId. |
||
| 185 | * |
||
| 186 | * @param int $stateId |
||
| 187 | */ |
||
| 188 | View Code Duplication | public function deleteObjectState($stateId) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Update object state links from $oldStateId to $newStateId. |
||
| 201 | * |
||
| 202 | * @param int $oldStateId |
||
| 203 | * @param int $newStateId |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function updateObjectStateLinks($oldStateId, $newStateId) |
|
| 206 | { |
||
| 207 | try { |
||
| 208 | return $this->innerGateway->updateObjectStateLinks($oldStateId, $newStateId); |
||
| 209 | } catch (DBALException $e) { |
||
| 210 | throw new RuntimeException('Database error', 0, $e); |
||
| 211 | } catch (PDOException $e) { |
||
| 212 | throw new RuntimeException('Database error', 0, $e); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Deletes object state links identified by $stateId. |
||
| 218 | * |
||
| 219 | * @param int $stateId |
||
| 220 | */ |
||
| 221 | View Code Duplication | public function deleteObjectStateLinks($stateId) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Inserts a new object state group into database. |
||
| 234 | * |
||
| 235 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $objectStateGroup |
||
| 236 | */ |
||
| 237 | View Code Duplication | public function insertObjectStateGroup(Group $objectStateGroup) |
|
| 238 | { |
||
| 239 | try { |
||
| 240 | return $this->innerGateway->insertObjectStateGroup($objectStateGroup); |
||
| 241 | } catch (DBALException $e) { |
||
| 242 | throw new RuntimeException('Database error', 0, $e); |
||
| 243 | } catch (PDOException $e) { |
||
| 244 | throw new RuntimeException('Database error', 0, $e); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Updates the stored object state group with provided data. |
||
| 250 | * |
||
| 251 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $objectStateGroup |
||
| 252 | */ |
||
| 253 | View Code Duplication | public function updateObjectStateGroup(Group $objectStateGroup) |
|
| 254 | { |
||
| 255 | try { |
||
| 256 | return $this->innerGateway->updateObjectStateGroup($objectStateGroup); |
||
| 257 | } catch (DBALException $e) { |
||
| 258 | throw new RuntimeException('Database error', 0, $e); |
||
| 259 | } catch (PDOException $e) { |
||
| 260 | throw new RuntimeException('Database error', 0, $e); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Deletes the object state group identified by $groupId. |
||
| 266 | * |
||
| 267 | * @param mixed $groupId |
||
| 268 | */ |
||
| 269 | View Code Duplication | public function deleteObjectStateGroup($groupId) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Sets the object state $stateId to content with $contentId ID. |
||
| 282 | * |
||
| 283 | * @param mixed $contentId |
||
| 284 | * @param mixed $groupId |
||
| 285 | * @param mixed $stateId |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function setContentState($contentId, $groupId, $stateId) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Loads object state data for $contentId content from $stateGroupId state group. |
||
| 300 | * |
||
| 301 | * @param int $contentId |
||
| 302 | * @param int $stateGroupId |
||
| 303 | * |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | View Code Duplication | public function loadObjectStateDataForContent($contentId, $stateGroupId) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Returns the number of objects which are in this state. |
||
| 319 | * |
||
| 320 | * @param mixed $stateId |
||
| 321 | * |
||
| 322 | * @return int |
||
| 323 | */ |
||
| 324 | View Code Duplication | public function getContentCount($stateId) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Updates the object state priority to provided value. |
||
| 337 | * |
||
| 338 | * @param mixed $stateId |
||
| 339 | * @param int $priority |
||
| 340 | */ |
||
| 341 | View Code Duplication | public function updateObjectStatePriority($stateId, $priority) |
|
| 351 | } |
||
| 352 |