Complex classes like GLOSSARY 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 GLOSSARY, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class GLOSSARY { |
||
| 27 | |||
| 28 | public $num_terms; // how many glossary entries do we have |
||
| 29 | // (changes depending on how GLOSSARY is called |
||
| 30 | public $hansard_count; // how many times does the phrase appear in hansard? |
||
| 31 | public $query; // search term |
||
| 32 | public $glossary_id; // if this is set then we only have 1 glossary term |
||
| 33 | public $current_term; // will only be set if we have a valid epobject_id |
||
| 34 | public $current_letter; |
||
| 35 | |||
| 36 | // constructor... |
||
| 37 | 8 | public function __construct($args=array()) { |
|
| 77 | |||
| 78 | 8 | } |
|
| 79 | |||
| 80 | 8 | public function get_glossary_item($args=array()) { |
|
| 81 | // Search for and fetch glossary item with title or glossary_id |
||
| 82 | // We could also search glossary text that contains the title text, for cross references |
||
| 83 | |||
| 84 | 8 | $this->alphabet = array(); |
|
| 85 | 8 | foreach (range ("A", "Z") as $letter) { |
|
| 86 | 8 | $this->alphabet[$letter] = array(); |
|
| 87 | 8 | } |
|
| 88 | |||
| 89 | 8 | $q = $this->db->query("SELECT g.glossary_id, g.title, g.body, u.user_id, u.firstname, u.lastname |
|
| 90 | FROM editqueue AS eq, glossary AS g, users AS u |
||
| 91 | WHERE g.glossary_id=eq.glossary_id AND u.user_id=eq.user_id AND g.visible=1 AND eq.approved=1 |
||
| 92 | 8 | ORDER by g.title"); |
|
| 93 | 8 | if ($q->success() && $q->rows()) { |
|
| 94 | 8 | for ($i=0; $i < $q->rows(); $i++) { |
|
| 95 | 8 | $this->terms[ $q->field($i,"glossary_id") ] = $q->row($i); |
|
| 96 | // Now add the epobject to the alphabet navigation. |
||
| 97 | 8 | $first_letter = strtoupper(substr($q->field($i,"title"),0,1)); |
|
| 98 | 8 | $this->alphabet[$first_letter][] = $q->field($i,"glossary_id"); |
|
| 99 | 8 | } |
|
| 100 | |||
| 101 | 8 | $this->num_terms = $q->rows(); |
|
| 102 | |||
| 103 | // If we were given a glossary_id, then we need one term in particular, |
||
| 104 | // as well as knowing the next and previous terms for the navigation |
||
| 105 | 8 | if (isset($args['glossary_id']) && ($args['glossary_id'] != "")) { |
|
| 106 | $next = 0; $first_term = null; |
||
| 107 | foreach ($this->terms as $term) { |
||
| 108 | if (!$first_term) $first_term = $term; |
||
| 109 | $last_term = $term; |
||
| 110 | if ($next == 1) { |
||
| 111 | $this->next_term = $term; |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | elseif ($term['glossary_id'] == $args['glossary_id']) { |
||
| 115 | $this->glossary_id = $args['glossary_id']; |
||
| 116 | $this->current_term = $term; |
||
| 117 | $next = 1; |
||
| 118 | |||
| 119 | } |
||
| 120 | else { |
||
| 121 | $this->previous_term = $term; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | // The first term in the list has no previous, so we'll make it the last term |
||
| 125 | if (!isset($this->previous_term)) { |
||
| 126 | $this->previous_term = $last_term; |
||
| 127 | } |
||
| 128 | // and the last has no next, so we'll make it the first |
||
| 129 | if (!isset($this->next_term)) { |
||
| 130 | $this->next_term = $first_term; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | 8 | return ($this->num_terms); |
|
| 135 | } |
||
| 136 | else { |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | public function search_glossary($args=array()) { |
||
| 142 | // Search for and fetch glossary item with a title |
||
| 143 | // Useful for the search page, and nowhere else (so far) |
||
| 144 | |||
| 145 | $this->query = addslashes($args['s']); |
||
| 146 | $this->search_matches = array(); |
||
| 147 | $this->num_search_matches = 0; |
||
| 148 | |||
| 149 | $query = "SELECT g.glossary_id, g.title, g.body, u.user_id, u.firstname, u.lastname |
||
| 150 | FROM editqueue AS eq, glossary AS g, users AS u |
||
| 151 | WHERE g.glossary_id=eq.glossary_id AND u.user_id=eq.user_id AND g.visible=1 |
||
| 152 | AND g.title LIKE '%" . $this->query . "%' |
||
| 153 | ORDER by g.title"; |
||
| 154 | $q = $this->db->query($query); |
||
| 155 | if ($q->success() && $q->rows()) { |
||
| 156 | for ($i=0; $i < $q->rows(); $i++) { |
||
| 157 | $this->search_matches[ $q->field($i,"glossary_id") ] = $q->row($i); |
||
| 158 | } |
||
| 159 | $this->num_search_matches = $q->rows(); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | public function create(&$data) { |
||
| 164 | // Add a Glossary definition. |
||
| 165 | // Sets visiblity to 0, and awaits moderator intervention. |
||
| 166 | // For this we need to start up an epobject of type 2 and then an editqueue item |
||
| 167 | // where editqueue.epobject_id_l = epobject.epobject_id |
||
| 168 | |||
| 169 | $EDITQUEUE = new \MySociety\TheyWorkForYou\GlossaryEditQueue; |
||
| 170 | |||
| 171 | // Assuming that everything is ok, we will need: |
||
| 172 | // For epobject: |
||
| 173 | // title VARCHAR(255), |
||
| 174 | // body TEXT, |
||
| 175 | // type INTEGER, |
||
| 176 | // created DATETIME, |
||
| 177 | // modified DATETIME, |
||
| 178 | // and for editqueue: |
||
| 179 | // edit_id INTEGER PRIMARY KEY NOT NULL, |
||
| 180 | // user_id INTEGER, |
||
| 181 | // edit_type INTEGER, |
||
| 182 | // epobject_id_l INTEGER, |
||
| 183 | // title VARCHAR(255), |
||
| 184 | // body TEXT, |
||
| 185 | // submitted DATETIME, |
||
| 186 | // editor_id INTEGER, |
||
| 187 | // approved BOOLEAN, |
||
| 188 | // decided DATETIME |
||
| 189 | |||
| 190 | global $THEUSER; |
||
| 191 | |||
| 192 | if ($data['title'] == '') { |
||
| 193 | error ("Sorry, you can't define a term without a title"); |
||
| 194 | return false; |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($data['body'] == '') { |
||
| 198 | error ("You haven't entered a definition!"); |
||
| 199 | return false; |
||
| 200 | } |
||
| 201 | |||
| 202 | if (is_numeric($THEUSER->user_id())) { |
||
| 203 | // Flood check - make sure the user hasn't just posted a term recently. |
||
| 204 | // To help prevent accidental duplicates, among other nasty things. |
||
| 205 | |||
| 206 | $flood_time_limit = 20; // How many seconds until a user can post again? |
||
| 207 | |||
| 208 | $q = $this->db->query("SELECT glossary_id |
||
| 209 | FROM editqueue |
||
| 210 | WHERE user_id = '" . $THEUSER->user_id() . "' |
||
| 211 | AND submitted + 0 > NOW() - $flood_time_limit"); |
||
| 212 | |||
| 213 | if ($q->rows() > 0) { |
||
| 214 | error("Sorry, we limit people to posting one term per $flood_time_limit seconds to help prevent duplicate postings. Please go back and try again, thanks."); |
||
| 215 | return false; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | // OK, let's get on with it... |
||
| 220 | |||
| 221 | // Tidy up the HTML tags |
||
| 222 | // (but we don't make URLs into links; only when displaying the comment). |
||
| 223 | // We can display Glossary terms the same as the comments |
||
| 224 | $data['title'] = filter_user_input($data['title'], 'comment_title'); // In utility.php |
||
| 225 | $data['body'] = filter_user_input($data['body'], 'comment'); // In utility.php |
||
| 226 | // Add the time and the edit type for the editqueue |
||
| 227 | $data['posted'] = date('Y-m-d H:i:s', time()); |
||
| 228 | $data['edit_type'] = 2; |
||
| 229 | |||
| 230 | // Add the item to the edit queue |
||
| 231 | $success = $EDITQUEUE->add($data); |
||
| 232 | |||
| 233 | if ($success) { |
||
| 234 | return ($success); |
||
| 235 | } else { |
||
| 236 | return false; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | public function delete($glossary_id) |
||
| 241 | { |
||
| 242 | $q = $this->db->query("DELETE from glossary where glossary_id=$glossary_id LIMIT 1;"); |
||
| 243 | // if that worked, we need to update the editqueue, |
||
| 244 | // and remove the term from the already generated object list. |
||
| 245 | if ($q->affected_rows() >= 1) { |
||
| 246 | unset($this->replace_order[$glossary_id]); |
||
| 247 | unset($this->terms[$glossary_id]); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | 8 | public function glossarise($body, $tokenize=0, $urlize=0) { |
|
| 314 | } |
||
| 315 | |||
| 316 | } |
||
| 317 |
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.