Total Complexity | 174 |
Total Lines | 696 |
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() { |
||
271 | |||
272 | } |
||
273 | |||
274 | private function wrap_phrase_in_quotes($phrase) { |
||
280 | } |
||
281 | |||
282 | private function getRecentResults($text) { |
||
283 | global $SEARCHENGINE; |
||
284 | $today = date_create(); |
||
285 | $one_week_ago = date_create('7 days ago'); |
||
286 | $restriction = date_format($one_week_ago, 'd/m/Y') . '..' . date_format($today, 'd/m/Y'); |
||
287 | |||
288 | $se = new \SEARCHENGINE($text . ' ' . $restriction); |
||
289 | $this->data['search_result_count'] = $se->run_count(0, 10); |
||
290 | $se = new \SEARCHENGINE($text); |
||
291 | $count = $se->run_count(0, 10, 'date'); |
||
292 | if ($count > 0) { |
||
293 | $se->run_search(0, 1, 'date'); |
||
294 | $gid = $se->get_gids()[0]; |
||
295 | |||
296 | $q = ['gid_to' => $gid]; |
||
297 | do { |
||
298 | $gid = $q['gid_to']; |
||
299 | $q = $this->db->query("SELECT gid_to FROM gidredirect WHERE gid_from = :gid", [':gid' => $gid])->first(); |
||
300 | } while ($q); |
||
301 | |||
302 | $q = $this->db->query("SELECT hansard.gid, hansard.hdate |
||
303 | FROM hansard |
||
304 | WHERE hansard.gid = :gid", [':gid' => $gid])->first(); |
||
305 | |||
306 | $last_mention = date_create($q['hdate']); |
||
307 | $this->data['lastmention'] = date_format($last_mention, 'd M Y'); //$se->get_gids()[0]; |
||
308 | } |
||
309 | } |
||
310 | |||
311 | private function getSearchSections() { |
||
312 | $this->data['sections'] = []; |
||
313 | if ($this->data['search_section']) { |
||
314 | foreach (explode(' ', $this->data['search_section']) as $section) { |
||
315 | $this->data['sections'][] = \MySociety\TheyWorkForYou\Utility\Alert::sectionToTitle($section); |
||
316 | } |
||
317 | } |
||
318 | } |
||
319 | |||
320 | private function updateAlert($token) { |
||
321 | $success = $this->alert->update($token, $this->data); |
||
322 | return $success; |
||
323 | } |
||
324 | |||
325 | private function checkInput() { |
||
326 | global $SEARCHENGINE; |
||
327 | |||
328 | $errors = []; |
||
329 | |||
330 | # these are the initial screens and so cannot have any errors as we've not submitted |
||
331 | if (!$this->data['submitted'] || $this->data['step'] == 'define' || $this->data['mp_step'] == 'mp_alert') { |
||
332 | $this->data['errors'] = $errors; |
||
333 | return; |
||
334 | } |
||
335 | |||
336 | // Check each of the things the user has input. |
||
337 | // If there is a problem with any of them, set an entry in the $errors array. |
||
338 | // This will then be used to (a) indicate there were errors and (b) display |
||
339 | // error messages when we show the form again. |
||
340 | |||
341 | // Check email address is valid and unique. |
||
342 | if (!$this->data['email']) { |
||
343 | $errors["email"] = gettext("Please enter your email address"); |
||
344 | } elseif (!validate_email($this->data["email"])) { |
||
345 | // validate_email() is in includes/utilities.php |
||
346 | $errors["email"] = gettext("Please enter a valid email address"); |
||
347 | } |
||
348 | |||
349 | if ($this->data['pid'] && !ctype_digit($this->data['pid'])) { |
||
350 | $errors['pid'] = 'Invalid person ID passed'; |
||
351 | } |
||
352 | |||
353 | $text = $this->data['alertsearch']; |
||
354 | if ($this->data['mp_search']) { |
||
355 | $text = $this->data['mp_search']; |
||
356 | } |
||
357 | if (!$text) { |
||
358 | $text = $this->data['keyword']; |
||
359 | } |
||
360 | |||
361 | if ($this->data['submitted'] && !$this->data['pid'] && !$text) { |
||
362 | $errors['alertsearch'] = gettext('Please enter what you want to be alerted about'); |
||
363 | } |
||
364 | |||
365 | if (strpos($text, '..')) { |
||
366 | $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!'); |
||
367 | } |
||
368 | |||
369 | $se = new \SEARCHENGINE($text); |
||
370 | if (!$se->valid) { |
||
371 | $errors['alertsearch'] = sprintf(gettext('That search appears to be invalid - %s - please check and try again.'), $se->error); |
||
372 | } |
||
373 | |||
374 | if (strlen($text) > 255) { |
||
375 | $errors['alertsearch'] = gettext('That search is too long for our database; please split it up into multiple smaller alerts.'); |
||
376 | } |
||
377 | |||
378 | $this->data['errors'] = $errors; |
||
379 | } |
||
380 | |||
381 | private function searchForConstituenciesAndMembers() { |
||
382 | if ($this->data['results'] == 'changes-abandoned') { |
||
383 | $this->data['members'] = false; |
||
384 | return; |
||
385 | } |
||
386 | |||
387 | $text = $this->data['alertsearch']; |
||
388 | if ($this->data['mp_search']) { |
||
389 | $text = $this->data['mp_search']; |
||
390 | } |
||
391 | $errors = []; |
||
392 | if ($text != '') { |
||
393 | //$members_from_pids = array_values(\MySociety\TheyWorkForYou\Utility\Search::membersForIDs($this->data['alertsearch'])); |
||
394 | $members_from_names = []; |
||
395 | $names_from_pids = array_values(\MySociety\TheyWorkForYou\Utility\Search::speakerNamesForIDs($text)); |
||
396 | foreach ($names_from_pids as $name) { |
||
397 | $members_from_names = array_merge($members_from_names, \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookupWithNames($name)); |
||
398 | } |
||
399 | $members_from_words = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookupWithNames($text, true); |
||
400 | $this->data['members'] = array_merge($members_from_words, $members_from_names); |
||
401 | [$this->data['constituencies'], $this->data['valid_postcode']] = \MySociety\TheyWorkForYou\Utility\Search::searchConstituenciesByQuery($text, false); |
||
402 | } elseif ($this->data['pid']) { |
||
403 | $MEMBER = new \MEMBER(['person_id' => $this->data['pid']]); |
||
404 | $this->data['members'] = [[ |
||
405 | "person_id" => $MEMBER->person_id, |
||
406 | "given_name" => $MEMBER->given_name, |
||
407 | "family_name" => $MEMBER->family_name, |
||
408 | "house" => $MEMBER->house_disp, |
||
409 | "title" => $MEMBER->title, |
||
410 | "lordofname" => $MEMBER->lordofname, |
||
411 | "constituency" => $MEMBER->constituency, |
||
412 | ]]; |
||
413 | } elseif (isset($this->data['representative']) && $this->data['representative'] != '') { |
||
414 | $this->data['members'] = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookupWithNames($this->data['representative'], true); |
||
415 | |||
416 | $member_count = count($this->data['members']); |
||
417 | if ($member_count == 0) { |
||
418 | $errors["representative"] = gettext("No matching representative found"); |
||
419 | } elseif ($member_count > 1) { |
||
420 | $errors["representative"] = gettext("Multiple matching representatives found, please select one."); |
||
421 | } else { |
||
422 | $this->data['pid'] = $this->data['members'][0]['person_id']; |
||
423 | } |
||
424 | } else { |
||
425 | $this->data['members'] = []; |
||
426 | } |
||
427 | |||
428 | # If the above search returned one result for constituency |
||
429 | # search by postcode, use it immediately |
||
430 | if (isset($this->data['constituencies']) && count($this->data['constituencies']) == 1 && $this->data['valid_postcode']) { |
||
431 | $MEMBER = new \MEMBER(['constituency' => array_values($this->data['constituencies'])[0], 'house' => 1]); |
||
432 | $this->data['pid'] = $MEMBER->person_id(); |
||
433 | $this->data['pc'] = $text; |
||
434 | unset($this->data['constituencies']); |
||
435 | } |
||
436 | |||
437 | if (isset($this->data['constituencies'])) { |
||
438 | $cons = []; |
||
439 | foreach ($this->data['constituencies'] as $constituency) { |
||
440 | try { |
||
441 | $MEMBER = new \MEMBER(['constituency' => $constituency]); |
||
442 | $cons[$constituency] = $MEMBER; |
||
443 | } catch (\MySociety\TheyWorkForYou\MemberException $e) { |
||
444 | // do nothing |
||
445 | } |
||
446 | } |
||
447 | $this->data['constituencies'] = $cons; |
||
448 | if (count($cons) == 1) { |
||
449 | $cons = array_values($cons); |
||
450 | $this->data['pid'] = $cons[0]->person_id(); |
||
451 | } |
||
452 | } |
||
453 | |||
454 | if ($this->data['alertsearch'] && !$this->data['mp_step'] && ($this->data['pid'] || $this->data['members'] || $this->data['constituencies'])) { |
||
455 | if (count($this->data['members']) == 1) { |
||
456 | $this->data['pid'] = $this->data['members'][0]['person_id']; |
||
457 | } |
||
458 | $this->data['mp_step'] = 'mp_alert'; |
||
459 | $this->data['mp_search'] = $this->data['alertsearch']; |
||
460 | $this->data['alertsearch'] = ''; |
||
461 | } |
||
462 | |||
463 | if (count($this->data["errors"]) > 0) { |
||
464 | $this->data["errors"] = array_merge($this->data["errors"], $errors); |
||
465 | } else { |
||
466 | $this->data["errors"] = $errors; |
||
467 | } |
||
468 | } |
||
469 | |||
470 | private function addAlert() { |
||
518 | } |
||
519 | |||
520 | |||
521 | private function formatSearchTerms() { |
||
522 | if ($this->data['alertsearch']) { |
||
523 | $this->data['alertsearch_pretty'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($this->data['alertsearch']); |
||
524 | $this->data['search_text'] = $this->data['alertsearch']; |
||
525 | } else { |
||
526 | $this->data['search_text'] = $this->data['keyword']; |
||
527 | } |
||
528 | } |
||
529 | |||
530 | private function checkForCommonMistakes() { |
||
531 | $mistakes = []; |
||
532 | if (strstr($this->data['alertsearch'], ',') > -1) { |
||
533 | $mistakes['multiple'] = 1; |
||
534 | } |
||
535 | |||
536 | if ( |
||
537 | preg_match('#([A-Z]{1,2}\d+[A-Z]? ?\d[A-Z]{2})#i', $this->data['alertsearch'], $m) && |
||
538 | strlen($this->data['alertsearch']) > strlen($m[1]) && |
||
539 | validate_postcode($m[1]) |
||
540 | ) { |
||
541 | $this->data['postcode'] = $m[1]; |
||
542 | $mistakes['postcode_and'] = 1; |
||
543 | } |
||
544 | |||
545 | $this->data['mistakes'] = $mistakes; |
||
546 | } |
||
547 | |||
548 | private function formatSearchMemberData() { |
||
549 | if (isset($this->data['postcode'])) { |
||
550 | try { |
||
551 | $postcode = $this->data['postcode']; |
||
552 | |||
553 | $MEMBER = new \MEMBER(['postcode' => $postcode]); |
||
554 | // move the postcode to the front just to be tidy |
||
555 | $tidy_alertsearch = $postcode . " " . trim(str_replace("$postcode", "", $this->data['alertsearch'])); |
||
556 | $alertsearch_display = str_replace("$postcode ", "", $tidy_alertsearch); |
||
557 | |||
558 | $this->data['member_alertsearch'] = str_replace("$postcode", "speaker:" . $MEMBER->person_id, $tidy_alertsearch); |
||
559 | $this->data['member_displaysearch'] = $alertsearch_display; |
||
560 | $this->data['member'] = $MEMBER; |
||
561 | |||
562 | if (isset($this->data['mistakes']['postcode_and'])) { |
||
563 | $constituencies = \MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituencies($postcode); |
||
564 | if (isset($constituencies['SPC'])) { |
||
565 | $MEMBER = new \MEMBER(['constituency' => $constituencies['SPC'], 'house' => HOUSE_TYPE_SCOTLAND]); |
||
566 | $this->data['scottish_alertsearch'] = str_replace("$postcode", "speaker:" . $MEMBER->person_id, $tidy_alertsearch); |
||
567 | $this->data['scottish_member'] = $MEMBER; |
||
568 | } elseif (isset($constituencies['WAC'])) { |
||
569 | $MEMBER = new \MEMBER(['constituency' => $constituencies['WAC'], 'house' => HOUSE_TYPE_WALES]); |
||
570 | $this->data['welsh_alertsearch'] = str_replace("$postcode", "speaker:" . $MEMBER->person_id, $tidy_alertsearch); |
||
571 | $this->data['welsh_member'] = $MEMBER; |
||
572 | } |
||
573 | } |
||
574 | } catch (\MySociety\TheyWorkForYou\MemberException $e) { |
||
575 | $this->data['member_error'] = 1; |
||
576 | } |
||
577 | } |
||
578 | |||
579 | if ($this->data['pid']) { |
||
580 | $MEMBER = new \MEMBER(['person_id' => $this->data['pid']]); |
||
581 | $this->data['pid_member'] = $MEMBER; |
||
582 | } |
||
583 | |||
584 | if ($this->data['keyword']) { |
||
585 | $this->data['display_keyword'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($this->data['keyword']); |
||
586 | } |
||
587 | } |
||
588 | |||
589 | private function setUserData() { |
||
707 | } |
||
708 | } |
||
709 | } |
||
710 |
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.