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 Notification 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 Notification, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Notification implements INotification { |
||
| 30 | /** @var string */ |
||
| 31 | protected $app; |
||
| 32 | |||
| 33 | /** @var string */ |
||
| 34 | protected $user; |
||
| 35 | |||
| 36 | /** @var \DateTime */ |
||
| 37 | protected $dateTime; |
||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | protected $objectType; |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | protected $objectId; |
||
| 44 | |||
| 45 | /** @var string */ |
||
| 46 | protected $subject; |
||
| 47 | |||
| 48 | /** @var array */ |
||
| 49 | protected $subjectParameters; |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | protected $subjectParsed; |
||
| 53 | |||
| 54 | /** @var string */ |
||
| 55 | protected $message; |
||
| 56 | |||
| 57 | /** @var array */ |
||
| 58 | protected $messageParameters; |
||
| 59 | |||
| 60 | /** @var string */ |
||
| 61 | protected $messageParsed; |
||
| 62 | |||
| 63 | /** @var string */ |
||
| 64 | protected $link; |
||
| 65 | |||
| 66 | /** @var string */ |
||
| 67 | protected $icon; |
||
| 68 | |||
| 69 | /** @var array */ |
||
| 70 | protected $actions; |
||
| 71 | |||
| 72 | /** @var array */ |
||
| 73 | protected $actionsParsed; |
||
| 74 | |||
| 75 | /** @var bool */ |
||
| 76 | protected $hasPrimaryAction; |
||
| 77 | |||
| 78 | /** @var bool */ |
||
| 79 | protected $hasPrimaryParsedAction; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Constructor |
||
| 83 | */ |
||
| 84 | public function __construct() { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $app |
||
| 105 | * @return $this |
||
| 106 | * @throws \InvalidArgumentException if the app id is invalid |
||
| 107 | * @since 8.2.0 |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function setApp($app) { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | * @since 8.2.0 |
||
| 120 | */ |
||
| 121 | public function getApp() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $user |
||
| 127 | * @return $this |
||
| 128 | * @throws \InvalidArgumentException if the user id is invalid |
||
| 129 | * @since 8.2.0 |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function setUser($user) { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return string |
||
| 141 | * @since 8.2.0 |
||
| 142 | */ |
||
| 143 | public function getUser() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param \DateTime $dateTime |
||
| 149 | * @return $this |
||
| 150 | * @throws \InvalidArgumentException if the $dateTime is invalid |
||
| 151 | * @since 9.0.0 |
||
| 152 | */ |
||
| 153 | public function setDateTime(\DateTime $dateTime) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return \DateTime |
||
| 163 | * @since 9.0.0 |
||
| 164 | */ |
||
| 165 | public function getDateTime() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $type |
||
| 171 | * @param string $id |
||
| 172 | * @return $this |
||
| 173 | * @throws \InvalidArgumentException if the object type or id is invalid |
||
| 174 | * @since 8.2.0 - 9.0.0: Type of $id changed to string |
||
| 175 | */ |
||
| 176 | public function setObject($type, $id) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return string |
||
| 191 | * @since 8.2.0 |
||
| 192 | */ |
||
| 193 | public function getObjectType() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return string |
||
| 199 | * @since 8.2.0 - 9.0.0: Return type changed to string |
||
| 200 | */ |
||
| 201 | public function getObjectId() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param string $subject |
||
| 207 | * @param array $parameters |
||
| 208 | * @return $this |
||
| 209 | * @throws \InvalidArgumentException if the subject or parameters are invalid |
||
| 210 | * @since 8.2.0 |
||
| 211 | */ |
||
| 212 | View Code Duplication | public function setSubject($subject, array $parameters = []) { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @return string |
||
| 227 | * @since 8.2.0 |
||
| 228 | */ |
||
| 229 | public function getSubject() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return string[] |
||
| 235 | * @since 8.2.0 |
||
| 236 | */ |
||
| 237 | public function getSubjectParameters() { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param string $subject |
||
| 243 | * @return $this |
||
| 244 | * @throws \InvalidArgumentException if the subject are invalid |
||
| 245 | * @since 8.2.0 |
||
| 246 | */ |
||
| 247 | public function setParsedSubject($subject) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return string |
||
| 257 | * @since 8.2.0 |
||
| 258 | */ |
||
| 259 | public function getParsedSubject() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $message |
||
| 265 | * @param array $parameters |
||
| 266 | * @return $this |
||
| 267 | * @throws \InvalidArgumentException if the message or parameters are invalid |
||
| 268 | * @since 8.2.0 |
||
| 269 | */ |
||
| 270 | View Code Duplication | public function setMessage($message, array $parameters = []) { |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @return string |
||
| 285 | * @since 8.2.0 |
||
| 286 | */ |
||
| 287 | public function getMessage() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return string[] |
||
| 293 | * @since 8.2.0 |
||
| 294 | */ |
||
| 295 | public function getMessageParameters() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $message |
||
| 301 | * @return $this |
||
| 302 | * @throws \InvalidArgumentException if the message are invalid |
||
| 303 | * @since 8.2.0 |
||
| 304 | */ |
||
| 305 | View Code Duplication | public function setParsedMessage($message) { |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @return string |
||
| 315 | * @since 8.2.0 |
||
| 316 | */ |
||
| 317 | public function getParsedMessage() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param string $link |
||
| 323 | * @return $this |
||
| 324 | * @throws \InvalidArgumentException if the link are invalid |
||
| 325 | * @since 8.2.0 |
||
| 326 | */ |
||
| 327 | View Code Duplication | public function setLink($link) { |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @return string |
||
| 337 | * @since 8.2.0 |
||
| 338 | */ |
||
| 339 | public function getLink() { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return IAction |
||
| 345 | * @since 8.2.0 |
||
| 346 | */ |
||
| 347 | public function createAction() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param IAction $action |
||
| 353 | * @return $this |
||
| 354 | * @throws \InvalidArgumentException if the action are invalid |
||
| 355 | * @since 8.2.0 |
||
| 356 | */ |
||
| 357 | View Code Duplication | public function addAction(IAction $action) { |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @return IAction[] |
||
| 376 | * @since 8.2.0 |
||
| 377 | */ |
||
| 378 | public function getActions() { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param IAction $action |
||
| 384 | * @return $this |
||
| 385 | * @throws \InvalidArgumentException if the action are invalid |
||
| 386 | * @since 8.2.0 |
||
| 387 | */ |
||
| 388 | View Code Duplication | public function addParsedAction(IAction $action) { |
|
| 404 | |||
| 405 | /** |
||
| 406 | * @return IAction[] |
||
| 407 | * @since 8.2.0 |
||
| 408 | */ |
||
| 409 | public function getParsedActions() { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @return bool |
||
| 415 | * @since 8.2.0 |
||
| 416 | */ |
||
| 417 | public function isValid() { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return bool |
||
| 427 | * @since 8.2.0 |
||
| 428 | */ |
||
| 429 | public function isValidParsed() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | protected function isValidCommon() { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Icon is an absolute URL to an image to be displayed in the notifications |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | * @since 10.0.3 |
||
| 459 | */ |
||
| 460 | public function getIcon() { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $icon |
||
| 466 | * @since 10.0.3 |
||
| 467 | */ |
||
| 468 | public function setIcon($icon) { |
||
| 474 | } |
||
| 475 |