Complex classes like Subscription 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 Subscription, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Subscription { |
||
13 | |||
14 | /** |
||
15 | * Check if subscription system is enabled |
||
16 | * |
||
17 | * @return bool |
||
18 | */ |
||
19 | public function isenabled() { |
||
22 | |||
23 | /** |
||
24 | * Return the subscription meta file for the given ID |
||
25 | * |
||
26 | * @author Adrian Lang <[email protected]> |
||
27 | * |
||
28 | * @param string $id The target page or namespace, specified by id; Namespaces |
||
29 | * are identified by appending a colon. |
||
30 | * @return string |
||
31 | */ |
||
32 | protected function file($id) { |
||
42 | |||
43 | /** |
||
44 | * Lock subscription info |
||
45 | * |
||
46 | * We don't use io_lock() her because we do not wait for the lock and use a larger stale time |
||
47 | * |
||
48 | * @author Adrian Lang <[email protected]> |
||
49 | * @param string $id The target page or namespace, specified by id; Namespaces |
||
50 | * are identified by appending a colon. |
||
51 | * @return bool true, if you got a succesful lock |
||
52 | */ |
||
53 | protected function lock($id) { |
||
71 | |||
72 | /** |
||
73 | * Unlock subscription info |
||
74 | * |
||
75 | * @author Adrian Lang <[email protected]> |
||
76 | * @param string $id The target page or namespace, specified by id; Namespaces |
||
77 | * are identified by appending a colon. |
||
78 | * @return bool |
||
79 | */ |
||
80 | protected function unlock($id) { |
||
85 | |||
86 | /** |
||
87 | * Construct a regular expression for parsing a subscription definition line |
||
88 | * |
||
89 | * @author Andreas Gohr <[email protected]> |
||
90 | * |
||
91 | * @param string|array $user |
||
92 | * @param string|array $style |
||
93 | * @param string|array $data |
||
94 | * @return string complete regexp including delimiters |
||
95 | * @throws Exception when no data is passed |
||
96 | */ |
||
97 | protected function buildregex($user = null, $style = null, $data = null) { |
||
142 | |||
143 | /** |
||
144 | * Recursively search for matching subscriptions |
||
145 | * |
||
146 | * This function searches all relevant subscription files for a page or |
||
147 | * namespace. |
||
148 | * |
||
149 | * @author Adrian Lang <[email protected]> |
||
150 | * |
||
151 | * @param string $page The target object’s (namespace or page) id |
||
152 | * @param string|array $user |
||
153 | * @param string|array $style |
||
154 | * @param string|array $data |
||
155 | * @return array |
||
156 | */ |
||
157 | public function subscribers($page, $user = null, $style = null, $data = null) { |
||
189 | |||
190 | /** |
||
191 | * Adds a new subscription for the given page or namespace |
||
192 | * |
||
193 | * This will automatically overwrite any existent subscription for the given user on this |
||
194 | * *exact* page or namespace. It will *not* modify any subscription that may exist in higher namespaces. |
||
195 | * |
||
196 | * @param string $id The target page or namespace, specified by id; Namespaces |
||
197 | * are identified by appending a colon. |
||
198 | * @param string $user |
||
199 | * @param string $style |
||
200 | * @param string $data |
||
201 | * @throws Exception when user or style is empty |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function add($id, $user, $style, $data = '') { |
||
222 | |||
223 | /** |
||
224 | * Removes a subscription for the given page or namespace |
||
225 | * |
||
226 | * This removes all subscriptions matching the given criteria on the given page or |
||
227 | * namespace. It will *not* modify any subscriptions that may exist in higher |
||
228 | * namespaces. |
||
229 | * |
||
230 | * @param string $id The target object’s (namespace or page) id |
||
231 | * @param string|array $user |
||
232 | * @param string|array $style |
||
233 | * @param string|array $data |
||
234 | * @return bool |
||
235 | */ |
||
236 | public function remove($id, $user = null, $style = null, $data = null) { |
||
245 | |||
246 | /** |
||
247 | * Get data for $INFO['subscribed'] |
||
248 | * |
||
249 | * $INFO['subscribed'] is either false if no subscription for the current page |
||
250 | * and user is in effect. Else it contains an array of arrays with the fields |
||
251 | * “target”, “style”, and optionally “data”. |
||
252 | * |
||
253 | * @param string $id Page ID, defaults to global $ID |
||
254 | * @param string $user User, defaults to $_SERVER['REMOTE_USER'] |
||
255 | * @return array|false |
||
256 | * @author Adrian Lang <[email protected]> |
||
257 | */ |
||
258 | public function user_subscription($id = '', $user = '') { |
||
281 | |||
282 | /** |
||
283 | * Send digest and list subscriptions |
||
284 | * |
||
285 | * This sends mails to all subscribers that have a subscription for namespaces above |
||
286 | * the given page if the needed $conf['subscribe_time'] has passed already. |
||
287 | * |
||
288 | * This function is called form lib/exe/indexer.php |
||
289 | * |
||
290 | * @param string $page |
||
291 | * @return int number of sent mails |
||
292 | */ |
||
293 | public function send_bulk($page) { |
||
383 | |||
384 | /** |
||
385 | * Send the diff for some page change |
||
386 | * |
||
387 | * @param string $subscriber_mail The target mail address |
||
388 | * @param string $template Mail template ('subscr_digest', 'subscr_single', 'mailtext', ...) |
||
389 | * @param string $id Page for which the notification is |
||
390 | * @param int|null $rev Old revision if any |
||
391 | * @param string $summary Change summary if any |
||
392 | * @return bool true if successfully sent |
||
393 | */ |
||
394 | public function send_diff($subscriber_mail, $template, $id, $rev = null, $summary = '') { |
||
445 | |||
446 | /** |
||
447 | * Send the diff for some media change |
||
448 | * |
||
449 | * @fixme this should embed thumbnails of images in HTML version |
||
450 | * |
||
451 | * @param string $subscriber_mail The target mail address |
||
452 | * @param string $template Mail template ('uploadmail', ...) |
||
453 | * @param string $id Media file for which the notification is |
||
454 | * @param int|bool $rev Old revision if any |
||
455 | */ |
||
456 | public function send_media_diff($subscriber_mail, $template, $id, $rev = false) { |
||
482 | |||
483 | /** |
||
484 | * Send a notify mail on new registration |
||
485 | * |
||
486 | * @author Andreas Gohr <[email protected]> |
||
487 | * |
||
488 | * @param string $login login name of the new user |
||
489 | * @param string $fullname full name of the new user |
||
490 | * @param string $email email address of the new user |
||
491 | * @return bool true if a mail was sent |
||
492 | */ |
||
493 | public function send_register($login, $fullname, $email) { |
||
511 | |||
512 | /** |
||
513 | * Send a digest mail |
||
514 | * |
||
515 | * Sends a digest mail showing a bunch of changes of a single page. Basically the same as send_diff() |
||
516 | * but determines the last known revision first |
||
517 | * |
||
518 | * @author Adrian Lang <[email protected]> |
||
519 | * |
||
520 | * @param string $subscriber_mail The target mail address |
||
521 | * @param string $id The ID |
||
522 | * @param int $lastupdate Time of the last notification |
||
523 | * @return bool |
||
524 | */ |
||
525 | protected function send_digest($subscriber_mail, $id, $lastupdate) { |
||
539 | |||
540 | /** |
||
541 | * Send a list mail |
||
542 | * |
||
543 | * Sends a list mail showing a list of changed pages. |
||
544 | * |
||
545 | * @author Adrian Lang <[email protected]> |
||
546 | * |
||
547 | * @param string $subscriber_mail The target mail address |
||
548 | * @param array $ids Array of ids |
||
549 | * @param string $ns_id The id of the namespace |
||
550 | * @return bool true if a mail was sent |
||
551 | */ |
||
552 | protected function send_list($subscriber_mail, $ids, $ns_id) { |
||
581 | |||
582 | /** |
||
583 | * Helper function for sending a mail |
||
584 | * |
||
585 | * @author Adrian Lang <[email protected]> |
||
586 | * |
||
587 | * @param string $subscriber_mail The target mail address |
||
588 | * @param string $subject The lang id of the mail subject (without the |
||
589 | * prefix “mail_”) |
||
590 | * @param string $context The context of this mail, eg. page or namespace id |
||
591 | * @param string $template The name of the mail template |
||
592 | * @param array $trep Predefined parameters used to parse the |
||
593 | * template (in text format) |
||
594 | * @param array $hrep Predefined parameters used to parse the |
||
595 | * template (in HTML format), null to default to $trep |
||
596 | * @param array $headers Additional mail headers in the form 'name' => 'value' |
||
597 | * @return bool |
||
598 | */ |
||
599 | protected function send($subscriber_mail, $subject, $context, $template, $trep, $hrep = null, $headers = array()) { |
||
622 | |||
623 | /** |
||
624 | * Get a valid message id for a certain $id and revision (or the current revision) |
||
625 | * |
||
626 | * @param string $id The id of the page (or media file) the message id should be for |
||
627 | * @param string $rev The revision of the page, set to the current revision of the page $id if not set |
||
628 | * @return string |
||
629 | */ |
||
630 | protected function getMessageID($id, $rev = null) { |
||
645 | |||
646 | /** |
||
647 | * Default callback for COMMON_NOTIFY_ADDRESSLIST |
||
648 | * |
||
649 | * Aggregates all email addresses of user who have subscribed the given page with 'every' style |
||
650 | * |
||
651 | * @author Steven Danz <[email protected]> |
||
652 | * @author Adrian Lang <[email protected]> |
||
653 | * |
||
654 | * @todo move the whole functionality into this class, trigger SUBSCRIPTION_NOTIFY_ADDRESSLIST instead, |
||
655 | * use an array for the addresses within it |
||
656 | * |
||
657 | * @param array &$data Containing the entries: |
||
658 | * - $id (the page id), |
||
659 | * - $self (whether the author should be notified, |
||
660 | * - $addresslist (current email address list) |
||
661 | * - $replacements (array of additional string substitutions, @KEY@ to be replaced by value) |
||
662 | */ |
||
663 | public function notifyaddresses(&$data) { |
||
696 | } |
||
697 |
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: