| Total Complexity | 54 |
| Total Lines | 297 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Normal 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.
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 Normal, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class Normal extends \MySociety\TheyWorkForYou\Search { |
||
| 7 | |||
| 8 | private function get_sort_args() { |
||
| 9 | $pagenum = get_http_var('p'); |
||
| 10 | if (!is_numeric($pagenum)) { |
||
| 11 | $pagenum = 1; |
||
| 12 | } |
||
| 13 | |||
| 14 | $o = get_http_var('o'); |
||
| 15 | $args = array ( |
||
| 16 | 's' => $this->searchstring, |
||
| 17 | 'p' => $pagenum, |
||
| 18 | 'num' => get_http_var('num'), |
||
| 19 | 'pop' => get_http_var('pop'), |
||
| 20 | 'o' => ($o=='d' || $o=='r' || $o=='o') ? $o : 'd', |
||
| 21 | ); |
||
| 22 | |||
| 23 | return $args; |
||
| 24 | } |
||
| 25 | |||
| 26 | private function get_first_page_data($args) { |
||
| 27 | $members = null; |
||
| 28 | $cons = null; |
||
| 29 | $glossary = null; |
||
| 30 | |||
| 31 | $mp_types = array(); |
||
| 32 | if ($args['p'] == 1 && $args['s'] && !preg_match('#[a-z]+:[a-z0-9]+#', $args['s'])) { |
||
| 33 | $members = $this->find_members(); |
||
| 34 | list($cons, $mp_types) = $this->find_constituency($args); |
||
| 35 | $glossary = $this->find_glossary_items($args); |
||
| 36 | } |
||
| 37 | |||
| 38 | return array($members, $cons, $mp_types, $glossary); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function search($searchstring) { |
||
| 42 | global $DATA, $this_page, $SEARCHENGINE; |
||
|
|
|||
| 43 | |||
| 44 | $this->searchstring = $searchstring; |
||
| 45 | $SEARCHENGINE = new \SEARCHENGINE($this->searchstring); |
||
| 46 | |||
| 47 | $args = $this->get_sort_args(); |
||
| 48 | |||
| 49 | $pagenum = $args['p']; |
||
| 50 | |||
| 51 | $DATA->set_page_metadata($this_page, 'rss', '/search/rss/?s=' . urlencode($this->searchstring)); |
||
| 52 | if ($pagenum == 1) { |
||
| 53 | # Allow indexing of first page of search results |
||
| 54 | $DATA->set_page_metadata($this_page, 'robots', ''); |
||
| 55 | } |
||
| 56 | |||
| 57 | $sort_order = 'newest'; |
||
| 58 | if ( $args['o'] == 'o' ) { |
||
| 59 | $sort_order = 'oldest'; |
||
| 60 | } else if ( $args['o'] == 'r' ) { |
||
| 61 | $sort_order = 'relevance'; |
||
| 62 | } |
||
| 63 | |||
| 64 | list($members, $cons, $mp_types, $glossary) = $this->get_first_page_data($args); |
||
| 65 | |||
| 66 | if (!defined('FRONT_END_SEARCH') || !FRONT_END_SEARCH) { |
||
|
1 ignored issue
–
show
|
|||
| 67 | return array( |
||
| 68 | 'error' =>'Apologies, search has been turned off currently for performance reasons.' |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (!$SEARCHENGINE->valid) { |
||
| 73 | return array('error' => $SEARCHENGINE->error); |
||
| 74 | } else { |
||
| 75 | $LIST = new \HANSARDLIST(); |
||
| 76 | $data = $LIST->display('search', $args , 'none'); |
||
| 77 | $data['search_type'] = 'normal'; |
||
| 78 | $data['sort_order'] = $sort_order; |
||
| 79 | $data['members'] = $members; |
||
| 80 | $data['cons'] = $cons; |
||
| 81 | $data['mp_types'] = $mp_types; |
||
| 82 | $data['glossary'] = $glossary; |
||
| 83 | $data['pagination_links'] = $this->generate_pagination($data['info']); |
||
| 84 | $data['search_sidebar'] = $this->get_sidebar_links($this->searchstring); |
||
| 85 | return $data; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | private function find_constituency($args) { |
||
| 140 | } |
||
| 141 | |||
| 142 | private function find_members() { |
||
| 143 | $searchstring = trim(preg_replace('#-?[a-z]+:[a-z0-9]+#', '', $this->searchstring)); |
||
| 144 | if (!$searchstring) return array(); |
||
| 145 | |||
| 146 | $members = array(); |
||
| 147 | |||
| 148 | $q = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup($searchstring); |
||
| 149 | $row_count = $q->rows(); |
||
| 150 | for ($n=0; $n<$row_count; $n++) { |
||
| 151 | $members[] = $q->field($n, 'person_id'); |
||
| 152 | } |
||
| 153 | |||
| 154 | $db = new \ParlDB; |
||
| 155 | $q = $db->query("SELECT person FROM moffice WHERE position LIKE :pos ORDER BY from_date DESC, moffice_id", |
||
| 156 | array('pos' => "%$searchstring%")); |
||
| 157 | $row_count = $q->rows(); |
||
| 158 | for ($n=0; $n<$row_count; $n++) { |
||
| 159 | $members[] = $q->field($n, 'person'); |
||
| 160 | } |
||
| 161 | |||
| 162 | // We might have duplicates, so get rid of them |
||
| 163 | $members = array_unique($members); |
||
| 164 | |||
| 165 | foreach ($members as $i => $pid) { |
||
| 166 | $member = new \MySociety\TheyWorkForYou\Member(array('person_id' => $pid)); |
||
| 167 | $members[$i] = $member; |
||
| 168 | } |
||
| 169 | return $members; |
||
| 170 | } |
||
| 171 | |||
| 172 | private function find_glossary_items($args) { |
||
| 173 | $GLOSSARY = new \GLOSSARY($args); |
||
| 174 | $items = array(); |
||
| 175 | |||
| 176 | if (isset($GLOSSARY->num_search_matches) && $GLOSSARY->num_search_matches >= 1) { |
||
| 177 | $URL = new \MySociety\TheyWorkForYou\Url('glossary'); |
||
| 178 | $URL->insert(array('gl' => "")); |
||
| 179 | foreach ($GLOSSARY->search_matches as $glossary_id => $term) { |
||
| 180 | $URL->update(array("gl" => $glossary_id)); |
||
| 181 | $items[] = array( |
||
| 182 | 'url' => $URL->generate(), |
||
| 183 | 'term' => $term['title'], |
||
| 184 | 'body' => $term['body'] |
||
| 185 | ); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | return $items; |
||
| 189 | } |
||
| 190 | |||
| 191 | private function generate_pagination_links($data, $url, $first, $last) { |
||
| 192 | $links = array(); |
||
| 193 | |||
| 194 | for ($n = $first; $n <= $last; $n++) { |
||
| 195 | |||
| 196 | if ($n > 1) { |
||
| 197 | $url->insert(array('p'=>$n)); |
||
| 198 | } else { |
||
| 199 | // No page number for the first page. |
||
| 200 | $url->remove(array('p')); |
||
| 201 | } |
||
| 202 | |||
| 203 | $link = array( |
||
| 204 | 'url' => $url->generate(), |
||
| 205 | 'page' => $n, |
||
| 206 | 'current' => ( $n == $data['page'] ) |
||
| 207 | ); |
||
| 208 | |||
| 209 | $links[] = $link; |
||
| 210 | } |
||
| 211 | |||
| 212 | return $links; |
||
| 213 | } |
||
| 214 | |||
| 215 | private function generate_pagination($data) { |
||
| 275 | } |
||
| 276 | |||
| 277 | private function get_sidebar_links() { |
||
| 278 | global $DATA, $SEARCHENGINE, $this_page; |
||
| 279 | |||
| 280 | $links = array(); |
||
| 303 | } |
||
| 304 | } |
||
| 305 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state