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 NotifynderManager 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 NotifynderManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class NotifynderManager extends NotifynderBuilder implements Notifynder |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Version Notifynder. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | const VERSION = '3.1.0'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var NotifynderCategory |
||
| 36 | */ |
||
| 37 | protected $notifynderCategory; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $categoriesContainer = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Models\NotificationCategory|null |
||
| 46 | */ |
||
| 47 | protected $defaultCategory; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var NotifynderSender |
||
| 51 | */ |
||
| 52 | protected $notifynderSender; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var NotifynderNotification |
||
| 56 | */ |
||
| 57 | protected $notification; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var NotifynderDispatcher |
||
| 61 | */ |
||
| 62 | protected $notifynderDispatcher; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * This sender method |
||
| 66 | * will be used on the dispatcher. |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $eventSender = 'send'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var NotifynderGroup |
||
| 74 | */ |
||
| 75 | protected $notifynderGroup; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string | null |
||
| 79 | */ |
||
| 80 | protected $entity; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param NotifynderCategory $notifynderCategory |
||
| 84 | * @param NotifynderSender $notifynderSender |
||
| 85 | * @param NotifynderNotification $notification |
||
| 86 | * @param NotifynderDispatcher $notifynderDispatcher |
||
| 87 | * @param NotifynderGroup $notifynderGroup |
||
| 88 | */ |
||
| 89 | public function __construct(NotifynderCategory $notifynderCategory, |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Set the category of the |
||
| 106 | * notification. |
||
| 107 | * |
||
| 108 | * @param $name |
||
| 109 | * @return $this |
||
| 110 | */ |
||
| 111 | public function category($name) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Define an entity when Notifynder is |
||
| 142 | * used Polymorpically. |
||
| 143 | * |
||
| 144 | * @param $name |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function entity($name) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Add a category. |
||
| 156 | * |
||
| 157 | * @param $name |
||
| 158 | * @param $text |
||
| 159 | * @return static |
||
| 160 | */ |
||
| 161 | public function addCategory($name, $text) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Update a category. |
||
| 168 | * |
||
| 169 | * @param array $updates |
||
| 170 | * @param $id |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | public function updateCategory(array $updates, $categoryId) |
||
| 174 | { |
||
| 175 | return $this->notifynderCategory->update($updates, $categoryId); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Send notifications |
||
| 180 | * Both multiple and single. |
||
| 181 | * |
||
| 182 | * @param array $info |
||
| 183 | * @return mixed |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function send($info = []) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Send immediately the notification |
||
| 198 | * even if the queue is enabled. |
||
| 199 | * |
||
| 200 | * @param array $info |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | View Code Duplication | public function sendNow($info = []) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Send One notification. |
||
| 216 | * |
||
| 217 | * @param array $info |
||
| 218 | * @return mixed |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function sendOne($info = []) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Send multiple notifications. |
||
| 233 | * |
||
| 234 | * @param array $info |
||
| 235 | * @return Senders\SendMultiple |
||
| 236 | */ |
||
| 237 | View Code Duplication | public function sendMultiple($info = []) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Send a group of notifications. |
||
| 250 | * |
||
| 251 | * @param $groupName |
||
| 252 | * @param $info |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | public function sendGroup($groupName, $info = []) |
||
| 256 | { |
||
| 257 | $info = (count($info) > 0) ? $info : $this->toArray(); |
||
| 258 | |||
| 259 | $notificationsSent = $this->notifynderSender->sendGroup($this, $groupName, $info); |
||
| 260 | |||
| 261 | $this->refresh(); |
||
| 262 | |||
| 263 | return $notificationsSent; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Read one notification. |
||
| 268 | * |
||
| 269 | * @param $notificationId |
||
| 270 | * @return bool|Models\Notification |
||
| 271 | */ |
||
| 272 | public function readOne($notificationId) |
||
| 273 | { |
||
| 274 | return $this->notification->readOne($notificationId); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Read notification in base the number |
||
| 279 | * Given. |
||
| 280 | * |
||
| 281 | * @param $toId |
||
| 282 | * @param $numbers |
||
| 283 | * @param string $order |
||
| 284 | * @return mixed |
||
| 285 | */ |
||
| 286 | public function readLimit($toId, $numbers, $order = 'ASC') |
||
| 287 | { |
||
| 288 | $notification = $this->notification->entity($this->entity); |
||
| 289 | |||
| 290 | return $notification->readLimit($toId, $numbers, $order); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Read all notifications of the given |
||
| 295 | * entity. |
||
| 296 | * |
||
| 297 | * @param $toId |
||
| 298 | * @return Number |
||
| 299 | */ |
||
| 300 | public function readAll($toId) |
||
| 301 | { |
||
| 302 | $notifications = $this->notification->entity($this->entity); |
||
| 303 | |||
| 304 | return $notifications->readAll($toId); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Delete a single notification. |
||
| 309 | * |
||
| 310 | * @param $notificationId |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | public function delete($notificationId) |
||
| 314 | { |
||
| 315 | return $this->notification->delete($notificationId); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Delete number of notifications |
||
| 320 | * secified of the given entity. |
||
| 321 | * |
||
| 322 | * @param $toId |
||
| 323 | * @param $number |
||
| 324 | * @param string $order |
||
| 325 | * @return mixed |
||
| 326 | */ |
||
| 327 | public function deleteLimit($toId, $number, $order = 'ASC') |
||
| 328 | { |
||
| 329 | $notifications = $this->notification->entity($this->entity); |
||
| 330 | |||
| 331 | return $notifications->deleteLimit($toId, $number, $order); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Delete all notifications |
||
| 336 | * of the the given entity. |
||
| 337 | * |
||
| 338 | * @param $toId |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | public function deleteAll($toId) |
||
| 342 | { |
||
| 343 | $notifications = $this->notification->entity($this->entity); |
||
| 344 | |||
| 345 | return $notifications->deleteAll($toId); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Delete All notifications from a |
||
| 350 | * defined category. |
||
| 351 | * |
||
| 352 | * @param $categoryName string |
||
| 353 | * @param $expired Bool |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | public function deleteByCategory($categoryName, $expired = false) |
||
| 357 | { |
||
| 358 | return $this->notification->deleteByCategory($categoryName, $expired); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get Notifications not read |
||
| 363 | * of the given entity. |
||
| 364 | * |
||
| 365 | * @param $toId |
||
| 366 | * @param null $limit |
||
| 367 | * @param null|int $paginate |
||
| 368 | * @param string $order |
||
| 369 | * @param Closure $filterScope |
||
| 370 | * @return mixed |
||
| 371 | */ |
||
| 372 | public function getNotRead($toId, $limit = null, $paginate = null, $order = 'desc', Closure $filterScope = null) |
||
| 373 | { |
||
| 374 | $notifications = $this->notification->entity($this->entity); |
||
| 375 | |||
| 376 | return $notifications->getNotRead($toId, $limit, $paginate, $order, $filterScope); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get all notifications of the |
||
| 381 | * given entity. |
||
| 382 | * |
||
| 383 | * @param $toId |
||
| 384 | * @param null $limit |
||
| 385 | * @param int|null $paginate |
||
| 386 | * @param string $order |
||
| 387 | * @param Closure $filterScope |
||
| 388 | * @return mixed |
||
| 389 | */ |
||
| 390 | public function getAll($toId, $limit = null, $paginate = null, $order = 'desc', Closure $filterScope = null) |
||
| 391 | { |
||
| 392 | $notifications = $this->notification->entity($this->entity); |
||
| 393 | |||
| 394 | return $notifications->getAll($toId, $limit, $paginate, $order, $filterScope); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get number of notification not read |
||
| 399 | * of the given entity. |
||
| 400 | * |
||
| 401 | * @param $toId |
||
| 402 | * @param Closure $filterScope |
||
| 403 | * @return mixed |
||
| 404 | */ |
||
| 405 | public function countNotRead($toId, Closure $filterScope = null) |
||
| 406 | { |
||
| 407 | $notifications = $this->notification->entity($this->entity); |
||
| 408 | |||
| 409 | return $notifications->countNotRead($toId, $filterScope); |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Find Notification by ID. |
||
| 414 | * |
||
| 415 | * @param $notificationId |
||
| 416 | * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|static |
||
| 417 | */ |
||
| 418 | public function findNotificationById($notificationId) |
||
| 419 | { |
||
| 420 | return $this->notification->find($notificationId); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Get last notification of the given |
||
| 425 | * entity, second parameter can filter by |
||
| 426 | * category. |
||
| 427 | * |
||
| 428 | * @param $toId |
||
| 429 | * @param null $category |
||
| 430 | * @param Closure $filterScope |
||
| 431 | * @return mixed |
||
| 432 | */ |
||
| 433 | public function getLastNotification($toId, $category = null, Closure $filterScope = null) |
||
| 434 | { |
||
| 435 | $notification = $this->notification->entity($this->entity); |
||
| 436 | |||
| 437 | if (is_null($category)) { |
||
| 438 | return $notification->getLastNotification($toId, $filterScope); |
||
| 439 | } |
||
| 440 | |||
| 441 | return $notification->getLastNotificationByCategory($category, $toId, $filterScope); |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Add category to a group |
||
| 446 | * giving the names of them. |
||
| 447 | * |
||
| 448 | * @param $groupName |
||
| 449 | * @param $categoryName |
||
| 450 | * @return mixed |
||
| 451 | */ |
||
| 452 | public function addCategoryToGroupByName($groupName, $categoryName) |
||
| 453 | { |
||
| 454 | return $this->notifynderGroup->addCategoryToGroupByName($groupName, $categoryName); |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Add category to a group |
||
| 459 | * giving the ids of them. |
||
| 460 | * |
||
| 461 | * @param $groupId |
||
| 462 | * @param $categoryId |
||
| 463 | * @return mixed |
||
| 464 | */ |
||
| 465 | public function addCategoryToGroupById($groupId, $categoryId) |
||
| 466 | { |
||
| 467 | return $this->notifynderGroup->addCategoryToGroupById($groupId, $categoryId); |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Add categories to a group having as first parameter |
||
| 472 | * the name of the group, and others as name |
||
| 473 | * categories. |
||
| 474 | * |
||
| 475 | * @return mixed |
||
| 476 | */ |
||
| 477 | public function addCategoriesToGroup() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Fire method for fire listeners |
||
| 484 | * of logic. |
||
| 485 | * |
||
| 486 | * @param string $key |
||
| 487 | * @param string $categoryName |
||
| 488 | * @param mixed|null $values |
||
| 489 | * @return mixed|null |
||
| 490 | */ |
||
| 491 | public function fire($key, $categoryName, $values = []) |
||
| 492 | { |
||
| 493 | return $this->notifynderDispatcher->sendWith($this->eventSender) |
||
| 494 | ->fire($this, $key, $categoryName, $values); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Associate events to categories. |
||
| 499 | * |
||
| 500 | * @param $data |
||
| 501 | * @param array $delegation |
||
| 502 | * @return mixed |
||
| 503 | */ |
||
| 504 | public function delegate(array $delegation, $data = []) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Boot Listeners. |
||
| 511 | * |
||
| 512 | * @param array $listeners |
||
| 513 | */ |
||
| 514 | public function bootListeners(array $listeners) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Get instance of the notifynder builder. |
||
| 521 | * |
||
| 522 | * @return NotifynderBuilder |
||
| 523 | */ |
||
| 524 | public function builder() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Extend a custom sender method. |
||
| 531 | * |
||
| 532 | * @param $name |
||
| 533 | * @param callable $registrar |
||
| 534 | * @return $this |
||
| 535 | */ |
||
| 536 | public function extend($name, $registrar) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Check if the category is eager Loaded. |
||
| 550 | * |
||
| 551 | * @param $name |
||
| 552 | * @return bool |
||
| 553 | */ |
||
| 554 | protected function isLazyLoaded($name) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Return the Id of the category. |
||
| 561 | * |
||
| 562 | * @return mixed |
||
| 563 | */ |
||
| 564 | public function id() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Push a category in the categoriesContainer |
||
| 571 | * property. |
||
| 572 | * |
||
| 573 | * @param $name |
||
| 574 | * @param array $categoriesContainer |
||
| 575 | */ |
||
| 576 | protected function setCategoriesContainer($name, $categoriesContainer) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Get the categoriesContainer property. |
||
| 583 | * |
||
| 584 | * @param $name |
||
| 585 | * @return array |
||
| 586 | */ |
||
| 587 | public function getCategoriesContainer($name) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Define which method |
||
| 594 | * the event dispatcher has |
||
| 595 | * to send the notifications. |
||
| 596 | * |
||
| 597 | * @param $customSenderName |
||
| 598 | * @return $this |
||
| 599 | */ |
||
| 600 | public function dispatchWith($customSenderName) |
||
| 601 | { |
||
| 602 | $this->eventSender = $customSenderName; |
||
| 603 | |||
| 604 | return $this; |
||
| 605 | } |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Call the custom sender method. |
||
| 609 | * |
||
| 610 | * @param $name |
||
| 611 | * @param $arguments |
||
| 612 | * @return void|mixed |
||
| 613 | */ |
||
| 614 | public function __call($name, $arguments) |
||
| 615 | { |
||
| 616 | if (starts_with($name, 'send')) { |
||
| 617 | $arguments = (isset($arguments[0])) ? $arguments[0] : $this->toArray(); |
||
| 618 | |||
| 619 | $notificationsSent = $this->notifynderSender->customSender($name, $arguments); |
||
| 620 | $this->refresh(); |
||
| 621 | |||
| 622 | return $notificationsSent; |
||
| 623 | } |
||
| 624 | |||
| 625 | $error = "method [$name] not found in the class ".self::class; |
||
| 626 | throw new BadMethodCallException($error); |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Set builder properties |
||
| 631 | * When setting dynamic properties. |
||
| 632 | * |
||
| 633 | * @param $name |
||
| 634 | * @param $value |
||
| 635 | */ |
||
| 636 | public function __set($name, $value) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Get property from the |
||
| 643 | * builder. |
||
| 644 | * |
||
| 645 | * @param $name |
||
| 646 | * @return mixed |
||
| 647 | */ |
||
| 648 | public function __get($name) |
||
| 652 | } |
||
| 653 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..