Conditions | 61 |
Paths | > 20000 |
Total Lines | 257 |
Code Lines | 169 |
Lines | 27 |
Ratio | 10.51 % |
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 |
||
160 | protected function extractRevisionInfo( Revision $revision, $row ) { |
||
161 | $title = $revision->getTitle(); |
||
162 | $user = $this->getUser(); |
||
163 | $vals = []; |
||
164 | $anyHidden = false; |
||
165 | |||
166 | if ( $this->fld_ids ) { |
||
167 | $vals['revid'] = intval( $revision->getId() ); |
||
168 | if ( !is_null( $revision->getParentId() ) ) { |
||
169 | $vals['parentid'] = intval( $revision->getParentId() ); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | if ( $this->fld_flags ) { |
||
174 | $vals['minor'] = $revision->isMinor(); |
||
175 | } |
||
176 | |||
177 | if ( $this->fld_user || $this->fld_userid ) { |
||
178 | if ( $revision->isDeleted( Revision::DELETED_USER ) ) { |
||
179 | $vals['userhidden'] = true; |
||
180 | $anyHidden = true; |
||
181 | } |
||
182 | if ( $revision->userCan( Revision::DELETED_USER, $user ) ) { |
||
183 | if ( $this->fld_user ) { |
||
184 | $vals['user'] = $revision->getUserText( Revision::RAW ); |
||
185 | } |
||
186 | $userid = $revision->getUser( Revision::RAW ); |
||
187 | if ( !$userid ) { |
||
188 | $vals['anon'] = true; |
||
189 | } |
||
190 | |||
191 | if ( $this->fld_userid ) { |
||
192 | $vals['userid'] = $userid; |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | if ( $this->fld_timestamp ) { |
||
198 | $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() ); |
||
199 | } |
||
200 | |||
201 | if ( $this->fld_size ) { |
||
202 | if ( !is_null( $revision->getSize() ) ) { |
||
203 | $vals['size'] = intval( $revision->getSize() ); |
||
204 | } else { |
||
205 | $vals['size'] = 0; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | if ( $this->fld_sha1 ) { |
||
210 | if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) { |
||
211 | $vals['sha1hidden'] = true; |
||
212 | $anyHidden = true; |
||
213 | } |
||
214 | if ( $revision->userCan( Revision::DELETED_TEXT, $user ) ) { |
||
215 | if ( $revision->getSha1() != '' ) { |
||
216 | $vals['sha1'] = Wikimedia\base_convert( $revision->getSha1(), 36, 16, 40 ); |
||
217 | } else { |
||
218 | $vals['sha1'] = ''; |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | |||
223 | if ( $this->fld_contentmodel ) { |
||
224 | $vals['contentmodel'] = $revision->getContentModel(); |
||
225 | } |
||
226 | |||
227 | if ( $this->fld_comment || $this->fld_parsedcomment ) { |
||
228 | if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) { |
||
229 | $vals['commenthidden'] = true; |
||
230 | $anyHidden = true; |
||
231 | } |
||
232 | if ( $revision->userCan( Revision::DELETED_COMMENT, $user ) ) { |
||
233 | $comment = $revision->getComment( Revision::RAW ); |
||
234 | |||
235 | if ( $this->fld_comment ) { |
||
236 | $vals['comment'] = $comment; |
||
237 | } |
||
238 | |||
239 | if ( $this->fld_parsedcomment ) { |
||
240 | $vals['parsedcomment'] = Linker::formatComment( $comment, $title ); |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | |||
245 | View Code Duplication | if ( $this->fld_tags ) { |
|
246 | if ( $row->ts_tags ) { |
||
247 | $tags = explode( ',', $row->ts_tags ); |
||
248 | ApiResult::setIndexedTagName( $tags, 'tag' ); |
||
249 | $vals['tags'] = $tags; |
||
250 | } else { |
||
251 | $vals['tags'] = []; |
||
252 | } |
||
253 | } |
||
254 | |||
255 | $content = null; |
||
256 | global $wgParser; |
||
257 | if ( $this->fetchContent ) { |
||
258 | $content = $revision->getContent( Revision::FOR_THIS_USER, $this->getUser() ); |
||
259 | // Expand templates after getting section content because |
||
260 | // template-added sections don't count and Parser::preprocess() |
||
261 | // will have less input |
||
262 | if ( $content && $this->section !== false ) { |
||
263 | $content = $content->getSection( $this->section, false ); |
||
264 | if ( !$content ) { |
||
265 | $this->dieUsage( |
||
266 | "There is no section {$this->section} in r" . $revision->getId(), |
||
267 | 'nosuchsection' |
||
268 | ); |
||
269 | } |
||
270 | } |
||
271 | if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) { |
||
272 | $vals['texthidden'] = true; |
||
273 | $anyHidden = true; |
||
274 | } elseif ( !$content ) { |
||
275 | $vals['textmissing'] = true; |
||
276 | } |
||
277 | } |
||
278 | if ( $this->fld_parsetree || ( $this->fld_content && $this->generateXML ) ) { |
||
279 | if ( $content ) { |
||
280 | if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) { |
||
281 | $t = $content->getNativeData(); # note: don't set $text |
||
282 | |||
283 | $wgParser->startExternalParse( |
||
284 | $title, |
||
285 | ParserOptions::newFromContext( $this->getContext() ), |
||
286 | Parser::OT_PREPROCESS |
||
287 | ); |
||
288 | $dom = $wgParser->preprocessToDom( $t ); |
||
289 | View Code Duplication | if ( is_callable( [ $dom, 'saveXML' ] ) ) { |
|
290 | $xml = $dom->saveXML(); |
||
291 | } else { |
||
292 | $xml = $dom->__toString(); |
||
293 | } |
||
294 | $vals['parsetree'] = $xml; |
||
295 | View Code Duplication | } else { |
|
296 | $vals['badcontentformatforparsetree'] = true; |
||
297 | $this->setWarning( 'Conversion to XML is supported for wikitext only, ' . |
||
298 | $title->getPrefixedDBkey() . |
||
299 | ' uses content model ' . $content->getModel() ); |
||
300 | } |
||
301 | } |
||
302 | } |
||
303 | |||
304 | if ( $this->fld_content && $content ) { |
||
305 | $text = null; |
||
306 | |||
307 | if ( $this->expandTemplates && !$this->parseContent ) { |
||
308 | # XXX: implement template expansion for all content types in ContentHandler? |
||
309 | if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) { |
||
310 | $text = $content->getNativeData(); |
||
311 | |||
312 | $text = $wgParser->preprocess( |
||
313 | $text, |
||
314 | $title, |
||
315 | ParserOptions::newFromContext( $this->getContext() ) |
||
316 | ); |
||
317 | View Code Duplication | } else { |
|
318 | $this->setWarning( 'Template expansion is supported for wikitext only, ' . |
||
319 | $title->getPrefixedDBkey() . |
||
320 | ' uses content model ' . $content->getModel() ); |
||
321 | $vals['badcontentformat'] = true; |
||
322 | $text = false; |
||
323 | } |
||
324 | } |
||
325 | if ( $this->parseContent ) { |
||
326 | $po = $content->getParserOutput( |
||
327 | $title, |
||
328 | $revision->getId(), |
||
329 | ParserOptions::newFromContext( $this->getContext() ) |
||
330 | ); |
||
331 | $text = $po->getText(); |
||
332 | } |
||
333 | |||
334 | if ( $text === null ) { |
||
335 | $format = $this->contentFormat ?: $content->getDefaultFormat(); |
||
336 | $model = $content->getModel(); |
||
337 | |||
338 | if ( !$content->isSupportedFormat( $format ) ) { |
||
339 | $name = $title->getPrefixedDBkey(); |
||
340 | $this->setWarning( "The requested format {$this->contentFormat} is not " . |
||
341 | "supported for content model $model used by $name" ); |
||
342 | $vals['badcontentformat'] = true; |
||
343 | $text = false; |
||
344 | } else { |
||
345 | $text = $content->serialize( $format ); |
||
346 | // always include format and model. |
||
347 | // Format is needed to deserialize, model is needed to interpret. |
||
348 | $vals['contentformat'] = $format; |
||
349 | $vals['contentmodel'] = $model; |
||
350 | } |
||
351 | } |
||
352 | |||
353 | if ( $text !== false ) { |
||
354 | ApiResult::setContentValue( $vals, 'content', $text ); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | if ( $content && ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) ) { |
||
359 | static $n = 0; // Number of uncached diffs we've had |
||
360 | |||
361 | if ( $n < $this->getConfig()->get( 'APIMaxUncachedDiffs' ) ) { |
||
362 | $vals['diff'] = []; |
||
363 | $context = new DerivativeContext( $this->getContext() ); |
||
364 | $context->setTitle( $title ); |
||
365 | $handler = $revision->getContentHandler(); |
||
366 | |||
367 | if ( !is_null( $this->difftotext ) ) { |
||
368 | $model = $title->getContentModel(); |
||
369 | |||
370 | if ( $this->contentFormat |
||
371 | && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat ) |
||
372 | ) { |
||
373 | $name = $title->getPrefixedDBkey(); |
||
374 | $this->setWarning( "The requested format {$this->contentFormat} is not " . |
||
375 | "supported for content model $model used by $name" ); |
||
376 | $vals['diff']['badcontentformat'] = true; |
||
377 | $engine = null; |
||
378 | } else { |
||
379 | $difftocontent = ContentHandler::makeContent( |
||
380 | $this->difftotext, |
||
381 | $title, |
||
382 | $model, |
||
383 | $this->contentFormat |
||
384 | ); |
||
385 | |||
386 | if ( $this->difftotextpst ) { |
||
387 | $popts = ParserOptions::newFromContext( $this->getContext() ); |
||
388 | $difftocontent = $difftocontent->preSaveTransform( $title, $user, $popts ); |
||
389 | } |
||
390 | |||
391 | $engine = $handler->createDifferenceEngine( $context ); |
||
392 | $engine->setContent( $content, $difftocontent ); |
||
393 | } |
||
394 | } else { |
||
395 | $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto ); |
||
396 | $vals['diff']['from'] = $engine->getOldid(); |
||
397 | $vals['diff']['to'] = $engine->getNewid(); |
||
398 | } |
||
399 | if ( $engine ) { |
||
400 | $difftext = $engine->getDiffBody(); |
||
401 | ApiResult::setContentValue( $vals['diff'], 'body', $difftext ); |
||
402 | if ( !$engine->wasCacheHit() ) { |
||
403 | $n++; |
||
404 | } |
||
405 | } |
||
406 | } else { |
||
407 | $vals['diff']['notcached'] = true; |
||
408 | } |
||
409 | } |
||
410 | |||
411 | if ( $anyHidden && $revision->isDeleted( Revision::DELETED_RESTRICTED ) ) { |
||
412 | $vals['suppressed'] = true; |
||
413 | } |
||
414 | |||
415 | return $vals; |
||
416 | } |
||
417 | |||
505 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.