| Conditions | 75 | 
| Paths | > 20000 | 
| Total Lines | 265 | 
| Code Lines | 195 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Tests | 0 | 
| CRAP Score | 5700 | 
| Changes | 1 | ||
| Bugs | 0 | Features | 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 | ||
| 40 |     public function __construct($query) { | ||
| 41 |         if (!defined('XAPIANDB') || !XAPIANDB) | ||
| 1 ignored issue–
                            show | |||
| 42 | return null; | ||
| 43 | |||
| 44 | global $xapiandb, $PAGE, $hansardmajors, $parties; | ||
| 45 |         if (!$xapiandb) { | ||
| 46 |             if (strstr(XAPIANDB, ":")) { | ||
| 47 |                 //ini_set('display_errors', 'On'); | ||
| 48 |                 list ($xapian_host, $xapian_port) = explode(":", XAPIANDB); | ||
| 49 |                 twfy_debug("SEARCH", "Using Xapian remote backend: " . $xapian_host . " port " . $xapian_port); | ||
| 50 | $xapiandb_remote = remote_open($xapian_host, intval($xapian_port)); | ||
| 51 | $xapiandb = new XapianDatabase($xapiandb_remote); | ||
| 1 ignored issue–
                            show | |||
| 52 |             } else { | ||
| 53 | $xapiandb = new XapianDatabase(XAPIANDB); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | $this->query = $query; | ||
| 57 |         if (!isset($this->stemmer)) $this->stemmer = new XapianStem('english'); | ||
| 1 ignored issue–
                            show | |||
| 58 | if (!isset($this->enquire)) $this->enquire = new XapianEnquire($xapiandb); | ||
| 1 ignored issue–
                            show | |||
| 59 |         if (!isset($this->queryparser)) { | ||
| 60 | $this->queryparser = new XapianQueryParser(); | ||
| 1 ignored issue–
                            show | |||
| 61 | $this->datevaluerange = new XapianDateValueRangeProcessor(1); | ||
| 1 ignored issue–
                            show | |||
| 62 | $this->queryparser->set_stemmer($this->stemmer); | ||
| 63 | $this->queryparser->set_stemming_strategy(XapianQueryParser::STEM_SOME); | ||
| 64 | $this->queryparser->set_database($xapiandb); | ||
| 65 | $this->queryparser->set_default_op(Query_OP_AND); | ||
| 1 ignored issue–
                            show | |||
| 66 |             $this->queryparser->add_boolean_prefix('speaker', 'S'); | ||
| 67 |             $this->queryparser->add_boolean_prefix('major', 'M'); | ||
| 68 |             $this->queryparser->add_boolean_prefix('date', 'D'); | ||
| 69 |             $this->queryparser->add_boolean_prefix('batch', 'B'); | ||
| 70 |             $this->queryparser->add_boolean_prefix('segment', 'U'); | ||
| 71 |             $this->queryparser->add_boolean_prefix('department', 'G'); | ||
| 72 |             $this->queryparser->add_boolean_prefix('party', 'P'); | ||
| 73 |             $this->queryparser->add_boolean_prefix('column', 'C'); | ||
| 74 |             $this->queryparser->add_boolean_prefix('gid', 'Q'); | ||
| 75 | $this->queryparser->add_valuerangeprocessor($this->datevaluerange); | ||
| 76 | } | ||
| 77 | |||
| 78 | # Force words to lower case | ||
| 79 |         $this->query = preg_replace('#(department|party):.+?\b#ie', 'strtolower("$0")', $this->query); | ||
| 80 | |||
| 81 | // Any characters other than this are treated as, basically, white space | ||
| 82 | // (apart from quotes and minuses, special case below) | ||
| 83 | // The colon is in here for prefixes speaker:10043 and so on. | ||
| 84 | $this->wordchars = "A-Za-z0-9,.'&:_\x80-\xbf\xc2-\xf4"; | ||
| 85 | $this->wordcharsnodigit = "A-Za-z0-9'&_\x80-\xbf\xc2-\xf4"; | ||
| 86 | |||
| 87 | // An array of normal words. | ||
| 88 | $this->words = array(); | ||
| 89 | // All quoted phrases, as an (array of (arrays of words in each phrase)). | ||
| 90 | $this->phrases = array(); | ||
| 91 | // Items prefixed with a colon (speaker:10024) as an (array of (name, value)) | ||
| 92 | $this->prefixed = array(); | ||
| 93 | |||
| 94 | // Split words up into individual words, and quoted phrases | ||
| 95 |         preg_match_all('/(' . | ||
| 96 | '"|' . # match either a quote, or... | ||
| 97 | '(?:(?<![' .$this->wordchars. '])-)?' . # optionally a - (exclude) | ||
| 98 | # if at start of word (i.e. not preceded by a word character, in | ||
| 99 | # which case it is probably a hyphenated-word) | ||
| 100 | '['.$this->wordchars.']+' . # followed by a string of word-characters | ||
| 101 | ')/', $this->query, $all_words); | ||
| 102 |         if ($all_words) { | ||
| 103 | $all_words = $all_words[0]; | ||
| 104 |         } else { | ||
| 105 | $all_words = array(); | ||
| 106 | } | ||
| 107 | $in_quote = false; | ||
| 108 | $from = ''; $to = ''; | ||
| 109 |         foreach ($all_words as $word) { | ||
| 110 |             if ($word == '"') { | ||
| 111 | $in_quote = !$in_quote; | ||
| 112 | if ($in_quote) array_push($this->phrases, array()); | ||
| 113 |                 if (!$in_quote && !count($this->phrases[count($this->phrases) - 1])) { | ||
| 114 | array_pop($this->phrases); | ||
| 115 | } | ||
| 116 | continue; | ||
| 117 | } | ||
| 118 |             if ($word == '') { | ||
| 119 | continue; | ||
| 120 | } | ||
| 121 | |||
| 122 |             if (strpos($word, ':') !== false) { | ||
| 123 |                 $items = explode(":", strtolower($word)); | ||
| 124 | $type = $items[0]; | ||
| 125 | if (substr($type, 0, 1)=='-') $type = substr($type, 1); | ||
| 126 |                 $value = strtolower(join(":", array_slice($items,1))); | ||
| 127 |                 if ($type == 'section') { | ||
| 128 | $newv = $value; | ||
| 129 | if ($value == 'debates' || $value == 'debate') $newv = 1; | ||
| 130 | elseif ($value == 'whall' || $value == 'westminster' || $value == 'westminhall') $newv = 2; | ||
| 131 | elseif ($value == 'wrans' || $value == 'wran') $newv = 3; | ||
| 132 | elseif ($value == 'wms' || $value == 'statements' || $value == 'statement') $newv = 4; | ||
| 133 | elseif ($value == 'lordsdebates' || $value == 'lords') $newv = 101; | ||
| 134 | elseif ($value == 'ni' || $value == 'nidebates') $newv = 5; | ||
| 135 | elseif ($value == 'pbc' || $value == 'standing') $newv = 6; | ||
| 136 | elseif ($value == 'sp') $newv = 7; | ||
| 137 | elseif ($value == 'spwrans' || $value == 'spwran') $newv = 8; | ||
| 138 | elseif ($value == 'uk') $newv = array(1,2,3,4,6,101); | ||
| 139 | elseif ($value == 'scotland') $newv = array(7,8); | ||
| 140 | elseif ($value == 'future') $newv = 'F'; | ||
| 141 |                     if (is_array($newv)) { | ||
| 142 |                         $newv = 'major:' . join(' major:', $newv); | ||
| 143 |                     } else { | ||
| 144 | $newv = "major:$newv"; | ||
| 145 | } | ||
| 146 |                     $this->query = str_ireplace("$type:$value", $newv, $this->query); | ||
| 147 |                 } elseif ($type == 'groupby') { | ||
| 148 | $newv = $value; | ||
| 149 | if ($value == 'debates' || $value == 'debate') $newv = 'debate'; | ||
| 150 | if ($value == 'speech' || $value == 'speeches') $newv = 'speech'; | ||
| 151 |                     $this->query = str_ireplace("$type:$value", '', $this->query); | ||
| 152 | array_push($this->prefixed, array($type, $newv)); | ||
| 153 |                 } elseif ($type == 'from') { | ||
| 154 | $from = $value; | ||
| 155 |                 } elseif ($type == 'to') { | ||
| 156 | $to = $value; | ||
| 157 | } | ||
| 158 |             } elseif (strpos($word, '-') !== false) { | ||
| 159 |             } elseif ($in_quote) { | ||
| 160 | array_push($this->phrases[count($this->phrases) - 1], strtolower($word)); | ||
| 161 |             } elseif (strpos($word, '..') !== false) { | ||
| 162 |             } elseif ($word == 'OR' || $word == 'AND' || $word == 'XOR' || $word == 'NEAR') { | ||
| 163 |             } else { | ||
| 164 | array_push($this->words, strtolower($word)); | ||
| 165 | } | ||
| 166 | } | ||
| 167 |         if ($from && $to) { | ||
| 168 |             $this->query = str_ireplace("from:$from", '', $this->query); | ||
| 169 |             $this->query = str_ireplace("to:$to", '', $this->query); | ||
| 170 | $this->query .= " $from..$to"; | ||
| 171 |         } elseif ($from) { | ||
| 172 |             $this->query = str_ireplace("from:$from", '', $this->query); | ||
| 173 |             $this->query .= " $from..".date('Ymd'); | ||
| 174 |         } elseif ($to) { | ||
| 175 |             $this->query = str_ireplace("to:$to", '', $this->query); | ||
| 176 | $this->query .= " 19990101..$to"; | ||
| 177 | } | ||
| 178 | |||
| 179 | # Merged people | ||
| 180 | $db = new ParlDB; | ||
| 181 |         $merged = $db->query('SELECT * FROM gidredirect WHERE gid_from LIKE :gid_from', array(':gid_from' => "uk.org.publicwhip/person/%")); | ||
| 182 |         for ($n=0; $n<$merged->rows(); $n++) { | ||
| 183 |             $from_id = str_replace('uk.org.publicwhip/person/', '', $merged->field($n, 'gid_from')); | ||
| 184 |             $to_id = str_replace('uk.org.publicwhip/person/', '', $merged->field($n, 'gid_to')); | ||
| 185 |             $this->query = preg_replace("#speaker:($from_id|$to_id)#i", "(speaker:$from_id OR speaker:$to_id)", $this->query); | ||
| 186 | } | ||
| 187 | |||
| 188 |         twfy_debug("SEARCH", "prefixed: " . var_export($this->prefixed, true)); | ||
| 189 | |||
| 190 |         twfy_debug("SEARCH", "query -- ". $this->query); | ||
| 191 | $flags = XapianQueryParser::FLAG_BOOLEAN | XapianQueryParser::FLAG_LOVEHATE | | ||
| 192 | XapianQueryParser::FLAG_WILDCARD | XapianQueryParser::FLAG_SPELLING_CORRECTION; | ||
| 193 | $flags = $flags | XapianQueryParser::FLAG_PHRASE; | ||
| 194 |         try { | ||
| 195 | $query = $this->queryparser->parse_query($this->query, $flags); | ||
| 196 |         } catch (Exception $e) { | ||
| 197 | # Nothing we can really do with a bad query | ||
| 198 | $this->error = _htmlspecialchars($e->getMessage()); | ||
| 199 | |||
| 200 | return null; | ||
| 201 | } | ||
| 202 | |||
| 203 | $this->enquire->set_query($query); | ||
| 204 | |||
| 205 | # Now parse the parsed query back into a query string, yummy | ||
| 206 | |||
| 207 | $qd = $query->get_description(); | ||
| 208 |         twfy_debug("SEARCH", "queryparser original description -- " . $qd); | ||
| 209 | $qd = substr($qd, 14, -1); # Strip Xapian::Query() | ||
| 210 |         $qd = preg_replace('#:\(.*?\)#', '', $qd); # Don't need pos or weight | ||
| 211 | # Date range | ||
| 212 |         $qd = preg_replace('#VALUE_RANGE 1 (\d+) (\d+)#e', 'preg_replace("#(\d{4})(\d\d)(\d\d)#", "\$3/\$2/\$1", $1) | ||
| 213 |             . ".." . preg_replace("#(\d{4})(\d\d)(\d\d)#", "\$3/\$2/\$1", $2)', $qd); | ||
| 214 | # Replace phrases with the phrase in quotes | ||
| 215 |         preg_match_all('#\(([^(]*? PHRASE [^(]*?)\)#', $qd, $m); | ||
| 216 |         foreach ($m[1] as $phrase) { | ||
| 217 |             $phrase_new = preg_replace('# PHRASE \d+#', '', $phrase); | ||
| 218 |             #$this->phrases[] = preg_split('#\s+#', $phrase_new); | ||
| 219 |             $qd = str_replace("($phrase)", '"'.$phrase_new.'"', $qd); | ||
| 220 | } | ||
| 221 |         preg_match_all('#\(([^(]*? NEAR [^(]*?)\)#', $qd, $m); | ||
| 222 |         foreach ($m[1] as $mm) { | ||
| 223 |             $mmn = preg_replace('# NEAR \d+ #', ' NEAR ', $mm); | ||
| 224 |             $qd = str_replace("($mm)", "($mmn)", $qd); | ||
| 225 | } | ||
| 226 | # Awesome regexes to get rid of superfluous matching brackets | ||
| 227 |         $qd = preg_replace('/( \( ( (?: (?>[^ ()]+) | (?1) ) (?: [ ](?:AND|OR|XOR|FILTER|NEAR[ ]\d+|PHRASE[ ]\d+)[ ] (?: (?>[^ ()]+) | (?1) ) )*  ) \) ) [ ] (FILTER|AND_NOT)/x', '$2 $3', $qd); | ||
| 228 |         $qd = preg_replace('/(?:FILTER | 0 [ ] \* ) [ ] ( \( ( (?: (?>[^ ()]+) | (?1) ) (?: [ ](?:AND|OR|XOR)[ ] (?: (?>[^ ()]+) | (?1) ) )*  ) \) )/x', '$2', $qd); | ||
| 229 |         $qd = preg_replace('/(?:FILTER | 0 [ ] \* ) [ ] ( [^()] )/x', '$1', $qd); | ||
| 230 |         $qd = str_replace('AND ', '', $qd); # AND is the default | ||
| 231 |         $qd = preg_replace('/^ ( \( ( (?: (?>[^()]+) | (?1) )* ) \) ) $/x', '$2', $qd); | ||
| 232 | # Other prefixes | ||
| 233 |         $qd = preg_replace('#\bU(\d+)\b#', 'segment:$1', $qd); | ||
| 234 |         $qd = preg_replace('#\bC(\d+)\b#', 'column:$1', $qd); | ||
| 235 |         $qd = preg_replace('#\bQ(.*?)\b#', 'gid:$1', $qd); | ||
| 236 |         $qd = preg_replace('#\bP(.*?)\b#e', '"party:" . (isset($parties[ucfirst("$1")]) ? $parties[ucfirst("$1")] : "$1")', $qd); | ||
| 237 |         $qd = preg_replace('#\bD(.*?)\b#', 'date:$1', $qd); | ||
| 238 |         $qd = preg_replace('#\bG(.*?)\b#', 'department:$1', $qd); # XXX Lookup to show proper name of dept | ||
| 239 |         if (strstr($qd, 'M1 OR M2 OR M3 OR M4 OR M6 OR M101')) { | ||
| 240 |             $qd = str_replace('M1 OR M2 OR M3 OR M4 OR M6 OR M101', 'section:uk', $qd); | ||
| 241 |         } elseif (strstr($qd, 'M7 OR M8')) { | ||
| 242 |             $qd = str_replace('M7 OR M8', 'section:scotland', $qd); | ||
| 243 | } | ||
| 244 |         $qd = preg_replace('#\bM(\d+)\b#e', '"in the \'" . (isset($hansardmajors[$1]["title"]) ? $hansardmajors[$1]["title"] . "\'" : "$1")', $qd); | ||
| 245 |         $qd = preg_replace('#\bMF\b#', 'in Future Business', $qd); | ||
| 246 | |||
| 247 | # Replace stemmed things with their unstemmed terms from the query | ||
| 248 | $used = array(); | ||
| 249 |         preg_match_all('#Z[^\s()]+#', $qd, $m); | ||
| 250 |         foreach ($m[0] as $mm) { | ||
| 251 | $iter = $this->queryparser->unstem_begin($mm); | ||
| 252 | $end = $this->queryparser->unstem_end($mm); | ||
| 253 |             while (!$iter->equals($end)) { | ||
| 254 | $tt = $iter->get_term(); | ||
| 255 | if (!in_array($tt, $used)) break; | ||
| 256 | $iter->next(); | ||
| 257 | } | ||
| 258 | $used[] = $tt; | ||
| 259 |             $qd = preg_replace('#' . preg_quote($mm, '#') . '#', $tt, $qd, 1); | ||
| 260 | } | ||
| 261 | |||
| 262 | # Speakers | ||
| 263 |         for ($n=0; $n<$merged->rows(); $n++) { | ||
| 264 |             $from_id = str_replace('uk.org.publicwhip/person/', '', $merged->field($n, 'gid_from')); | ||
| 265 |             $to_id = str_replace('uk.org.publicwhip/person/', '', $merged->field($n, 'gid_to')); | ||
| 266 |             $qd = str_replace("(S$from_id OR S$to_id)", "S$to_id", $qd); | ||
| 267 |             $qd = str_replace("S$from_id OR S$to_id", "S$to_id", $qd); | ||
| 268 | } | ||
| 269 | |||
| 270 |         preg_match_all('#S(\d+)#', $qd, $m); | ||
| 271 |         foreach ($m[1] as $mm) { | ||
| 272 |             $member = new MEMBER(array('person_id' => $mm)); | ||
| 273 | $name = $member->full_name(); | ||
| 274 |             $qd = str_replace("S$mm", "speaker:$name", $qd); | ||
| 275 | } | ||
| 276 | |||
| 277 | # Simplify display of excluded words | ||
| 278 |         $qd = preg_replace('#AND_NOT ([a-z0-9"]+)#', '-$1', $qd); | ||
| 279 |         preg_match_all('#AND_NOT \((.*?)\)#', $qd, $m); | ||
| 280 |         foreach ($m[1] as $mm) { | ||
| 281 |             $mmn = '-' . join(' -', explode(' OR ', $mm)); | ||
| 282 |             $qd = str_replace("AND_NOT ($mm)", $mmn, $qd); | ||
| 283 | } | ||
| 284 | |||
| 285 |         foreach ($this->prefixed as $items) { | ||
| 286 |             if ($items[0] == 'groupby') { | ||
| 287 |                 if ($items[1] == 'debate') { | ||
| 288 | $qd .= ' grouped by debate'; | ||
| 289 |                 } elseif ($items[1] == 'speech') { | ||
| 290 | $qd .= ' showing all speeches'; | ||
| 291 |                 } else { | ||
| 292 |                     $PAGE->error_message("Unknown group by '$items[1]' ignored"); | ||
| 293 | } | ||
| 294 | } | ||
| 295 | } | ||
| 296 | |||
| 297 | $this->query_desc = trim($qd); | ||
| 298 | |||
| 299 | #print 'DEBUG: ' . $query->get_description(); | ||
| 300 |         twfy_debug("SEARCH", "words: " . var_export($this->words, true)); | ||
| 301 |         twfy_debug("SEARCH", "phrases: " . var_export($this->phrases, true)); | ||
| 302 |         twfy_debug("SEARCH", "queryparser description -- " . $this->query_desc); | ||
| 303 | |||
| 304 | $this->valid = true; | ||
| 305 | } | ||
| 621 | 
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.