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 ObjectRevision 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 ObjectRevision, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class ObjectRevision extends AbstractModel implements ObjectRevisionInterface |
||
|
|
|||
| 35 | { |
||
| 36 | use ModelFactoryTrait; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Object type of this revision (required) |
||
| 40 | * @var string $targetType |
||
| 41 | */ |
||
| 42 | private $targetType; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Object ID of this revision (required) |
||
| 46 | * @var mixed $objectId |
||
| 47 | */ |
||
| 48 | private $targetId; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Revision number. Sequential integer for each object's ID. (required) |
||
| 52 | * @var integer $revNum |
||
| 53 | */ |
||
| 54 | private $revNum; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Timestamp; when this revision was created |
||
| 58 | * @var DateTimeInterface|null $revTs |
||
| 59 | */ |
||
| 60 | private $revTs; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The (admin) user that was |
||
| 64 | * @var string|null $revUser |
||
| 65 | */ |
||
| 66 | private $revUser; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array $dataPrev |
||
| 70 | */ |
||
| 71 | private $dataPrev; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array $dataObj |
||
| 75 | */ |
||
| 76 | private $dataObj; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array $dataDiff |
||
| 80 | */ |
||
| 81 | private $dataDiff; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param Container $container DI Container. |
||
| 85 | * @return void |
||
| 86 | */ |
||
| 87 | protected function setDependencies(Container $container) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $targetType The object type (type-ident). |
||
| 96 | * @throws InvalidArgumentException If the obj type parameter is not a string. |
||
| 97 | * @return ObjectRevision Chainable |
||
| 98 | */ |
||
| 99 | public function setTargetType($targetType) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function getTargetType() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param mixed $targetId The object ID. |
||
| 120 | * @return ObjectRevision Chainable |
||
| 121 | */ |
||
| 122 | public function setTargetId($targetId) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return mixed |
||
| 130 | */ |
||
| 131 | public function getTargetId() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param integer $revNum The revision number. |
||
| 138 | * @throws InvalidArgumentException If the revision number argument is not numerical. |
||
| 139 | * @return ObjectRevision Chainable |
||
| 140 | */ |
||
| 141 | public function setRevNum($revNum) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return integer |
||
| 154 | */ |
||
| 155 | public function getRevNum() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param mixed $revTs The revision's timestamp. |
||
| 162 | * @throws InvalidArgumentException If the timestamp is invalid. |
||
| 163 | * @return ObjectRevision Chainable |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function setRevTs($revTs) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @return DateTimeInterface|null |
||
| 185 | */ |
||
| 186 | public function getRevTs() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $revUser The revision user ident. |
||
| 193 | * @throws InvalidArgumentException If the revision user parameter is not a string. |
||
| 194 | * @return ObjectRevision Chainable |
||
| 195 | */ |
||
| 196 | View Code Duplication | public function setRevUser($revUser) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getRevUser() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param string|array|null $data The previous revision data. |
||
| 221 | * @return ObjectRevision Chainable |
||
| 222 | */ |
||
| 223 | View Code Duplication | public function setDataPrev($data) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function getDataPrev() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param array|string|null $data The current revision (object) data. |
||
| 245 | * @return ObjectRevision Chainable |
||
| 246 | */ |
||
| 247 | View Code Duplication | public function setDataObj($data) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function getDataObj() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param array|string $data The data diff. |
||
| 269 | * @return ObjectRevision |
||
| 270 | */ |
||
| 271 | View Code Duplication | public function setDataDiff($data) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function getDataDiff() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Create a new revision from an object |
||
| 293 | * |
||
| 294 | * 1. Load the last revision |
||
| 295 | * 2. Load the current item from DB |
||
| 296 | * 3. Create diff from (1) and (2). |
||
| 297 | * |
||
| 298 | * @param RevisionableInterface $obj The object to create the revision from. |
||
| 299 | * @return ObjectRevision Chainable |
||
| 300 | */ |
||
| 301 | public function createFromObject(RevisionableInterface $obj) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param array $dataPrev Optional. Previous revision data. |
||
| 325 | * @param array $dataObj Optional. Current revision (object) data. |
||
| 326 | * @return array The diff data |
||
| 327 | */ |
||
| 328 | public function createDiff(array $dataPrev = null, array $dataObj = null) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Recursive arrayDiff. |
||
| 342 | * |
||
| 343 | * @param array $array1 First array. |
||
| 344 | * @param array $array2 Second Array. |
||
| 345 | * @return array The array diff. |
||
| 346 | */ |
||
| 347 | public function recursiveDiff(array $array1, array $array2) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param RevisionableInterface $obj The object to load the last revision of. |
||
| 388 | * @return ObjectRevision The last revision for the give object. |
||
| 389 | */ |
||
| 390 | View Code Duplication | public function lastObjectRevision(RevisionableInterface $obj) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Retrieve a specific object revision, by revision number. |
||
| 412 | * |
||
| 413 | * @param RevisionableInterface $obj Target object. |
||
| 414 | * @param integer $revNum The revision number to load. |
||
| 415 | * @return ObjectRevision |
||
| 416 | */ |
||
| 417 | View Code Duplication | public function objectRevisionNum(RevisionableInterface $obj, $revNum) |
|
| 437 | } |
||
| 438 |