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 | final public function onAdminCategoryDelete() { |
||
132 | |||
133 | /** |
||
134 | * @return $this |
||
135 | */ |
||
136 | final public function onAdminCategoryPreCreate() { |
||
141 | |||
142 | final public function onAdminCategoryPreUpdate() { |
||
147 | |||
148 | /** |
||
149 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
150 | * <table> |
||
151 | * <tr valign="top"><td>['userId']</td><td>User ID</td></tr> |
||
152 | * <tr valign="top"><td>['name']</td><td>Category name</td></tr> |
||
153 | * <tr valign="top"><td>['url']</td><td>Category url</td></tr> |
||
154 | * <tr valign="top"><td>['short_desc']</td><td>Category short description</td></tr> |
||
155 | * </table> |
||
156 | * @return BaseEvents |
||
157 | * @author Kaero |
||
158 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
159 | */ |
||
160 | final public function onAdminCategoryUpdate() { |
||
165 | |||
166 | final public function onAdminDashboardShow() { |
||
171 | |||
172 | /** |
||
173 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
174 | * @return BaseEvents |
||
175 | * @author a.gula |
||
176 | * @copyright ImageCMS (c) 2014, a.gula <[email protected]> |
||
177 | */ |
||
178 | public function onAdminModulesTable() { |
||
183 | |||
184 | final public function onAdminOrderUserCreate() { |
||
189 | |||
190 | /** |
||
191 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
192 | * <table> |
||
193 | * <tr valign="top"><td>['id']</td><td>Page ID</td></tr> |
||
194 | * <tr valign="top"><td>['title']</td><td>Page title</td></tr> |
||
195 | * <tr valign="top"><td>['full_text']</td><td>Page full text</td></tr> |
||
196 | * <tr valign="top"><td>['prev_text']</td><td>Page short text</td></tr> |
||
197 | * </table> |
||
198 | * @return BaseEvents |
||
199 | * @author Kaero |
||
200 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
201 | */ |
||
202 | final public function onAdminPageCreate() { |
||
207 | |||
208 | /** |
||
209 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
210 | * <table> |
||
211 | * <tr valign="top"><td>['id']</td><td>Page ID</td></tr> |
||
212 | * <tr valign="top"><td>['title']</td><td>Page title</td></tr> |
||
213 | * <tr valign="top"><td>['full_text']</td><td>Page full text</td></tr> |
||
214 | * <tr valign="top"><td>['prev_text']</td><td>Page short text</td></tr> |
||
215 | * </table> |
||
216 | * @return BaseEvents |
||
217 | * @author Kaero |
||
218 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
219 | */ |
||
220 | final public function onAdminPageDelete() { |
||
225 | |||
226 | final public function onAdminPagePreCreate() { |
||
231 | |||
232 | final public function onAdminPagePreEdit() { |
||
237 | |||
238 | /** |
||
239 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
240 | * <table> |
||
241 | * <tr valign="top"><td>['id']</td><td>Page ID</td></tr> |
||
242 | * <tr valign="top"><td>['title']</td><td>Page title</td></tr> |
||
243 | * <tr valign="top"><td>['full_text']</td><td>Page full text</td></tr> |
||
244 | * <tr valign="top"><td>['prev_text']</td><td>Page short text</td></tr> |
||
245 | * </table> |
||
246 | * @return BaseEvents |
||
247 | * @author Kaero |
||
248 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
249 | */ |
||
250 | final public function onAdminPageUpdate() { |
||
255 | |||
256 | final public function onAuthUserRegister() { |
||
261 | |||
262 | public function onBrandPageLoad() { |
||
267 | |||
268 | public function onBrandsPageLoad() { |
||
273 | |||
274 | final public function onCartInit() { |
||
279 | |||
280 | public function onCartShowed() { |
||
285 | |||
286 | public function onActionTypeSearch() { |
||
290 | |||
291 | public function onCategoryPageLoad() { |
||
296 | |||
297 | public function onGalleryLoad() { |
||
301 | |||
302 | public function onGalleryAlbumLoad() { |
||
306 | |||
307 | public function onGalleryCategoryLoad() { |
||
311 | |||
312 | final public function onCorePageLoaded() { |
||
317 | |||
318 | final public function onFrontOrderUserCreate() { |
||
323 | |||
324 | final public function onNotifyingRequest() { |
||
329 | |||
330 | /** |
||
331 | * @return $this |
||
332 | */ |
||
333 | final public function onPageCategoryLoad() { |
||
338 | |||
339 | /** |
||
340 | * @return $this |
||
341 | */ |
||
342 | final public function onPageLoad() { |
||
347 | |||
348 | final public function onPaimentSystemSuccessPaid() { |
||
353 | |||
354 | public function onProductPageLoad() { |
||
359 | |||
360 | final public function onProfileApiChangeInfo() { |
||
365 | |||
366 | public function onRemoveFromCart() { |
||
371 | |||
372 | final public function onSearchPageLoad() { |
||
377 | |||
378 | /** |
||
379 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
380 | * @return BaseEvents |
||
381 | * @author a.gula |
||
382 | * @copyright ImageCMS (c) 2014, a.gula <[email protected]> |
||
383 | */ |
||
384 | public function onSeoExpertSave() { |
||
389 | |||
390 | final public function onShopAdminAjaxChangeOrderPaid() { |
||
395 | |||
396 | final public function onShopAdminAjaxChangeOrderStatus() { |
||
401 | |||
402 | final public function onShopAdminOrderCreate() { |
||
407 | |||
408 | final public function onShopAdminOrderDelete() { |
||
413 | |||
414 | final public function onShopAdminOrderEdit() { |
||
419 | |||
420 | /** */ |
||
421 | |||
422 | final public function onShopAdminOrderUserCreate() { |
||
427 | |||
428 | /** */ |
||
429 | /** */ |
||
430 | |||
431 | /** |
||
432 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
433 | * <table> |
||
434 | * <tr valign="top"><td>['model']</td><td>Brand model</td></tr> |
||
435 | * <tr valign="top"><td>['userId']</td><td>User Id</td></tr> |
||
436 | * </table> |
||
437 | * @return BaseEvents |
||
438 | * @author Hellmark |
||
439 | * @copyright ImageCMS (c) 2014, Hellmark <[email protected]> |
||
440 | */ |
||
441 | public function onShopBrandCreate() { |
||
446 | |||
447 | /** |
||
448 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
449 | * <table> |
||
450 | * <tr valign="top"><td>['brandId']</td><td>Brand Id</td></tr> |
||
451 | * <tr valign="top"><td>['url']</td><td>Brand Url</td></tr> |
||
452 | * </table> |
||
453 | * @return BaseEvents |
||
454 | * @author Hellmark |
||
455 | * @copyright ImageCMS (c) 2014, Hellmark <[email protected]> |
||
456 | */ |
||
457 | public function onShopBrandDelete() { |
||
462 | |||
463 | /** |
||
464 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
465 | * <table> |
||
466 | * <tr valign="top"><td>['model']</td><td>Brand model</td></tr> |
||
467 | * <tr valign="top"><td>['url']</td><td>Brand Url</td></tr> |
||
468 | * <tr valign="top"><td>['userId']</td><td>User Id</td></tr> |
||
469 | * </table> |
||
470 | * @return BaseEvents |
||
471 | * @author Hellmark |
||
472 | * @copyright ImageCMS (c) 2014, Hellmark <[email protected]> |
||
473 | */ |
||
474 | public function onShopBrandEdit() { |
||
479 | |||
480 | /** |
||
481 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
482 | * @return BaseEvents |
||
483 | * @author a.gula |
||
484 | * @copyright ImageCMS (c) 2014, a.gula <[email protected]> |
||
485 | */ |
||
486 | public function onShopBrandPreCreate() { |
||
491 | |||
492 | /** |
||
493 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
494 | * <table> |
||
495 | * <tr valign="top"><td>['model']</td><td>Brand model</td></tr> |
||
496 | * <tr valign="top"><td>['url']</td><td>Brand Url</td></tr> |
||
497 | * <tr valign="top"><td>['userId']</td><td>User Id</td></tr> |
||
498 | * </table> |
||
499 | * @return BaseEvents |
||
500 | * @author Hellmark |
||
501 | * @copyright ImageCMS (c) 2014, Hellmark <[email protected]> |
||
502 | */ |
||
503 | public function onShopBrandPreEdit() { |
||
508 | |||
509 | final public function onShopCallback() { |
||
514 | |||
515 | /** |
||
516 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
517 | * <table> |
||
518 | * <tr valign="top"><td>['ShopCategoryId']</td><td>Category ID</td></tr> |
||
519 | * </table> |
||
520 | * @return BaseEvents |
||
521 | * @author Kaero |
||
522 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
523 | */ |
||
524 | public function onShopCategoryCreate() { |
||
529 | |||
530 | /** |
||
531 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
532 | * <table> |
||
533 | * <tr valign="top"><td>['ShopCategoryId']</td><td>Category ID</td></tr> |
||
534 | * </table> |
||
535 | * @return BaseEvents |
||
536 | * @author Kaero |
||
537 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
538 | */ |
||
539 | public function onShopCategoryDelete() { |
||
544 | |||
545 | /** |
||
546 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
547 | * <table> |
||
548 | * <tr valign="top"><td>['ShopCategoryId']</td><td>Category ID</td></tr> |
||
549 | * </table> |
||
550 | * @return BaseEvents |
||
551 | * @author Kaero |
||
552 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
553 | */ |
||
554 | public function onShopCategoryEdit() { |
||
559 | |||
560 | public function onShopCategoryFastCreate() { |
||
565 | |||
566 | public function onShopCategoryPreCreate() { |
||
571 | |||
572 | public function onShopCategoryPreEdit() { |
||
577 | |||
578 | public function onShopCategoryPreFastCreate() { |
||
583 | |||
584 | final public function onShopCategoryAjaxChangeShowInSite() { |
||
589 | |||
590 | final public function onShopDashboardShow() { |
||
595 | |||
596 | final public function onShopMakeOrder() { |
||
601 | |||
602 | final public function onShopOrderView() { |
||
607 | |||
608 | final public function onShopProductAjaxChangeActive() { |
||
613 | |||
614 | /** |
||
615 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
616 | * <table> |
||
617 | * <tr valign="top"><td>['userId']</td><td>User ID</td></tr> |
||
618 | * <tr valign="top"><td>['productId']</td><td>Product ID</td></tr> |
||
619 | * </table> |
||
620 | * @return BaseEvents |
||
621 | * @author Kaero |
||
622 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
623 | */ |
||
624 | final public function onShopProductCreate() { |
||
629 | |||
630 | /** |
||
631 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
632 | * <table> |
||
633 | * <tr valign="top"><td>['userId']</td><td>User ID</td></tr> |
||
634 | * <tr valign="top"><td>['model']</td><td></td>Instanceof SProducts</tr> |
||
635 | * </table> |
||
636 | * @return BaseEvents |
||
637 | * @author Kaero |
||
638 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
639 | */ |
||
640 | final public function onShopProductDelete() { |
||
645 | |||
646 | final public function onShopProductFastProdCreate() { |
||
651 | |||
652 | final public function onShopProductPreCreate() { |
||
657 | |||
658 | final public function onShopProductPreFastProdCreate() { |
||
663 | |||
664 | final public function onShopProductPreUpdate() { |
||
669 | |||
670 | /** |
||
671 | * <p>The possible returned elements from <b>setListener</b> are as follows:</p> |
||
672 | * <table> |
||
673 | * <tr valign="top"><td>['userId']</td><td>User ID</td></tr> |
||
674 | * <tr valign="top"><td>['productId']</td><td>Product ID</td></tr> |
||
675 | * </table> |
||
676 | * @return BaseEvents |
||
677 | * @author Kaero |
||
678 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
679 | */ |
||
680 | final public function onShopProductUpdate() { |
||
685 | |||
686 | final public function onShopSearchAC() { |
||
691 | |||
692 | final public function onShopUserAfterEdit() { |
||
697 | |||
698 | final public function onShopUserBeforeEdit() { |
||
703 | |||
704 | final public function onShopUserCreate() { |
||
709 | |||
710 | final public function onWidgetHTMLPreUpdate() { |
||
715 | |||
716 | final public function onWidgetModulePreUpdate() { |
||
721 | |||
722 | /** |
||
723 | * @return $this |
||
724 | */ |
||
725 | final public function onMakeCurrencyMain() { |
||
730 | |||
731 | /** |
||
732 | * Run listeners for one event. After running removes listeners. |
||
733 | * @param array|object $data |
||
734 | * @param string $eventAlias |
||
735 | * @return $this |
||
736 | */ |
||
737 | public function raiseEvent($data, $eventAlias) { |
||
743 | |||
744 | /** |
||
745 | * Declares a new event. The method adds the general pool of information about the event and sets it as held. |
||
746 | * The user can call the place where, in his opinion, there is a need. |
||
747 | * Will be generated key that consists of a pair of "Class: method." |
||
748 | * @param mixed $data <b>[optional]</b>Fetch some Data and peredaje to user method's |
||
749 | * @param string $key |
||
750 | * @access public |
||
751 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
752 | * @return $this |
||
753 | */ |
||
754 | public function registerEvent($data = null, $key = null) { |
||
764 | |||
765 | /** |
||
766 | * Run Behavior factory when controller is completely loaded |
||
767 | * @param string $eventAlias |
||
768 | * @param bool $cleanQueue |
||
769 | * @access public |
||
770 | * @author Kaero |
||
771 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
772 | */ |
||
773 | public static function runFactory($eventAlias = null, $cleanQueue = false) { |
||
797 | |||
798 | /** |
||
799 | * Removes specified event with all listeners |
||
800 | * @param string $eventAlias |
||
801 | */ |
||
802 | public function removeEvent($eventAlias) { |
||
808 | |||
809 | /** |
||
810 | * Binds a custom method to the event. |
||
811 | * <br/><br/><code> |
||
812 | * public function autoload() {<br/> |
||
813 | *   \CMSFactory\Events::create()->setListener('myMethod', 'Comments::add');<br/> |
||
814 | * } |
||
815 | * </code> |
||
816 | * @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. |
||
817 | * @param string $alias <b>[optional]</b> The second parameter is optional if you make a call type was given an expected event. |
||
818 | * @throws Exception |
||
819 | * @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
||
820 | */ |
||
821 | public function setListener($callback, $alias = null) { |
||
860 | |||
861 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: