Complex classes like XoopsGTicket 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 XoopsGTicket, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class XoopsGTicket |
||
10 | { |
||
11 | public $_errors = array(); |
||
12 | public $_latest_token = ''; |
||
13 | |||
14 | // render form as plain html |
||
15 | /** |
||
16 | * @param string $salt |
||
17 | * @param int $timeout |
||
18 | * @param string $area |
||
19 | * @return string |
||
20 | */ |
||
21 | public function getTicketHtml($salt = '', $timeout = 1800, $area = '') |
||
25 | |||
26 | // returns an object of XoopsFormHidden including theh ticket |
||
27 | /** |
||
28 | * @param string $salt |
||
29 | * @param int $timeout |
||
30 | * @param string $area |
||
31 | * @return XoopsFormHidden |
||
32 | */ |
||
33 | public function getTicketXoopsForm($salt = '', $timeout = 1800, $area = '') |
||
37 | |||
38 | // add a ticket as Hidden Element into XoopsForm |
||
39 | /** |
||
40 | * @param $form |
||
41 | * @param string $salt |
||
42 | * @param int $timeout |
||
43 | * @param string $area |
||
44 | */ |
||
45 | public function addTicketXoopsFormElement(&$form, $salt = '', $timeout = 1800, $area = '') |
||
49 | |||
50 | // returns an array for xoops_confirm() ; |
||
51 | /** |
||
52 | * @param string $salt |
||
53 | * @param int $timeout |
||
54 | * @param string $area |
||
55 | * @return array |
||
56 | */ |
||
57 | public function getTicketArray($salt = '', $timeout = 1800, $area = '') |
||
61 | |||
62 | // return GET parameter string. |
||
63 | /** |
||
64 | * @param string $salt |
||
65 | * @param bool $noamp |
||
66 | * @param int $timeout |
||
67 | * @param string $area |
||
68 | * @return string |
||
69 | */ |
||
70 | public function getTicketParamString($salt = '', $noamp = false, $timeout = 1800, $area = '') |
||
74 | |||
75 | // issue a ticket |
||
76 | /** |
||
77 | * @param string $salt |
||
78 | * @param int $timeout |
||
79 | * @param string $area |
||
80 | * @return string |
||
81 | */ |
||
82 | public function issue($salt = '', $timeout = 1800, $area = '') |
||
122 | |||
123 | // check a ticket |
||
124 | /** |
||
125 | * @param bool $post |
||
126 | * @param string $area |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function check($post = true, $area = '') |
||
215 | |||
216 | // clear all stubs |
||
217 | public function clear() |
||
221 | |||
222 | // Ticket Using |
||
223 | /** |
||
224 | * @return bool |
||
225 | */ |
||
226 | public function using() |
||
234 | |||
235 | // return errors |
||
236 | /** |
||
237 | * @param bool $ashtml |
||
238 | * @return array|string |
||
239 | */ |
||
240 | public function getErrors($ashtml = true) |
||
253 | |||
254 | // end of class |
||
255 | } |
||
256 | |||
286 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.