Complex classes like BaseEvents 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 BaseEvents, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | abstract class BaseEvents |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | public $holder = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | public $key; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $storage = []; |
||
| 29 | |||
| 30 | final public function ShopAdminPropertiesCreate() { |
||
| 35 | |||
| 36 | final public function ShopAdminPropertiesFastCreate() { |
||
| 41 | |||
| 42 | final public function ShopAdminPropertiesPreCreate() { |
||
| 47 | |||
| 48 | final public function ShopAdminPropertiesPreFastCreate() { |
||
| 53 | |||
| 54 | final public function onShopPropertiesDelete() { |
||
| 59 | |||
| 60 | final public function onShopPropertiesEdit() { |
||
| 65 | |||
| 66 | public function get() { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $key |
||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | final public function on($key) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 83 | * <table> |
||
| 84 | * <tr valign="top"><td>['commentId']</td><td>Comment ID</td></tr> |
||
| 85 | * </table> |
||
| 86 | * @return BaseEvents |
||
| 87 | * @author Kaero |
||
| 88 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 89 | */ |
||
| 90 | final public function onAddComment() { |
||
| 95 | |||
| 96 | public function onAddItemToCart() { |
||
| 101 | |||
| 102 | /** */ |
||
| 103 | public function onAddToCart() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 111 | * <table> |
||
| 112 | * <tr valign="top"><td>['userId']</td><td>User ID</td></tr> |
||
| 113 | * <tr valign="top"><td>['name']</td><td>Category name</td></tr> |
||
| 114 | * <tr valign="top"><td>['url']</td><td>Category url</td></tr> |
||
| 115 | * <tr valign="top"><td>['short_desc']</td><td>Category short description</td></tr> |
||
| 116 | * </table> |
||
| 117 | * @return BaseEvents |
||
| 118 | * @author Kaero |
||
| 119 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 120 | */ |
||
| 121 | final public function onAdminCategoryCreate() { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | final public function onAdminCategoryPreCreate() { |
||
| 135 | |||
| 136 | final public function onAdminCategoryPreUpdate() { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 144 | * <table> |
||
| 145 | * <tr valign="top"><td>['userId']</td><td>User ID</td></tr> |
||
| 146 | * <tr valign="top"><td>['name']</td><td>Category name</td></tr> |
||
| 147 | * <tr valign="top"><td>['url']</td><td>Category url</td></tr> |
||
| 148 | * <tr valign="top"><td>['short_desc']</td><td>Category short description</td></tr> |
||
| 149 | * </table> |
||
| 150 | * @return BaseEvents |
||
| 151 | * @author Kaero |
||
| 152 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 153 | */ |
||
| 154 | final public function onAdminCategoryUpdate() { |
||
| 159 | |||
| 160 | final public function onAdminDashboardShow() { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 168 | * @return BaseEvents |
||
| 169 | * @author a.gula |
||
| 170 | * @copyright ImageCMS (c) 2014, a.gula <[email protected]> |
||
| 171 | */ |
||
| 172 | public function onAdminModulesTable() { |
||
| 177 | |||
| 178 | final public function onAdminOrderUserCreate() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 186 | * <table> |
||
| 187 | * <tr valign="top"><td>['id']</td><td>Page ID</td></tr> |
||
| 188 | * <tr valign="top"><td>['title']</td><td>Page title</td></tr> |
||
| 189 | * <tr valign="top"><td>['full_text']</td><td>Page full text</td></tr> |
||
| 190 | * <tr valign="top"><td>['prev_text']</td><td>Page short text</td></tr> |
||
| 191 | * </table> |
||
| 192 | * @return BaseEvents |
||
| 193 | * @author Kaero |
||
| 194 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 195 | */ |
||
| 196 | final public function onAdminPageCreate() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 204 | * <table> |
||
| 205 | * <tr valign="top"><td>['id']</td><td>Page ID</td></tr> |
||
| 206 | * <tr valign="top"><td>['title']</td><td>Page title</td></tr> |
||
| 207 | * <tr valign="top"><td>['full_text']</td><td>Page full text</td></tr> |
||
| 208 | * <tr valign="top"><td>['prev_text']</td><td>Page short text</td></tr> |
||
| 209 | * </table> |
||
| 210 | * @return BaseEvents |
||
| 211 | * @author Kaero |
||
| 212 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 213 | */ |
||
| 214 | final public function onAdminPageDelete() { |
||
| 219 | |||
| 220 | final public function onAdminPagePreCreate() { |
||
| 225 | |||
| 226 | final public function onAdminPagePreEdit() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 234 | * <table> |
||
| 235 | * <tr valign="top"><td>['id']</td><td>Page ID</td></tr> |
||
| 236 | * <tr valign="top"><td>['title']</td><td>Page title</td></tr> |
||
| 237 | * <tr valign="top"><td>['full_text']</td><td>Page full text</td></tr> |
||
| 238 | * <tr valign="top"><td>['prev_text']</td><td>Page short text</td></tr> |
||
| 239 | * </table> |
||
| 240 | * @return BaseEvents |
||
| 241 | * @author Kaero |
||
| 242 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 243 | */ |
||
| 244 | final public function onAdminPageUpdate() { |
||
| 249 | |||
| 250 | final public function onAuthUserRegister() { |
||
| 255 | |||
| 256 | public function onBrandPageLoad() { |
||
| 261 | |||
| 262 | public function onBrandsPageLoad() { |
||
| 267 | |||
| 268 | final public function onCartInit() { |
||
| 273 | |||
| 274 | public function onCartShowed() { |
||
| 279 | |||
| 280 | public function onActionTypeSearch() { |
||
| 284 | |||
| 285 | public function onCategoryPageLoad() { |
||
| 290 | |||
| 291 | public function onGalleryLoad() { |
||
| 295 | |||
| 296 | public function onGalleryAlbumLoad() { |
||
| 300 | |||
| 301 | public function onGalleryCategoryLoad() { |
||
| 305 | |||
| 306 | final public function onCorePageLoaded() { |
||
| 311 | |||
| 312 | final public function onFrontOrderUserCreate() { |
||
| 317 | |||
| 318 | final public function onNotifyingRequest() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return $this |
||
| 326 | */ |
||
| 327 | final public function onPageCategoryLoad() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | final public function onPageLoad() { |
||
| 341 | |||
| 342 | final public function onPaimentSystemSuccessPaid() { |
||
| 347 | |||
| 348 | public function onProductPageLoad() { |
||
| 353 | |||
| 354 | final public function onProfileApiChangeInfo() { |
||
| 359 | |||
| 360 | public function onRemoveFromCart() { |
||
| 365 | |||
| 366 | final public function onSearchPageLoad() { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 374 | * @return BaseEvents |
||
| 375 | * @author a.gula |
||
| 376 | * @copyright ImageCMS (c) 2014, a.gula <[email protected]> |
||
| 377 | */ |
||
| 378 | public function onSeoExpertSave() { |
||
| 383 | |||
| 384 | final public function onShopAdminAjaxChangeOrderPaid() { |
||
| 389 | |||
| 390 | final public function onShopAdminAjaxChangeOrderStatus() { |
||
| 395 | |||
| 396 | final public function onShopAdminOrderCreate() { |
||
| 401 | |||
| 402 | final public function onShopAdminOrderDelete() { |
||
| 407 | |||
| 408 | final public function onShopAdminOrderEdit() { |
||
| 413 | |||
| 414 | /** */ |
||
| 415 | |||
| 416 | final public function onShopAdminOrderUserCreate() { |
||
| 421 | |||
| 422 | /** */ |
||
| 423 | /** */ |
||
| 424 | |||
| 425 | /** |
||
| 426 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 427 | * <table> |
||
| 428 | * <tr valign="top"><td>['model']</td><td>Brand model</td></tr> |
||
| 429 | * <tr valign="top"><td>['userId']</td><td>User Id</td></tr> |
||
| 430 | * </table> |
||
| 431 | * @return BaseEvents |
||
| 432 | * @author Hellmark |
||
| 433 | * @copyright ImageCMS (c) 2014, Hellmark <[email protected]> |
||
| 434 | */ |
||
| 435 | public function onShopBrandCreate() { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 443 | * <table> |
||
| 444 | * <tr valign="top"><td>['brandId']</td><td>Brand Id</td></tr> |
||
| 445 | * <tr valign="top"><td>['url']</td><td>Brand Url</td></tr> |
||
| 446 | * </table> |
||
| 447 | * @return BaseEvents |
||
| 448 | * @author Hellmark |
||
| 449 | * @copyright ImageCMS (c) 2014, Hellmark <[email protected]> |
||
| 450 | */ |
||
| 451 | public function onShopBrandDelete() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 459 | * <table> |
||
| 460 | * <tr valign="top"><td>['model']</td><td>Brand model</td></tr> |
||
| 461 | * <tr valign="top"><td>['url']</td><td>Brand Url</td></tr> |
||
| 462 | * <tr valign="top"><td>['userId']</td><td>User Id</td></tr> |
||
| 463 | * </table> |
||
| 464 | * @return BaseEvents |
||
| 465 | * @author Hellmark |
||
| 466 | * @copyright ImageCMS (c) 2014, Hellmark <[email protected]> |
||
| 467 | */ |
||
| 468 | public function onShopBrandEdit() { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 476 | * @return BaseEvents |
||
| 477 | * @author a.gula |
||
| 478 | * @copyright ImageCMS (c) 2014, a.gula <[email protected]> |
||
| 479 | */ |
||
| 480 | public function onShopBrandPreCreate() { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 488 | * <table> |
||
| 489 | * <tr valign="top"><td>['model']</td><td>Brand model</td></tr> |
||
| 490 | * <tr valign="top"><td>['url']</td><td>Brand Url</td></tr> |
||
| 491 | * <tr valign="top"><td>['userId']</td><td>User Id</td></tr> |
||
| 492 | * </table> |
||
| 493 | * @return BaseEvents |
||
| 494 | * @author Hellmark |
||
| 495 | * @copyright ImageCMS (c) 2014, Hellmark <[email protected]> |
||
| 496 | */ |
||
| 497 | public function onShopBrandPreEdit() { |
||
| 502 | |||
| 503 | final public function onShopCallback() { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 511 | * <table> |
||
| 512 | * <tr valign="top"><td>['ShopCategoryId']</td><td>Category ID</td></tr> |
||
| 513 | * </table> |
||
| 514 | * @return BaseEvents |
||
| 515 | * @author Kaero |
||
| 516 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 517 | */ |
||
| 518 | public function onShopCategoryCreate() { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 526 | * <table> |
||
| 527 | * <tr valign="top"><td>['ShopCategoryId']</td><td>Category ID</td></tr> |
||
| 528 | * </table> |
||
| 529 | * @return BaseEvents |
||
| 530 | * @author Kaero |
||
| 531 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 532 | */ |
||
| 533 | public function onShopCategoryDelete() { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 541 | * <table> |
||
| 542 | * <tr valign="top"><td>['ShopCategoryId']</td><td>Category ID</td></tr> |
||
| 543 | * </table> |
||
| 544 | * @return BaseEvents |
||
| 545 | * @author Kaero |
||
| 546 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 547 | */ |
||
| 548 | public function onShopCategoryEdit() { |
||
| 553 | |||
| 554 | public function onShopCategoryFastCreate() { |
||
| 559 | |||
| 560 | public function onShopCategoryPreCreate() { |
||
| 565 | |||
| 566 | public function onShopCategoryPreEdit() { |
||
| 571 | |||
| 572 | public function onShopCategoryPreFastCreate() { |
||
| 577 | |||
| 578 | final public function onShopDashboardShow() { |
||
| 583 | |||
| 584 | final public function onShopMakeOrder() { |
||
| 589 | |||
| 590 | final public function onShopOrderView() { |
||
| 595 | |||
| 596 | final public function onShopProductAjaxChangeActive() { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 604 | * <table> |
||
| 605 | * <tr valign="top"><td>['userId']</td><td>User ID</td></tr> |
||
| 606 | * <tr valign="top"><td>['productId']</td><td>Product ID</td></tr> |
||
| 607 | * </table> |
||
| 608 | * @return BaseEvents |
||
| 609 | * @author Kaero |
||
| 610 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 611 | */ |
||
| 612 | final public function onShopProductCreate() { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 620 | * <table> |
||
| 621 | * <tr valign="top"><td>['userId']</td><td>User ID</td></tr> |
||
| 622 | * <tr valign="top"><td>['model']</td><td></td>Instanceof SProducts</tr> |
||
| 623 | * </table> |
||
| 624 | * @return BaseEvents |
||
| 625 | * @author Kaero |
||
| 626 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 627 | */ |
||
| 628 | final public function onShopProductDelete() { |
||
| 633 | |||
| 634 | final public function onShopProductFastProdCreate() { |
||
| 639 | |||
| 640 | final public function onShopProductPreCreate() { |
||
| 645 | |||
| 646 | final public function onShopProductPreFastProdCreate() { |
||
| 651 | |||
| 652 | final public function onShopProductPreUpdate() { |
||
| 657 | |||
| 658 | /** |
||
| 659 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
| 660 | * <table> |
||
| 661 | * <tr valign="top"><td>['userId']</td><td>User ID</td></tr> |
||
| 662 | * <tr valign="top"><td>['productId']</td><td>Product ID</td></tr> |
||
| 663 | * </table> |
||
| 664 | * @return BaseEvents |
||
| 665 | * @author Kaero |
||
| 666 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 667 | */ |
||
| 668 | final public function onShopProductUpdate() { |
||
| 673 | |||
| 674 | final public function onShopSearchAC() { |
||
| 679 | |||
| 680 | final public function onShopUserAfterEdit() { |
||
| 685 | |||
| 686 | final public function onShopUserBeforeEdit() { |
||
| 691 | |||
| 692 | final public function onShopUserCreate() { |
||
| 697 | |||
| 698 | final public function onWidgetHTMLPreUpdate() { |
||
| 703 | |||
| 704 | final public function onWidgetModulePreUpdate() { |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Run listeners for one event. After running removes listeners. |
||
| 712 | * @param array|object $data |
||
| 713 | * @param string $eventAlias |
||
| 714 | * @return $this |
||
| 715 | */ |
||
| 716 | public function raiseEvent($data, $eventAlias) { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Declares a new event. The method adds the general pool of information about the event and sets it as held. |
||
| 725 | * The user can call the place where, in his opinion, there is a need. |
||
| 726 | * Will be generated key that consists of a pair of "Class: method." |
||
| 727 | * @param mixed $data <b>[optional]</b>Fetch some Data and peredaje to user method's |
||
| 728 | * @param string $key |
||
| 729 | * @access public |
||
| 730 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 731 | * @return $this |
||
| 732 | */ |
||
| 733 | public function registerEvent($data = null, $key = null) { |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Run Behavior factory when controller is completely loaded |
||
| 746 | * @param string $eventAlias |
||
| 747 | * @param bool $cleanQueue |
||
| 748 | * @access public |
||
| 749 | * @author Kaero |
||
| 750 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 751 | */ |
||
| 752 | public static function runFactory($eventAlias = null, $cleanQueue = false) { |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Removes specified event with all listeners |
||
| 778 | * @param string $eventAlias |
||
| 779 | */ |
||
| 780 | public function removeEvent($eventAlias) { |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Binds a custom method to the event. |
||
| 789 | * <br/><br/><code> |
||
| 790 | * public function autoload() {<br/> |
||
| 791 | *   \CMSFactory\Events::create()->setListener('myMethod', 'Comments::add');<br/> |
||
| 792 | * } |
||
| 793 | * </code> |
||
| 794 | * @param string|array|Closure $callback Indicates the name of the method that will be called in response to a trigger-event. The method will be matched in the class from which the requested binding. |
||
| 795 | * @param string $alias <b>[optional]</b> The second parameter is optional if you make a call type was given an expected event. |
||
| 796 | * @throws Exception |
||
| 797 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
| 798 | */ |
||
| 799 | public function setListener($callback, $alias = null) { |
||
| 838 | |||
| 839 | } |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: