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 |
||
| 30 | class Notification implements INotification { |
||
| 31 | /** @var string */ |
||
| 32 | protected $app; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | protected $user; |
||
| 36 | |||
| 37 | /** @var \DateTime */ |
||
| 38 | protected $dateTime; |
||
| 39 | |||
| 40 | /** @var string */ |
||
| 41 | protected $objectType; |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | protected $objectId; |
||
| 45 | |||
| 46 | /** @var string */ |
||
| 47 | protected $subject; |
||
| 48 | |||
| 49 | /** @var array */ |
||
| 50 | protected $subjectParameters; |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | protected $subjectParsed; |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | protected $message; |
||
| 57 | |||
| 58 | /** @var array */ |
||
| 59 | protected $messageParameters; |
||
| 60 | |||
| 61 | /** @var string */ |
||
| 62 | protected $messageParsed; |
||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | protected $link; |
||
| 66 | |||
| 67 | /** @var string */ |
||
| 68 | protected $icon; |
||
| 69 | |||
| 70 | /** @var array */ |
||
| 71 | protected $actions; |
||
| 72 | |||
| 73 | /** @var array */ |
||
| 74 | protected $actionsParsed; |
||
| 75 | |||
| 76 | /** @var bool */ |
||
| 77 | protected $hasPrimaryAction; |
||
| 78 | |||
| 79 | /** @var bool */ |
||
| 80 | protected $hasPrimaryParsedAction; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Constructor |
||
| 84 | */ |
||
| 85 | public function __construct() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $app |
||
| 106 | * @return $this |
||
| 107 | * @throws \InvalidArgumentException if the app id is invalid |
||
| 108 | * @since 8.2.0 |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function setApp($app) { |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | * @since 8.2.0 |
||
| 121 | */ |
||
| 122 | public function getApp() { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $user |
||
| 128 | * @return $this |
||
| 129 | * @throws \InvalidArgumentException if the user id is invalid |
||
| 130 | * @since 8.2.0 |
||
| 131 | */ |
||
| 132 | View Code Duplication | public function setUser($user) { |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return string |
||
| 142 | * @since 8.2.0 |
||
| 143 | */ |
||
| 144 | public function getUser() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param \DateTime $dateTime |
||
| 150 | * @return $this |
||
| 151 | * @throws \InvalidArgumentException if the $dateTime is invalid |
||
| 152 | * @since 9.0.0 |
||
| 153 | */ |
||
| 154 | public function setDateTime(\DateTime $dateTime) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return \DateTime |
||
| 164 | * @since 9.0.0 |
||
| 165 | */ |
||
| 166 | public function getDateTime() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $type |
||
| 172 | * @param string $id |
||
| 173 | * @return $this |
||
| 174 | * @throws \InvalidArgumentException if the object type or id is invalid |
||
| 175 | * @since 8.2.0 - 9.0.0: Type of $id changed to string |
||
| 176 | */ |
||
| 177 | public function setObject($type, $id) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return string |
||
| 192 | * @since 8.2.0 |
||
| 193 | */ |
||
| 194 | public function getObjectType() { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return string |
||
| 200 | * @since 8.2.0 - 9.0.0: Return type changed to string |
||
| 201 | */ |
||
| 202 | public function getObjectId() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $subject |
||
| 208 | * @param array $parameters |
||
| 209 | * @return $this |
||
| 210 | * @throws \InvalidArgumentException if the subject or parameters are invalid |
||
| 211 | * @since 8.2.0 |
||
| 212 | */ |
||
| 213 | View Code Duplication | public function setSubject($subject, array $parameters = []) { |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @return string |
||
| 228 | * @since 8.2.0 |
||
| 229 | */ |
||
| 230 | public function getSubject() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return string[] |
||
| 236 | * @since 8.2.0 |
||
| 237 | */ |
||
| 238 | public function getSubjectParameters() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param string $subject |
||
| 244 | * @return $this |
||
| 245 | * @throws \InvalidArgumentException if the subject is invalid |
||
| 246 | * @since 8.2.0 |
||
| 247 | */ |
||
| 248 | public function setParsedSubject($subject) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return string |
||
| 258 | * @since 8.2.0 |
||
| 259 | */ |
||
| 260 | public function getParsedSubject() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $message |
||
| 266 | * @param array $parameters |
||
| 267 | * @return $this |
||
| 268 | * @throws \InvalidArgumentException if the message or parameters are invalid |
||
| 269 | * @since 8.2.0 |
||
| 270 | */ |
||
| 271 | View Code Duplication | public function setMessage($message, array $parameters = []) { |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @return string |
||
| 286 | * @since 8.2.0 |
||
| 287 | */ |
||
| 288 | public function getMessage() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return string[] |
||
| 294 | * @since 8.2.0 |
||
| 295 | */ |
||
| 296 | public function getMessageParameters() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $message |
||
| 302 | * @return $this |
||
| 303 | * @throws \InvalidArgumentException if the message is invalid |
||
| 304 | * @since 8.2.0 |
||
| 305 | */ |
||
| 306 | public function setParsedMessage($message) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return string |
||
| 316 | * @since 8.2.0 |
||
| 317 | */ |
||
| 318 | public function getParsedMessage() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $link |
||
| 324 | * @return $this |
||
| 325 | * @throws \InvalidArgumentException if the link is invalid |
||
| 326 | * @since 8.2.0 |
||
| 327 | */ |
||
| 328 | View Code Duplication | public function setLink($link) { |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @return string |
||
| 338 | * @since 8.2.0 |
||
| 339 | */ |
||
| 340 | public function getLink() { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param string $icon |
||
| 346 | * @return $this |
||
| 347 | * @throws \InvalidArgumentException if the icon is invalid |
||
| 348 | * @since 9.2.0 |
||
| 349 | */ |
||
| 350 | View Code Duplication | public function setIcon($icon) { |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @return string |
||
| 360 | * @since 9.2.0 |
||
| 361 | */ |
||
| 362 | public function getIcon() { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @return IAction |
||
| 368 | * @since 8.2.0 |
||
| 369 | */ |
||
| 370 | public function createAction() { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param IAction $action |
||
| 376 | * @return $this |
||
| 377 | * @throws \InvalidArgumentException if the action is invalid |
||
| 378 | * @since 8.2.0 |
||
| 379 | */ |
||
| 380 | View Code Duplication | public function addAction(IAction $action) { |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @return IAction[] |
||
| 399 | * @since 8.2.0 |
||
| 400 | */ |
||
| 401 | public function getActions() { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param IAction $action |
||
| 407 | * @return $this |
||
| 408 | * @throws \InvalidArgumentException if the action is invalid |
||
| 409 | * @since 8.2.0 |
||
| 410 | */ |
||
| 411 | View Code Duplication | public function addParsedAction(IAction $action) { |
|
| 431 | |||
| 432 | /** |
||
| 433 | * @return IAction[] |
||
| 434 | * @since 8.2.0 |
||
| 435 | */ |
||
| 436 | public function getParsedActions() { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return bool |
||
| 442 | * @since 8.2.0 |
||
| 443 | */ |
||
| 444 | public function isValid() { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return bool |
||
| 454 | * @since 8.2.0 |
||
| 455 | */ |
||
| 456 | public function isValidParsed() { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return bool |
||
| 466 | */ |
||
| 467 | protected function isValidCommon() { |
||
| 480 | } |
||
| 481 |
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.