| Total Complexity | 171 |
| Total Lines | 672 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
Complex classes like Standard 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 Standard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Standard extends \MySociety\TheyWorkForYou\AlertView { |
||
| 12 | public $data; |
||
| 13 | |||
| 14 | public function __construct($THEUSER = null) { |
||
| 17 | } |
||
| 18 | |||
| 19 | public function display() { |
||
| 40 | } |
||
| 41 | |||
| 42 | # This only happens if we have an alert and want to do something to it. |
||
| 43 | private function processAction() { |
||
| 86 | } |
||
| 87 | |||
| 88 | # Process a screen in the alert creation wizard |
||
| 89 | private function processStep() { |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | |||
| 133 | private function getBasicData() { |
||
| 134 | global $this_page; |
||
| 135 | |||
| 136 | if ($this->user->loggedin()) { |
||
| 137 | $this->data['email'] = $this->user->email(); |
||
| 138 | $this->data['email_verified'] = true; |
||
| 139 | } elseif ($this->data['alert']) { |
||
| 140 | $this->data['email'] = $this->data['alert']['email']; |
||
| 141 | $this->data['email_verified'] = true; |
||
| 142 | } else { |
||
| 143 | $this->data["email"] = trim(get_http_var("email")); |
||
| 144 | $this->data['email_verified'] = false; |
||
| 145 | } |
||
| 146 | |||
| 147 | $this->data['token'] = get_http_var('t'); |
||
| 148 | $this->data['step'] = trim(get_http_var("step")); |
||
| 149 | $this->data['mp_step'] = trim(get_http_var("mp_step")); |
||
| 150 | $this->data['addword'] = trim(get_http_var("addword")); |
||
| 151 | $this->data['this_step'] = trim(get_http_var("this_step")); |
||
| 152 | $this->data['shown_related'] = get_http_var('shown_related'); |
||
| 153 | $this->data['match_all'] = get_http_var('match_all') == 'on'; |
||
| 154 | $this->data['keyword'] = trim(get_http_var("keyword")); |
||
| 155 | $this->data['search_section'] = ''; |
||
| 156 | $this->data['alertsearch'] = trim(get_http_var("alertsearch")); |
||
| 157 | $this->data['mp_search'] = trim(get_http_var("mp_search")); |
||
| 158 | $this->data['pid'] = trim(get_http_var("pid")); |
||
| 159 | $this->data['pc'] = get_http_var('pc'); |
||
| 160 | $this->data['submitted'] = get_http_var('submitted') || $this->data['pid'] || $this->data['keyword'] || $this->data['step']; |
||
| 161 | $this->data['ignore_speaker_votes'] = get_http_var('ignore_speaker_votes'); |
||
| 162 | |||
| 163 | if ($this->data['addword'] || $this->data['step']) { |
||
| 164 | $alert = $this->alert->check_token($this->data['token']); |
||
| 165 | |||
| 166 | $criteria = ''; |
||
| 167 | $alert_ignore_speaker_votes = 0; |
||
| 168 | if ($alert) { |
||
| 169 | $criteria = $alert['criteria']; |
||
| 170 | $alert_ignore_speaker_votes = $alert['ignore_speaker_votes']; |
||
| 171 | } |
||
| 172 | |||
| 173 | $ignore_speaker_votes = get_http_var('ignore_speaker_votes', $alert_ignore_speaker_votes); |
||
| 174 | $this->data['ignore_speaker_votes'] = ($ignore_speaker_votes == 'on' || $ignore_speaker_votes == 1); |
||
| 175 | |||
| 176 | $this->data['alert'] = $alert; |
||
| 177 | |||
| 178 | $this->data['alert_parts'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($criteria, $alert_ignore_speaker_votes, true); |
||
| 179 | |||
| 180 | $existing_rep = ''; |
||
| 181 | if (isset($this->data['alert_parts']['spokenby'])) { |
||
| 182 | $existing_rep = $this->data['alert_parts']['spokenby'][0]; |
||
| 183 | } |
||
| 184 | |||
| 185 | $existing_section = ''; |
||
| 186 | if (count($this->data['alert_parts']['sections'])) { |
||
| 187 | $existing_section = $this->data['alert_parts']['sections'][0]; |
||
| 188 | } |
||
| 189 | |||
| 190 | if ($this->data['alert_parts']['match_all']) { |
||
| 191 | $this->data['match_all'] = true; |
||
| 192 | } |
||
| 193 | |||
| 194 | $words = get_http_var('words', $this->data['alert_parts']['words'], true); |
||
| 195 | |||
| 196 | $this->data['words'] = []; |
||
| 197 | $this->data['keywords'] = []; |
||
| 198 | foreach ($words as $word) { |
||
| 199 | if (trim($word) != '') { |
||
| 200 | $this->data['keywords'][] = $word; |
||
| 201 | $this->data['words'][] = $this->wrap_phrase_in_quotes($word); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | $add_all_related = get_http_var('add_all_related'); |
||
| 206 | $this->data['add_all_related'] = $add_all_related; |
||
| 207 | $this->data['skip_keyword_terms'] = []; |
||
| 208 | |||
| 209 | $selected_related_terms = get_http_var('selected_related_terms', [], true); |
||
| 210 | $this->data['selected_related_terms'] = $selected_related_terms; |
||
| 211 | |||
| 212 | if ($this->data['step'] !== 'define') { |
||
| 213 | if ($add_all_related) { |
||
| 214 | $this->data['selected_related_terms'] = []; |
||
| 215 | $related_terms = get_http_var('related_terms', [], true); |
||
| 216 | foreach ($related_terms as $term) { |
||
| 217 | $this->data['skip_keyword_terms'][] = $term; |
||
| 218 | $this->data['keywords'][] = $term; |
||
| 219 | $this->data['words'][] = $this->wrap_phrase_in_quotes($term); |
||
| 220 | } |
||
| 221 | } else { |
||
| 222 | $this->data['skip_keyword_terms'] = $selected_related_terms; |
||
| 223 | foreach ($selected_related_terms as $term) { |
||
| 224 | $this->data['keywords'][] = $term; |
||
| 225 | $this->data['words'][] = $this->wrap_phrase_in_quotes($term); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | $this->data['exclusions'] = trim(get_http_var("exclusions", implode(' ', $this->data['alert_parts']['exclusions']))); |
||
| 230 | $this->data['representative'] = trim(get_http_var("representative", $existing_rep)); |
||
| 231 | |||
| 232 | $this->data['search_section'] = trim(get_http_var("search_section", $existing_section)); |
||
| 233 | |||
| 234 | $separator = ' OR '; |
||
| 235 | if ($this->data['match_all']) { |
||
| 236 | $separator = ' '; |
||
| 237 | } |
||
| 238 | $this->data['keyword'] = implode($separator, $this->data['words']); |
||
| 239 | if ($this->data['exclusions']) { |
||
| 240 | $this->data['keyword'] = '(' . $this->data['keyword'] . ') -' . implode(' -', explode(' ', $this->data["exclusions"])); |
||
| 241 | } |
||
| 242 | |||
| 243 | $this->data['results'] = ''; |
||
| 244 | |||
| 245 | $this->getSearchSections(); |
||
| 246 | } else if ($this->data['mp_step'] == 'mp_alert') { |
||
| 247 | $alert = $this->alert->check_token($this->data['token']); |
||
| 248 | if ($alert) { |
||
| 249 | $ignore_speaker_votes = get_http_var('ignore_speaker_votes', $alert['ignore_speaker_votes']); |
||
| 250 | $this->data['ignore_speaker_votes'] = ($ignore_speaker_votes == 'on' || $ignore_speaker_votes == 1); |
||
| 251 | |||
| 252 | $existing_rep = ''; |
||
| 253 | if (isset($alert['criteria'])) { |
||
| 254 | $alert_parts = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($alert['criteria'], $alert['ignore_speaker_votes'], true); |
||
| 255 | $existing_rep = $alert_parts['spokenby'][0]; |
||
| 256 | $this->data['pid'] = $alert_parts['pid']; |
||
| 257 | } |
||
| 258 | $this->data['keyword'] = get_http_var('mp_search', $existing_rep); |
||
| 259 | } else { |
||
| 260 | $this->data['ignore_speaker_votes'] = get_http_var('ignore_speaker_votes'); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | $this->data['sign'] = get_http_var('sign'); |
||
| 265 | $this->data['site'] = get_http_var('site'); |
||
| 266 | $this->data['message'] = ''; |
||
| 267 | |||
| 268 | $ACTIONURL = new \MySociety\TheyWorkForYou\Url($this_page); |
||
| 269 | $ACTIONURL->reset(); |
||
| 270 | $this->data['actionurl'] = $ACTIONURL->generate(); |
||
| 271 | |||
| 272 | } |
||
| 273 | |||
| 274 | private function wrap_phrase_in_quotes($phrase) { |
||
| 275 | if (strpos($phrase, ' ') > 0) { |
||
| 276 | $phrase = '"' . trim($phrase, '"') . '"'; |
||
| 277 | } |
||
| 278 | |||
| 279 | return $phrase; |
||
| 280 | } |
||
| 281 | |||
| 282 | private function getRecentResults($text) { |
||
| 283 | global $SEARCHENGINE; |
||
| 284 | $se = new \SEARCHENGINE($text); |
||
| 285 | $this->data['search_result_count'] = $se->run_count(0, 10); |
||
| 286 | $se->run_search(0, 1, 'date'); |
||
| 287 | } |
||
| 288 | |||
| 289 | private function getSearchSections() { |
||
| 290 | $this->data['sections'] = []; |
||
| 291 | if ($this->data['search_section']) { |
||
| 292 | foreach (explode(' ', $this->data['search_section']) as $section) { |
||
| 293 | $this->data['sections'][] = \MySociety\TheyWorkForYou\Utility\Alert::sectionToTitle($section); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | private function updateAlert($token) { |
||
| 299 | $success = $this->alert->update($token, $this->data); |
||
| 300 | return $success; |
||
| 301 | } |
||
| 302 | |||
| 303 | private function checkInput() { |
||
| 304 | global $SEARCHENGINE; |
||
| 305 | |||
| 306 | $errors = []; |
||
| 307 | |||
| 308 | # these are the initial screens and so cannot have any errors as we've not submitted |
||
| 309 | if (!$this->data['submitted'] || $this->data['step'] == 'define' || $this->data['mp_step'] == 'mp_alert') { |
||
| 310 | $this->data['errors'] = $errors; |
||
| 311 | return; |
||
| 312 | } |
||
| 313 | |||
| 314 | // Check each of the things the user has input. |
||
| 315 | // If there is a problem with any of them, set an entry in the $errors array. |
||
| 316 | // This will then be used to (a) indicate there were errors and (b) display |
||
| 317 | // error messages when we show the form again. |
||
| 318 | |||
| 319 | // Check email address is valid and unique. |
||
| 320 | if (!$this->data['email']) { |
||
| 321 | $errors["email"] = gettext("Please enter your email address"); |
||
| 322 | } elseif (!validate_email($this->data["email"])) { |
||
| 323 | // validate_email() is in includes/utilities.php |
||
| 324 | $errors["email"] = gettext("Please enter a valid email address"); |
||
| 325 | } |
||
| 326 | |||
| 327 | if ($this->data['pid'] && !ctype_digit($this->data['pid'])) { |
||
| 328 | $errors['pid'] = 'Invalid person ID passed'; |
||
| 329 | } |
||
| 330 | |||
| 331 | $text = $this->data['alertsearch']; |
||
| 332 | if ($this->data['mp_search']) { |
||
| 333 | $text = $this->data['mp_search']; |
||
| 334 | } |
||
| 335 | if (!$text) { |
||
| 336 | $text = $this->data['keyword']; |
||
| 337 | } |
||
| 338 | |||
| 339 | if ($this->data['submitted'] && !$this->data['pid'] && !$text) { |
||
| 340 | $errors['alertsearch'] = gettext('Please enter what you want to be alerted about'); |
||
| 341 | } |
||
| 342 | |||
| 343 | if (strpos($text, '..')) { |
||
| 344 | $errors['alertsearch'] = gettext('You probably don’t want a date range as part of your criteria, as you won’t be alerted to anything new!'); |
||
| 345 | } |
||
| 346 | |||
| 347 | $se = new \SEARCHENGINE($text); |
||
| 348 | if (!$se->valid) { |
||
| 349 | $errors['alertsearch'] = sprintf(gettext('That search appears to be invalid - %s - please check and try again.'), $se->error); |
||
| 350 | } |
||
| 351 | |||
| 352 | if (strlen($text) > 255) { |
||
| 353 | $errors['alertsearch'] = gettext('That search is too long for our database; please split it up into multiple smaller alerts.'); |
||
| 354 | } |
||
| 355 | |||
| 356 | $this->data['errors'] = $errors; |
||
| 357 | } |
||
| 358 | |||
| 359 | private function searchForConstituenciesAndMembers() { |
||
| 360 | if ($this->data['results'] == 'changes-abandoned') { |
||
| 361 | $this->data['members'] = false; |
||
| 362 | return; |
||
| 363 | } |
||
| 364 | |||
| 365 | $text = $this->data['alertsearch']; |
||
| 366 | if ($this->data['mp_search']) { |
||
| 367 | $text = $this->data['mp_search']; |
||
| 368 | } |
||
| 369 | $errors = []; |
||
| 370 | if ($text != '') { |
||
| 371 | //$members_from_pids = array_values(\MySociety\TheyWorkForYou\Utility\Search::membersForIDs($this->data['alertsearch'])); |
||
| 372 | $members_from_names = []; |
||
| 373 | $names_from_pids = array_values(\MySociety\TheyWorkForYou\Utility\Search::speakerNamesForIDs($text)); |
||
| 374 | foreach ($names_from_pids as $name) { |
||
| 375 | $members_from_names = array_merge($members_from_names,\MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookupWithNames($name)); |
||
| 376 | } |
||
| 377 | $members_from_words = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookupWithNames($text, true); |
||
| 378 | $this->data['members'] = array_merge($members_from_words, $members_from_names); |
||
| 379 | [$this->data['constituencies'], $this->data['valid_postcode']] = \MySociety\TheyWorkForYou\Utility\Search::searchConstituenciesByQuery($text, false); |
||
| 380 | } elseif ($this->data['pid']) { |
||
| 381 | $MEMBER = new \MEMBER(['person_id' => $this->data['pid']]); |
||
| 382 | $this->data['members'] = [[ |
||
| 383 | "person_id" => $MEMBER->person_id, |
||
| 384 | "given_name" => $MEMBER->given_name, |
||
| 385 | "family_name" => $MEMBER->family_name, |
||
| 386 | "house" => $MEMBER->house_disp, |
||
| 387 | "title" => $MEMBER->title, |
||
| 388 | "lordofname" => $MEMBER->lordofname, |
||
| 389 | "constituency" => $MEMBER->constituency, |
||
| 390 | ]]; |
||
| 391 | } elseif (isset($this->data['representative']) && $this->data['representative'] != '') { |
||
| 392 | $this->data['members'] = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookupWithNames($this->data['representative'], true); |
||
| 393 | |||
| 394 | $member_count = count($this->data['members']); |
||
| 395 | if ($member_count == 0) { |
||
| 396 | $errors["representative"] = gettext("No matching representative found"); |
||
| 397 | } elseif ($member_count > 1) { |
||
| 398 | $errors["representative"] = gettext("Multiple matching representatives found, please select one."); |
||
| 399 | } else { |
||
| 400 | $this->data['pid'] = $this->data['members'][0]['person_id']; |
||
| 401 | } |
||
| 402 | } else { |
||
| 403 | $this->data['members'] = []; |
||
| 404 | } |
||
| 405 | |||
| 406 | # If the above search returned one result for constituency |
||
| 407 | # search by postcode, use it immediately |
||
| 408 | if (isset($this->data['constituencies']) && count($this->data['constituencies']) == 1 && $this->data['valid_postcode']) { |
||
| 409 | $MEMBER = new \MEMBER(['constituency' => array_values($this->data['constituencies'])[0], 'house' => 1]); |
||
| 410 | $this->data['pid'] = $MEMBER->person_id(); |
||
| 411 | $this->data['pc'] = $text; |
||
| 412 | unset($this->data['constituencies']); |
||
| 413 | } |
||
| 414 | |||
| 415 | if (isset($this->data['constituencies'])) { |
||
| 416 | $cons = []; |
||
| 417 | foreach ($this->data['constituencies'] as $constituency) { |
||
| 418 | try { |
||
| 419 | $MEMBER = new \MEMBER(['constituency' => $constituency]); |
||
| 420 | $cons[$constituency] = $MEMBER; |
||
| 421 | } catch (\MySociety\TheyWorkForYou\MemberException $e) { |
||
| 422 | // do nothing |
||
| 423 | } |
||
| 424 | } |
||
| 425 | $this->data['constituencies'] = $cons; |
||
| 426 | if (count($cons) == 1) { |
||
| 427 | $cons = array_values($cons); |
||
| 428 | $this->data['pid'] = $cons[0]->person_id(); |
||
| 429 | } |
||
| 430 | } |
||
| 431 | |||
| 432 | if ($this->data['alertsearch'] && !$this->data['mp_step'] && ($this->data['pid'] || $this->data['members'] || $this->data['constituencies'])) { |
||
| 433 | if (count($this->data['members']) == 1) { |
||
| 434 | $this->data['pid'] = $this->data['members'][0]['person_id']; |
||
| 435 | } |
||
| 436 | $this->data['mp_step'] = 'mp_alert'; |
||
| 437 | $this->data['mp_search'] = $this->data['alertsearch']; |
||
| 438 | $this->data['alertsearch'] = ''; |
||
| 439 | } |
||
| 440 | |||
| 441 | if (count($this->data["errors"]) > 0) { |
||
| 442 | $this->data["errors"] = array_merge($this->data["errors"], $errors); |
||
| 443 | } else { |
||
| 444 | $this->data["errors"] = $errors; |
||
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | private function addAlert() { |
||
| 496 | } |
||
| 497 | |||
| 498 | |||
| 499 | private function formatSearchTerms() { |
||
| 500 | if ($this->data['alertsearch']) { |
||
| 501 | $this->data['alertsearch_pretty'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($this->data['alertsearch']); |
||
| 502 | $this->data['search_text'] = $this->data['alertsearch']; |
||
| 503 | } else { |
||
| 504 | $this->data['search_text'] = $this->data['keyword']; |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | private function checkForCommonMistakes() { |
||
| 509 | $mistakes = []; |
||
| 510 | if (strstr($this->data['alertsearch'], ',') > -1) { |
||
| 511 | $mistakes['multiple'] = 1; |
||
| 512 | } |
||
| 513 | |||
| 514 | if ( |
||
| 515 | preg_match('#([A-Z]{1,2}\d+[A-Z]? ?\d[A-Z]{2})#i', $this->data['alertsearch'], $m) && |
||
| 516 | strlen($this->data['alertsearch']) > strlen($m[1]) && |
||
| 517 | validate_postcode($m[1]) |
||
| 518 | ) { |
||
| 519 | $this->data['postcode'] = $m[1]; |
||
| 520 | $mistakes['postcode_and'] = 1; |
||
| 521 | } |
||
| 522 | |||
| 523 | $this->data['mistakes'] = $mistakes; |
||
| 524 | } |
||
| 525 | |||
| 526 | private function formatSearchMemberData() { |
||
| 527 | if (isset($this->data['postcode'])) { |
||
| 528 | try { |
||
| 529 | $postcode = $this->data['postcode']; |
||
| 530 | |||
| 531 | $MEMBER = new \MEMBER(['postcode' => $postcode]); |
||
| 532 | // move the postcode to the front just to be tidy |
||
| 533 | $tidy_alertsearch = $postcode . " " . trim(str_replace("$postcode", "", $this->data['alertsearch'])); |
||
| 534 | $alertsearch_display = str_replace("$postcode ", "", $tidy_alertsearch); |
||
| 535 | |||
| 536 | $this->data['member_alertsearch'] = str_replace("$postcode", "speaker:" . $MEMBER->person_id, $tidy_alertsearch); |
||
| 537 | $this->data['member_displaysearch'] = $alertsearch_display; |
||
| 538 | $this->data['member'] = $MEMBER; |
||
| 539 | |||
| 540 | if (isset($this->data['mistakes']['postcode_and'])) { |
||
| 541 | $constituencies = \MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituencies($postcode); |
||
| 542 | if (isset($constituencies['SPC'])) { |
||
| 543 | $MEMBER = new \MEMBER(['constituency' => $constituencies['SPC'], 'house' => HOUSE_TYPE_SCOTLAND]); |
||
| 544 | $this->data['scottish_alertsearch'] = str_replace("$postcode", "speaker:" . $MEMBER->person_id, $tidy_alertsearch); |
||
| 545 | $this->data['scottish_member'] = $MEMBER; |
||
| 546 | } elseif (isset($constituencies['WAC'])) { |
||
| 547 | $MEMBER = new \MEMBER(['constituency' => $constituencies['WAC'], 'house' => HOUSE_TYPE_WALES]); |
||
| 548 | $this->data['welsh_alertsearch'] = str_replace("$postcode", "speaker:" . $MEMBER->person_id, $tidy_alertsearch); |
||
| 549 | $this->data['welsh_member'] = $MEMBER; |
||
| 550 | } |
||
| 551 | } |
||
| 552 | } catch (\MySociety\TheyWorkForYou\MemberException $e) { |
||
| 553 | $this->data['member_error'] = 1; |
||
| 554 | } |
||
| 555 | } |
||
| 556 | |||
| 557 | if ($this->data['pid']) { |
||
| 558 | $MEMBER = new \MEMBER(['person_id' => $this->data['pid']]); |
||
| 559 | $this->data['pid_member'] = $MEMBER; |
||
| 560 | } |
||
| 561 | |||
| 562 | if ($this->data['keyword']) { |
||
| 563 | $this->data['display_keyword'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($this->data['keyword']); |
||
| 564 | } |
||
| 565 | } |
||
| 566 | |||
| 567 | private function setUserData() { |
||
| 683 | } |
||
| 684 | } |
||
| 685 | } |
||
| 686 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.