| Conditions | 53 |
| Paths | > 20000 |
| Total Lines | 238 |
| Code Lines | 147 |
| Lines | 12 |
| Ratio | 5.04 % |
| 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 |
||
| 64 | private function run( $resultPageSet = null ) { |
||
| 65 | global $wgContLang; |
||
| 66 | $params = $this->extractRequestParams(); |
||
| 67 | |||
| 68 | if ( isset( $params['backend'] ) && $params['backend'] == self::BACKEND_NULL_PARAM ) { |
||
| 69 | unset( $params['backend'] ); |
||
| 70 | } |
||
| 71 | |||
| 72 | // Extract parameters |
||
| 73 | $query = $params['search']; |
||
| 74 | $what = $params['what']; |
||
| 75 | $interwiki = $params['interwiki']; |
||
| 76 | $searchInfo = array_flip( $params['info'] ); |
||
| 77 | $prop = array_flip( $params['prop'] ); |
||
| 78 | |||
| 79 | // Deprecated parameters |
||
| 80 | if ( isset( $prop['hasrelated'] ) ) { |
||
| 81 | $this->logFeatureUsage( 'action=search&srprop=hasrelated' ); |
||
| 82 | $this->setWarning( 'srprop=hasrelated has been deprecated' ); |
||
| 83 | } |
||
| 84 | if ( isset( $prop['score'] ) ) { |
||
| 85 | $this->logFeatureUsage( 'action=search&srprop=score' ); |
||
| 86 | $this->setWarning( 'srprop=score has been deprecated' ); |
||
| 87 | } |
||
| 88 | |||
| 89 | // Create search engine instance and set options |
||
| 90 | $search = $this->buildSearchEngine( $params ); |
||
| 91 | $search->setFeatureData( 'rewrite', (bool)$params['enablerewrites'] ); |
||
| 92 | |||
| 93 | $query = $search->transformSearchTerm( $query ); |
||
| 94 | $query = $search->replacePrefixes( $query ); |
||
| 95 | |||
| 96 | // Perform the actual search |
||
| 97 | if ( $what == 'text' ) { |
||
| 98 | $matches = $search->searchText( $query ); |
||
| 99 | } elseif ( $what == 'title' ) { |
||
| 100 | $matches = $search->searchTitle( $query ); |
||
| 101 | } elseif ( $what == 'nearmatch' ) { |
||
| 102 | // near matches must receive the user input as provided, otherwise |
||
| 103 | // the near matches within namespaces are lost. |
||
| 104 | $matches = $search->getNearMatcher( $this->getConfig() ) |
||
| 105 | ->getNearMatchResultSet( $params['search'] ); |
||
| 106 | } else { |
||
| 107 | // We default to title searches; this is a terrible legacy |
||
| 108 | // of the way we initially set up the MySQL fulltext-based |
||
| 109 | // search engine with separate title and text fields. |
||
| 110 | // In the future, the default should be for a combined index. |
||
| 111 | $what = 'title'; |
||
| 112 | $matches = $search->searchTitle( $query ); |
||
| 113 | |||
| 114 | // Not all search engines support a separate title search, |
||
| 115 | // for instance the Lucene-based engine we use on Wikipedia. |
||
| 116 | // In this case, fall back to full-text search (which will |
||
| 117 | // include titles in it!) |
||
| 118 | if ( is_null( $matches ) ) { |
||
| 119 | $what = 'text'; |
||
| 120 | $matches = $search->searchText( $query ); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | if ( is_null( $matches ) ) { |
||
| 124 | $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" ); |
||
| 125 | } elseif ( $matches instanceof Status && !$matches->isGood() ) { |
||
| 126 | $this->dieUsage( $matches->getWikiText( false, false, 'en' ), 'search-error' ); |
||
| 127 | } |
||
| 128 | |||
| 129 | if ( $resultPageSet === null ) { |
||
| 130 | $apiResult = $this->getResult(); |
||
| 131 | // Add search meta data to result |
||
| 132 | if ( isset( $searchInfo['totalhits'] ) ) { |
||
| 133 | $totalhits = $matches->getTotalHits(); |
||
|
|
|||
| 134 | if ( $totalhits !== null ) { |
||
| 135 | $apiResult->addValue( [ 'query', 'searchinfo' ], |
||
| 136 | 'totalhits', $totalhits ); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) { |
||
| 140 | $apiResult->addValue( [ 'query', 'searchinfo' ], |
||
| 141 | 'suggestion', $matches->getSuggestionQuery() ); |
||
| 142 | $apiResult->addValue( [ 'query', 'searchinfo' ], |
||
| 143 | 'suggestionsnippet', $matches->getSuggestionSnippet() ); |
||
| 144 | } |
||
| 145 | if ( isset( $searchInfo['rewrittenquery'] ) && $matches->hasRewrittenQuery() ) { |
||
| 146 | $apiResult->addValue( [ 'query', 'searchinfo' ], |
||
| 147 | 'rewrittenquery', $matches->getQueryAfterRewrite() ); |
||
| 148 | $apiResult->addValue( [ 'query', 'searchinfo' ], |
||
| 149 | 'rewrittenquerysnippet', $matches->getQueryAfterRewriteSnippet() ); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | // Add the search results to the result |
||
| 154 | $terms = $wgContLang->convertForSearchResult( $matches->termMatches() ); |
||
| 155 | $titles = []; |
||
| 156 | $count = 0; |
||
| 157 | $result = $matches->next(); |
||
| 158 | $limit = $params['limit']; |
||
| 159 | |||
| 160 | while ( $result ) { |
||
| 161 | View Code Duplication | if ( ++$count > $limit ) { |
|
| 162 | // We've reached the one extra which shows that there are |
||
| 163 | // additional items to be had. Stop here... |
||
| 164 | $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] ); |
||
| 165 | break; |
||
| 166 | } |
||
| 167 | |||
| 168 | // Silently skip broken and missing titles |
||
| 169 | if ( $result->isBrokenTitle() || $result->isMissingRevision() ) { |
||
| 170 | $result = $matches->next(); |
||
| 171 | continue; |
||
| 172 | } |
||
| 173 | |||
| 174 | $title = $result->getTitle(); |
||
| 175 | if ( $resultPageSet === null ) { |
||
| 176 | $vals = []; |
||
| 177 | ApiQueryBase::addTitleInfo( $vals, $title ); |
||
| 178 | |||
| 179 | if ( isset( $prop['snippet'] ) ) { |
||
| 180 | $vals['snippet'] = $result->getTextSnippet( $terms ); |
||
| 181 | } |
||
| 182 | if ( isset( $prop['size'] ) ) { |
||
| 183 | $vals['size'] = $result->getByteSize(); |
||
| 184 | } |
||
| 185 | if ( isset( $prop['wordcount'] ) ) { |
||
| 186 | $vals['wordcount'] = $result->getWordCount(); |
||
| 187 | } |
||
| 188 | if ( isset( $prop['timestamp'] ) ) { |
||
| 189 | $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() ); |
||
| 190 | } |
||
| 191 | if ( isset( $prop['titlesnippet'] ) ) { |
||
| 192 | $vals['titlesnippet'] = $result->getTitleSnippet(); |
||
| 193 | } |
||
| 194 | if ( isset( $prop['categorysnippet'] ) ) { |
||
| 195 | $vals['categorysnippet'] = $result->getCategorySnippet(); |
||
| 196 | } |
||
| 197 | if ( !is_null( $result->getRedirectTitle() ) ) { |
||
| 198 | if ( isset( $prop['redirecttitle'] ) ) { |
||
| 199 | $vals['redirecttitle'] = $result->getRedirectTitle()->getPrefixedText(); |
||
| 200 | } |
||
| 201 | if ( isset( $prop['redirectsnippet'] ) ) { |
||
| 202 | $vals['redirectsnippet'] = $result->getRedirectSnippet(); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | if ( !is_null( $result->getSectionTitle() ) ) { |
||
| 206 | if ( isset( $prop['sectiontitle'] ) ) { |
||
| 207 | $vals['sectiontitle'] = $result->getSectionTitle()->getFragment(); |
||
| 208 | } |
||
| 209 | if ( isset( $prop['sectionsnippet'] ) ) { |
||
| 210 | $vals['sectionsnippet'] = $result->getSectionSnippet(); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | if ( isset( $prop['isfilematch'] ) ) { |
||
| 214 | $vals['isfilematch'] = $result->isFileMatch(); |
||
| 215 | } |
||
| 216 | |||
| 217 | // Add item to results and see whether it fits |
||
| 218 | $fit = $apiResult->addValue( [ 'query', $this->getModuleName() ], |
||
| 219 | null, $vals ); |
||
| 220 | if ( !$fit ) { |
||
| 221 | $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 ); |
||
| 222 | break; |
||
| 223 | } |
||
| 224 | } else { |
||
| 225 | $titles[] = $title; |
||
| 226 | } |
||
| 227 | |||
| 228 | $result = $matches->next(); |
||
| 229 | } |
||
| 230 | |||
| 231 | $hasInterwikiResults = false; |
||
| 232 | $totalhits = null; |
||
| 233 | if ( $interwiki && $resultPageSet === null && $matches->hasInterwikiResults() ) { |
||
| 234 | foreach ( $matches->getInterwikiResults() as $matches ) { |
||
| 235 | $matches = $matches->getInterwikiResults(); |
||
| 236 | $hasInterwikiResults = true; |
||
| 237 | |||
| 238 | // Include number of results if requested |
||
| 239 | if ( $resultPageSet === null && isset( $searchInfo['totalhits'] ) ) { |
||
| 240 | $totalhits += $matches->getTotalHits(); |
||
| 241 | } |
||
| 242 | |||
| 243 | $result = $matches->next(); |
||
| 244 | while ( $result ) { |
||
| 245 | $title = $result->getTitle(); |
||
| 246 | |||
| 247 | if ( $resultPageSet === null ) { |
||
| 248 | $vals = [ |
||
| 249 | 'namespace' => $result->getInterwikiNamespaceText(), |
||
| 250 | 'title' => $title->getText(), |
||
| 251 | 'url' => $title->getFullUrl(), |
||
| 252 | ]; |
||
| 253 | |||
| 254 | // Add item to results and see whether it fits |
||
| 255 | $fit = $apiResult->addValue( |
||
| 256 | [ 'query', 'interwiki' . $this->getModuleName(), $result->getInterwikiPrefix() ], |
||
| 257 | null, |
||
| 258 | $vals |
||
| 259 | ); |
||
| 260 | |||
| 261 | if ( !$fit ) { |
||
| 262 | // We hit the limit. We can't really provide any meaningful |
||
| 263 | // pagination info so just bail out |
||
| 264 | break; |
||
| 265 | } |
||
| 266 | } else { |
||
| 267 | $titles[] = $title; |
||
| 268 | } |
||
| 269 | |||
| 270 | $result = $matches->next(); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | if ( $totalhits !== null ) { |
||
| 274 | $apiResult->addValue( [ 'query', 'interwikisearchinfo' ], |
||
| 275 | 'totalhits', $totalhits ); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | if ( $resultPageSet === null ) { |
||
| 280 | $apiResult->addIndexedTagName( [ |
||
| 281 | 'query', $this->getModuleName() |
||
| 282 | ], 'p' ); |
||
| 283 | if ( $hasInterwikiResults ) { |
||
| 284 | $apiResult->addIndexedTagName( [ |
||
| 285 | 'query', 'interwiki' . $this->getModuleName() |
||
| 286 | ], 'p' ); |
||
| 287 | } |
||
| 288 | } else { |
||
| 289 | View Code Duplication | $resultPageSet->setRedirectMergePolicy( function ( $current, $new ) { |
|
| 290 | if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) { |
||
| 291 | $current['index'] = $new['index']; |
||
| 292 | } |
||
| 293 | return $current; |
||
| 294 | } ); |
||
| 295 | $resultPageSet->populateFromTitles( $titles ); |
||
| 296 | $offset = $params['offset'] + 1; |
||
| 297 | foreach ( $titles as $index => $title ) { |
||
| 298 | $resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset ] ); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 421 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: