| Conditions | 16 | 
| Paths | 1446 | 
| Total Lines | 84 | 
| Code Lines | 60 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php | ||
| 77 | function pick_multiple($pc, $areas, $area_type, $rep_type) { | ||
| 78 | global $PAGE; | ||
| 79 | $db = new ParlDB; | ||
| 80 | |||
| 81 |     $q = $db->query("SELECT member.person_id, given_name, family_name, constituency, left_house | ||
| 82 | FROM member, person_names pn | ||
| 83 | WHERE constituency = :constituency | ||
| 84 | AND member.person_id = pn.person_id AND pn.type = 'name' | ||
| 85 | AND pn.end_date = (SELECT MAX(end_date) from person_names where person_names.person_id = member.person_id) | ||
| 86 | AND house = 1 ORDER BY left_house DESC LIMIT 1", array( | ||
| 87 | ':constituency' => MySociety\TheyWorkForYou\Utility\Constituencies::normaliseConstituencyName($areas['WMC']) | ||
| 88 | )); | ||
| 89 | $mp = array(); | ||
| 90 |     if ($q->rows()) { | ||
| 91 | $mp = $q->row(0); | ||
| 92 | if ($mp['left_house'] != '9999-12-31') $mp['former'] = true; | ||
| 93 | } | ||
| 94 | |||
| 95 | $a = array_values($areas); | ||
| 96 | |||
| 97 | $query_base = "SELECT member.person_id, given_name, family_name, constituency, house | ||
| 98 | FROM member, person_names pn | ||
| 99 |         WHERE constituency IN ('" . join("','", $a) . "') | ||
| 100 | AND member.person_id = pn.person_id AND pn.type = 'name' | ||
| 101 | AND pn.end_date = (SELECT MAX(end_date) from person_names where person_names.person_id = member.person_id)"; | ||
| 102 | $q = $db->query($query_base . " AND left_reason = 'still_in_office' AND house in (3,4)"); | ||
| 103 | $current = true; | ||
| 104 |     if (!$q->rows() && ($dissolution = MySociety\TheyWorkForYou\Dissolution::db())) { | ||
| 105 | $current = false; | ||
| 106 | $q = $db->query($query_base . " AND $dissolution[query]", | ||
| 107 | $dissolution['params']); | ||
| 108 | } | ||
| 109 | |||
| 110 | $mcon = array(); $mreg = array(); | ||
| 111 |     for ($i=0; $i<$q->rows(); $i++) { | ||
| 112 | $house = $q->field($i, 'house'); | ||
| 113 | $cons = $q->field($i, 'constituency'); | ||
| 114 |         if ($house==3) { | ||
| 115 | $mreg[] = $q->row($i); | ||
| 116 |         } elseif ($house==4) { | ||
| 117 |             if ($cons == $areas['SPC']) { | ||
| 118 | $mcon = $q->row($i); | ||
| 119 |             } elseif ($cons == $areas['SPE']) { | ||
| 120 | $mreg[] = $q->row($i); | ||
| 121 | } | ||
| 122 |         } else { | ||
| 123 |             $PAGE->error_message('Odd result returned, please let us know!'); | ||
| 124 | return; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | $out = ''; | ||
| 129 | $out .= '<p>That postcode has multiple results, please pick who you are interested in:</p>'; | ||
| 130 | $out .= '<ul><li>Your '; | ||
| 131 | if (isset($mp['former'])) $out .= 'former '; | ||
| 132 | $out .= '<strong>MP</strong> (Member of Parliament) is <a href="/mp/?p=' . $mp['person_id'] . '">'; | ||
| 133 | $out .= $mp['given_name'] . ' ' . $mp['family_name'] . '</a>, ' . $mp['constituency'] . '</li>'; | ||
| 134 |     if ($mcon) { | ||
| 135 | $out .= '<li>Your <strong>constituency MSP</strong> (Member of the Scottish Parliament) '; | ||
| 136 | $out .= $current ? 'is' : 'was'; | ||
| 137 | $out .= ' <a href="/msp/?p=' . $mcon['person_id'] . '">'; | ||
| 138 | $out .= $mcon['given_name'] . ' ' . $mcon['family_name'] . '</a>, ' . $mcon['constituency'] . '</li>'; | ||
| 139 | } | ||
| 140 | $out .= '<li>Your <strong>' . $areas[$area_type] . ' ' . $rep_type . 's</strong> '; | ||
| 141 | if ($rep_type=='MLA') $out .= '(Members of the Legislative Assembly)'; | ||
| 142 | $out .= ' ' . ($current ? 'are' : 'were') . ':'; | ||
| 143 | $out .= '<ul>'; | ||
| 144 |     foreach ($mreg as $reg) { | ||
| 145 | $out .= '<li><a href="/' . strtolower($rep_type) . '/?p=' . $reg['person_id'] . '">'; | ||
| 146 | $out .= $reg['given_name'] . ' ' . $reg['family_name']; | ||
| 147 | $out .= '</a>'; | ||
| 148 | } | ||
| 149 | $out .= '</ul></ul>'; | ||
| 150 | |||
| 151 |     $MPSURL = new \MySociety\TheyWorkForYou\Url('mps'); | ||
| 152 | $REGURL = new \MySociety\TheyWorkForYou\Url(strtolower($rep_type) . 's'); | ||
| 153 | $sidebar = array(array( | ||
| 154 | 'type' => 'html', | ||
| 155 | 'content' => '<div class="block"><h4>Browse people</h4> | ||
| 156 | <ul><li><a href="' . $MPSURL->generate() . '">Browse all MPs</a></li> | ||
| 157 | <li><a href="' . $REGURL->generate() . '">Browse all ' . $rep_type . 's</a></li> | ||
| 158 | </ul></div>' | ||
| 159 | )); | ||
| 160 | return array($out, $sidebar); | ||
| 161 | } | ||
| 170 | 
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.