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 Event 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 Event, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Event implements IEvent { |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | protected $app = ''; |
||
| 34 | /** @var string */ |
||
| 35 | protected $type = ''; |
||
| 36 | /** @var string */ |
||
| 37 | protected $affectedUser = ''; |
||
| 38 | /** @var string */ |
||
| 39 | protected $author = ''; |
||
| 40 | /** @var int */ |
||
| 41 | protected $timestamp = 0; |
||
| 42 | /** @var string */ |
||
| 43 | protected $subject = ''; |
||
| 44 | /** @var array */ |
||
| 45 | protected $subjectParameters = []; |
||
| 46 | /** @var string */ |
||
| 47 | protected $subjectParsed; |
||
| 48 | /** @var string */ |
||
| 49 | protected $subjectRich; |
||
| 50 | /** @var array */ |
||
| 51 | protected $subjectRichParameters; |
||
| 52 | /** @var string */ |
||
| 53 | protected $message = ''; |
||
| 54 | /** @var array */ |
||
| 55 | protected $messageParameters = []; |
||
| 56 | /** @var string */ |
||
| 57 | protected $messageParsed; |
||
| 58 | /** @var string */ |
||
| 59 | protected $messageRich; |
||
| 60 | /** @var array */ |
||
| 61 | protected $messageRichParameters; |
||
| 62 | /** @var string */ |
||
| 63 | protected $objectType = ''; |
||
| 64 | /** @var int */ |
||
| 65 | protected $objectId = 0; |
||
| 66 | /** @var string */ |
||
| 67 | protected $objectName = ''; |
||
| 68 | /** @var string */ |
||
| 69 | protected $link = ''; |
||
| 70 | /** @var string */ |
||
| 71 | protected $icon = ''; |
||
| 72 | |||
| 73 | /** @var IEvent */ |
||
| 74 | protected $child = null; |
||
| 75 | /** @var IValidator */ |
||
| 76 | protected $richValidator; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param IValidator $richValidator |
||
| 80 | */ |
||
| 81 | public function __construct(IValidator $richValidator) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Set the app of the activity |
||
| 87 | * |
||
| 88 | * @param string $app |
||
| 89 | * @return IEvent |
||
| 90 | * @throws \InvalidArgumentException if the app id is invalid |
||
| 91 | * @since 8.2.0 |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function setApp($app) { |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public function getApp() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set the type of the activity |
||
| 110 | * |
||
| 111 | * @param string $type |
||
| 112 | * @return IEvent |
||
| 113 | * @throws \InvalidArgumentException if the type is invalid |
||
| 114 | * @since 8.2.0 |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function setType($type) { |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function getType() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Set the affected user of the activity |
||
| 133 | * |
||
| 134 | * @param string $affectedUser |
||
| 135 | * @return IEvent |
||
| 136 | * @throws \InvalidArgumentException if the affected user is invalid |
||
| 137 | * @since 8.2.0 |
||
| 138 | */ |
||
| 139 | View Code Duplication | public function setAffectedUser($affectedUser) { |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getAffectedUser() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set the author of the activity |
||
| 156 | * |
||
| 157 | * @param string $author |
||
| 158 | * @return IEvent |
||
| 159 | * @throws \InvalidArgumentException if the author is invalid |
||
| 160 | * @since 8.2.0 |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function setAuthor($author) { |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function getAuthor() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set the timestamp of the activity |
||
| 179 | * |
||
| 180 | * @param int $timestamp |
||
| 181 | * @return IEvent |
||
| 182 | * @throws \InvalidArgumentException if the timestamp is invalid |
||
| 183 | * @since 8.2.0 |
||
| 184 | */ |
||
| 185 | public function setTimestamp($timestamp) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return int |
||
| 195 | */ |
||
| 196 | public function getTimestamp() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Set the subject of the activity |
||
| 202 | * |
||
| 203 | * @param string $subject |
||
| 204 | * @param array $parameters |
||
| 205 | * @return IEvent |
||
| 206 | * @throws \InvalidArgumentException if the subject or parameters are invalid |
||
| 207 | * @since 8.2.0 |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function setSubject($subject, array $parameters = []) { |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getSubject() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function getSubjectParameters() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $subject |
||
| 234 | * @return $this |
||
| 235 | * @throws \InvalidArgumentException if the subject is invalid |
||
| 236 | * @since 11.0.0 |
||
| 237 | */ |
||
| 238 | View Code Duplication | public function setParsedSubject($subject) { |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @return string |
||
| 248 | * @since 11.0.0 |
||
| 249 | */ |
||
| 250 | public function getParsedSubject() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $subject |
||
| 256 | * @param array $parameters |
||
| 257 | * @return $this |
||
| 258 | * @throws \InvalidArgumentException if the subject or parameters are invalid |
||
| 259 | * @since 11.0.0 |
||
| 260 | */ |
||
| 261 | View Code Duplication | public function setRichSubject($subject, array $parameters = []) { |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @return string |
||
| 277 | * @since 11.0.0 |
||
| 278 | */ |
||
| 279 | public function getRichSubject() { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return array[] |
||
| 285 | * @since 11.0.0 |
||
| 286 | */ |
||
| 287 | public function getRichSubjectParameters() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Set the message of the activity |
||
| 293 | * |
||
| 294 | * @param string $message |
||
| 295 | * @param array $parameters |
||
| 296 | * @return IEvent |
||
| 297 | * @throws \InvalidArgumentException if the message or parameters are invalid |
||
| 298 | * @since 8.2.0 |
||
| 299 | */ |
||
| 300 | View Code Duplication | public function setMessage($message, array $parameters = []) { |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public function getMessage() { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function getMessageParameters() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $message |
||
| 325 | * @return $this |
||
| 326 | * @throws \InvalidArgumentException if the message is invalid |
||
| 327 | * @since 11.0.0 |
||
| 328 | */ |
||
| 329 | public function setParsedMessage($message) { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return string |
||
| 339 | * @since 11.0.0 |
||
| 340 | */ |
||
| 341 | public function getParsedMessage() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $message |
||
| 347 | * @param array $parameters |
||
| 348 | * @return $this |
||
| 349 | * @throws \InvalidArgumentException if the subject or parameters are invalid |
||
| 350 | * @since 11.0.0 |
||
| 351 | */ |
||
| 352 | View Code Duplication | public function setRichMessage($message, array $parameters = []) { |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @return string |
||
| 368 | * @since 11.0.0 |
||
| 369 | */ |
||
| 370 | public function getRichMessage() { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @return array[] |
||
| 376 | * @since 11.0.0 |
||
| 377 | */ |
||
| 378 | public function getRichMessageParameters() { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set the object of the activity |
||
| 384 | * |
||
| 385 | * @param string $objectType |
||
| 386 | * @param int $objectId |
||
| 387 | * @param string $objectName |
||
| 388 | * @return IEvent |
||
| 389 | * @throws \InvalidArgumentException if the object is invalid |
||
| 390 | * @since 8.2.0 |
||
| 391 | */ |
||
| 392 | public function setObject($objectType, $objectId, $objectName = '') { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | public function getObjectType() { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function getObjectId() { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public function getObjectName() { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Set the link of the activity |
||
| 431 | * |
||
| 432 | * @param string $link |
||
| 433 | * @return IEvent |
||
| 434 | * @throws \InvalidArgumentException if the link is invalid |
||
| 435 | * @since 8.2.0 |
||
| 436 | */ |
||
| 437 | View Code Duplication | public function setLink($link) { |
|
| 444 | |||
| 445 | /** |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public function getLink() { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param string $icon |
||
| 454 | * @return $this |
||
| 455 | * @throws \InvalidArgumentException if the icon is invalid |
||
| 456 | * @since 11.0.0 |
||
| 457 | */ |
||
| 458 | View Code Duplication | public function setIcon($icon) { |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @return string |
||
| 468 | * @since 11.0.0 |
||
| 469 | */ |
||
| 470 | public function getIcon() { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param IEvent $child |
||
| 476 | * @since 11.0.0 |
||
| 477 | */ |
||
| 478 | public function setChildEvent(IEvent $child) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @return IEvent|null |
||
| 484 | * @since 11.0.0 |
||
| 485 | */ |
||
| 486 | public function getChildEvent() { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @return bool |
||
| 492 | * @since 8.2.0 |
||
| 493 | */ |
||
| 494 | public function isValid() { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @return bool |
||
| 504 | * @since 8.2.0 |
||
| 505 | */ |
||
| 506 | View Code Duplication | public function isValidParsed() { |
|
| 529 | |||
| 530 | /** |
||
| 531 | * @return bool |
||
| 532 | */ |
||
| 533 | protected function isValidCommon() { |
||
| 551 | } |
||
| 552 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.