| Conditions | 44 |
| Paths | > 20000 |
| Total Lines | 240 |
| Code Lines | 151 |
| 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 |
||
| 46 | public function highlightText( $text, $terms, $contextlines, $contextchars ) { |
||
| 47 | global $wgContLang, $wgSearchHighlightBoundaries; |
||
| 48 | |||
| 49 | if ( $text == '' ) { |
||
| 50 | return ''; |
||
| 51 | } |
||
| 52 | |||
| 53 | // spli text into text + templates/links/tables |
||
| 54 | $spat = "/(\\{\\{)|(\\[\\[[^\\]:]+:)|(\n\\{\\|)"; |
||
| 55 | // first capture group is for detecting nested templates/links/tables/references |
||
| 56 | $endPatterns = [ |
||
| 57 | 1 => '/(\{\{)|(\}\})/', // template |
||
| 58 | 2 => '/(\[\[)|(\]\])/', // image |
||
| 59 | 3 => "/(\n\\{\\|)|(\n\\|\\})/" ]; // table |
||
| 60 | |||
| 61 | // @todo FIXME: This should prolly be a hook or something |
||
| 62 | // instead of hardcoding a class name from the Cite extension |
||
| 63 | if ( class_exists( 'Cite' ) ) { |
||
| 64 | $spat .= '|(<ref>)'; // references via cite extension |
||
| 65 | $endPatterns[4] = '/(<ref>)|(<\/ref>)/'; |
||
| 66 | } |
||
| 67 | $spat .= '/'; |
||
| 68 | $textExt = []; // text extracts |
||
| 69 | $otherExt = []; // other extracts |
||
| 70 | $start = 0; |
||
| 71 | $textLen = strlen( $text ); |
||
| 72 | $count = 0; // sequence number to maintain ordering |
||
| 73 | while ( $start < $textLen ) { |
||
| 74 | // find start of template/image/table |
||
| 75 | if ( preg_match( $spat, $text, $matches, PREG_OFFSET_CAPTURE, $start ) ) { |
||
| 76 | $epat = ''; |
||
| 77 | foreach ( $matches as $key => $val ) { |
||
| 78 | if ( $key > 0 && $val[1] != - 1 ) { |
||
| 79 | if ( $key == 2 ) { |
||
| 80 | // see if this is an image link |
||
| 81 | $ns = substr( $val[0], 2, - 1 ); |
||
| 82 | if ( $wgContLang->getNsIndex( $ns ) != NS_FILE ) { |
||
| 83 | break; |
||
| 84 | } |
||
| 85 | |||
| 86 | } |
||
| 87 | $epat = $endPatterns[$key]; |
||
| 88 | $this->splitAndAdd( $textExt, $count, substr( $text, $start, $val[1] - $start ) ); |
||
| 89 | $start = $val[1]; |
||
| 90 | break; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | if ( $epat ) { |
||
| 94 | // find end (and detect any nested elements) |
||
| 95 | $level = 0; |
||
| 96 | $offset = $start + 1; |
||
| 97 | $found = false; |
||
| 98 | while ( preg_match( $epat, $text, $endMatches, PREG_OFFSET_CAPTURE, $offset ) ) { |
||
| 99 | if ( array_key_exists( 2, $endMatches ) ) { |
||
| 100 | // found end |
||
| 101 | if ( $level == 0 ) { |
||
| 102 | $len = strlen( $endMatches[2][0] ); |
||
| 103 | $off = $endMatches[2][1]; |
||
| 104 | $this->splitAndAdd( $otherExt, $count, |
||
| 105 | substr( $text, $start, $off + $len - $start ) ); |
||
| 106 | $start = $off + $len; |
||
| 107 | $found = true; |
||
| 108 | break; |
||
| 109 | } else { |
||
| 110 | // end of nested element |
||
| 111 | $level -= 1; |
||
| 112 | } |
||
| 113 | } else { |
||
| 114 | // nested |
||
| 115 | $level += 1; |
||
| 116 | } |
||
| 117 | $offset = $endMatches[0][1] + strlen( $endMatches[0][0] ); |
||
| 118 | } |
||
| 119 | if ( !$found ) { |
||
| 120 | // couldn't find appropriate closing tag, skip |
||
| 121 | $this->splitAndAdd( $textExt, $count, substr( $text, $start, strlen( $matches[0][0] ) ) ); |
||
| 122 | $start += strlen( $matches[0][0] ); |
||
| 123 | } |
||
| 124 | continue; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | // else: add as text extract |
||
| 128 | $this->splitAndAdd( $textExt, $count, substr( $text, $start ) ); |
||
| 129 | break; |
||
| 130 | } |
||
| 131 | |||
| 132 | $all = $textExt + $otherExt; // these have disjunct key sets |
||
| 133 | |||
| 134 | // prepare regexps |
||
| 135 | foreach ( $terms as $index => $term ) { |
||
| 136 | // manually do upper/lowercase stuff for utf-8 since PHP won't do it |
||
| 137 | if ( preg_match( '/[\x80-\xff]/', $term ) ) { |
||
| 138 | $terms[$index] = preg_replace_callback( |
||
| 139 | '/./us', |
||
| 140 | [ $this, 'caseCallback' ], |
||
| 141 | $terms[$index] |
||
| 142 | ); |
||
| 143 | } else { |
||
| 144 | $terms[$index] = $term; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | $anyterm = implode( '|', $terms ); |
||
| 148 | $phrase = implode( "$wgSearchHighlightBoundaries+", $terms ); |
||
| 149 | // @todo FIXME: A hack to scale contextchars, a correct solution |
||
| 150 | // would be to have contextchars actually be char and not byte |
||
| 151 | // length, and do proper utf-8 substrings and lengths everywhere, |
||
| 152 | // but PHP is making that very hard and unclean to implement :( |
||
| 153 | $scale = strlen( $anyterm ) / mb_strlen( $anyterm ); |
||
| 154 | $contextchars = intval( $contextchars * $scale ); |
||
| 155 | |||
| 156 | $patPre = "(^|$wgSearchHighlightBoundaries)"; |
||
| 157 | $patPost = "($wgSearchHighlightBoundaries|$)"; |
||
| 158 | |||
| 159 | $pat1 = "/(" . $phrase . ")/ui"; |
||
| 160 | $pat2 = "/$patPre(" . $anyterm . ")$patPost/ui"; |
||
| 161 | |||
| 162 | $left = $contextlines; |
||
| 163 | |||
| 164 | $snippets = []; |
||
| 165 | $offsets = []; |
||
| 166 | |||
| 167 | // show beginning only if it contains all words |
||
| 168 | $first = 0; |
||
| 169 | $firstText = ''; |
||
| 170 | foreach ( $textExt as $index => $line ) { |
||
| 171 | if ( strlen( $line ) > 0 && $line[0] != ';' && $line[0] != ':' ) { |
||
| 172 | $firstText = $this->extract( $line, 0, $contextchars * $contextlines ); |
||
| 173 | $first = $index; |
||
| 174 | break; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | if ( $firstText ) { |
||
| 178 | $succ = true; |
||
| 179 | // check if first text contains all terms |
||
| 180 | foreach ( $terms as $term ) { |
||
| 181 | if ( !preg_match( "/$patPre" . $term . "$patPost/ui", $firstText ) ) { |
||
| 182 | $succ = false; |
||
| 183 | break; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | if ( $succ ) { |
||
| 187 | $snippets[$first] = $firstText; |
||
| 188 | $offsets[$first] = 0; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | if ( !$snippets ) { |
||
| 192 | // match whole query on text |
||
| 193 | $this->process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets ); |
||
| 194 | // match whole query on templates/tables/images |
||
| 195 | $this->process( $pat1, $otherExt, $left, $contextchars, $snippets, $offsets ); |
||
| 196 | // match any words on text |
||
| 197 | $this->process( $pat2, $textExt, $left, $contextchars, $snippets, $offsets ); |
||
| 198 | // match any words on templates/tables/images |
||
| 199 | $this->process( $pat2, $otherExt, $left, $contextchars, $snippets, $offsets ); |
||
| 200 | |||
| 201 | ksort( $snippets ); |
||
| 202 | } |
||
| 203 | |||
| 204 | // add extra chars to each snippet to make snippets constant size |
||
| 205 | $extended = []; |
||
| 206 | if ( count( $snippets ) == 0 ) { |
||
| 207 | // couldn't find the target words, just show beginning of article |
||
| 208 | if ( array_key_exists( $first, $all ) ) { |
||
| 209 | $targetchars = $contextchars * $contextlines; |
||
| 210 | $snippets[$first] = ''; |
||
| 211 | $offsets[$first] = 0; |
||
| 212 | } |
||
| 213 | } else { |
||
| 214 | // if begin of the article contains the whole phrase, show only that !! |
||
| 215 | if ( array_key_exists( $first, $snippets ) && preg_match( $pat1, $snippets[$first] ) |
||
| 216 | && $offsets[$first] < $contextchars * 2 ) { |
||
| 217 | $snippets = [ $first => $snippets[$first] ]; |
||
| 218 | } |
||
| 219 | |||
| 220 | // calc by how much to extend existing snippets |
||
| 221 | $targetchars = intval( ( $contextchars * $contextlines ) / count( $snippets ) ); |
||
| 222 | } |
||
| 223 | |||
| 224 | foreach ( $snippets as $index => $line ) { |
||
| 225 | $extended[$index] = $line; |
||
| 226 | $len = strlen( $line ); |
||
| 227 | if ( $len < $targetchars - 20 ) { |
||
| 228 | // complete this line |
||
| 229 | if ( $len < strlen( $all[$index] ) ) { |
||
| 230 | $extended[$index] = $this->extract( |
||
| 231 | $all[$index], |
||
| 232 | $offsets[$index], |
||
| 233 | $offsets[$index] + $targetchars, |
||
|
|
|||
| 234 | $offsets[$index] |
||
| 235 | ); |
||
| 236 | $len = strlen( $extended[$index] ); |
||
| 237 | } |
||
| 238 | |||
| 239 | // add more lines |
||
| 240 | $add = $index + 1; |
||
| 241 | while ( $len < $targetchars - 20 |
||
| 242 | && array_key_exists( $add, $all ) |
||
| 243 | && !array_key_exists( $add, $snippets ) ) { |
||
| 244 | $offsets[$add] = 0; |
||
| 245 | $tt = "\n" . $this->extract( $all[$add], 0, $targetchars - $len, $offsets[$add] ); |
||
| 246 | $extended[$add] = $tt; |
||
| 247 | $len += strlen( $tt ); |
||
| 248 | $add++; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | // $snippets = array_map( 'htmlspecialchars', $extended ); |
||
| 254 | $snippets = $extended; |
||
| 255 | $last = - 1; |
||
| 256 | $extract = ''; |
||
| 257 | foreach ( $snippets as $index => $line ) { |
||
| 258 | if ( $last == - 1 ) { |
||
| 259 | $extract .= $line; // first line |
||
| 260 | } elseif ( $last + 1 == $index |
||
| 261 | && $offsets[$last] + strlen( $snippets[$last] ) >= strlen( $all[$last] ) |
||
| 262 | ) { |
||
| 263 | $extract .= " " . $line; // continous lines |
||
| 264 | } else { |
||
| 265 | $extract .= '<b> ... </b>' . $line; |
||
| 266 | } |
||
| 267 | |||
| 268 | $last = $index; |
||
| 269 | } |
||
| 270 | if ( $extract ) { |
||
| 271 | $extract .= '<b> ... </b>'; |
||
| 272 | } |
||
| 273 | |||
| 274 | $processed = []; |
||
| 275 | foreach ( $terms as $term ) { |
||
| 276 | if ( !isset( $processed[$term] ) ) { |
||
| 277 | $pat3 = "/$patPre(" . $term . ")$patPost/ui"; // highlight word |
||
| 278 | $extract = preg_replace( $pat3, |
||
| 279 | "\\1<span class='searchmatch'>\\2</span>\\3", $extract ); |
||
| 280 | $processed[$term] = true; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | return $extract; |
||
| 285 | } |
||
| 286 | |||
| 558 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: