Conditions | 45 |
Paths | > 20000 |
Total Lines | 365 |
Code Lines | 233 |
Lines | 50 |
Ratio | 13.7 % |
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 |
||
141 | function showForm( $err ) { |
||
142 | $this->getSkin()->setRelevantTitle( $this->oldTitle ); |
||
143 | |||
144 | $out = $this->getOutput(); |
||
145 | $out->setPageTitle( $this->msg( 'move-page', $this->oldTitle->getPrefixedText() ) ); |
||
146 | $out->addModules( 'mediawiki.special.movePage' ); |
||
147 | $out->addModuleStyles( 'mediawiki.special.movePage.styles' ); |
||
148 | $this->addHelpLink( 'Help:Moving a page' ); |
||
149 | |||
150 | $out->addWikiMsg( $this->getConfig()->get( 'FixDoubleRedirects' ) ? |
||
151 | 'movepagetext' : |
||
152 | 'movepagetext-noredirectfixer' |
||
153 | ); |
||
154 | |||
155 | if ( $this->oldTitle->getNamespace() == NS_USER && !$this->oldTitle->isSubpage() ) { |
||
156 | $out->wrapWikiMsg( |
||
157 | "<div class=\"warningbox mw-moveuserpage-warning\">\n$1\n</div>", |
||
158 | 'moveuserpage-warning' |
||
159 | ); |
||
160 | } elseif ( $this->oldTitle->getNamespace() == NS_CATEGORY ) { |
||
161 | $out->wrapWikiMsg( |
||
162 | "<div class=\"warningbox mw-movecategorypage-warning\">\n$1\n</div>", |
||
163 | 'movecategorypage-warning' |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | $deleteAndMove = false; |
||
168 | $moveOverShared = false; |
||
169 | |||
170 | $newTitle = $this->newTitle; |
||
171 | |||
172 | if ( !$newTitle ) { |
||
173 | # Show the current title as a default |
||
174 | # when the form is first opened. |
||
175 | $newTitle = $this->oldTitle; |
||
176 | } elseif ( !count( $err ) ) { |
||
177 | # If a title was supplied, probably from the move log revert |
||
178 | # link, check for validity. We can then show some diagnostic |
||
179 | # information and save a click. |
||
180 | $newerr = $this->oldTitle->isValidMoveOperation( $newTitle ); |
||
181 | if ( is_array( $newerr ) ) { |
||
182 | $err = $newerr; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | $user = $this->getUser(); |
||
187 | |||
188 | View Code Duplication | if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'articleexists' |
|
189 | && $newTitle->quickUserCan( 'delete', $user ) |
||
190 | ) { |
||
191 | $out->wrapWikiMsg( |
||
192 | "<div class='warningbox'>\n$1\n</div>\n", |
||
193 | [ 'delete_and_move_text', $newTitle->getPrefixedText() ] |
||
194 | ); |
||
195 | $deleteAndMove = true; |
||
196 | $err = []; |
||
197 | } |
||
198 | |||
199 | View Code Duplication | if ( count( $err ) == 1 && isset( $err[0][0] ) && $err[0][0] == 'file-exists-sharedrepo' |
|
200 | && $user->isAllowed( 'reupload-shared' ) |
||
201 | ) { |
||
202 | $out->wrapWikiMsg( |
||
203 | "<div class='warningbox'>\n$1\n</div>\n", |
||
204 | [ |
||
205 | 'move-over-sharedrepo', |
||
206 | $newTitle->getPrefixedText() |
||
207 | ] |
||
208 | ); |
||
209 | $moveOverShared = true; |
||
210 | $err = []; |
||
211 | } |
||
212 | |||
213 | $oldTalk = $this->oldTitle->getTalkPage(); |
||
214 | $oldTitleSubpages = $this->oldTitle->hasSubpages(); |
||
215 | $oldTitleTalkSubpages = $this->oldTitle->getTalkPage()->hasSubpages(); |
||
216 | |||
217 | $canMoveSubpage = ( $oldTitleSubpages || $oldTitleTalkSubpages ) && |
||
218 | !count( $this->oldTitle->getUserPermissionsErrors( 'move-subpages', $user ) ); |
||
219 | |||
220 | # We also want to be able to move assoc. subpage talk-pages even if base page |
||
221 | # has no associated talk page, so || with $oldTitleTalkSubpages. |
||
222 | $considerTalk = !$this->oldTitle->isTalkPage() && |
||
223 | ( $oldTalk->exists() |
||
224 | || ( $oldTitleTalkSubpages && $canMoveSubpage ) ); |
||
225 | |||
226 | $dbr = wfGetDB( DB_REPLICA ); |
||
227 | if ( $this->getConfig()->get( 'FixDoubleRedirects' ) ) { |
||
228 | $hasRedirects = $dbr->selectField( 'redirect', '1', |
||
229 | [ |
||
230 | 'rd_namespace' => $this->oldTitle->getNamespace(), |
||
231 | 'rd_title' => $this->oldTitle->getDBkey(), |
||
232 | ], __METHOD__ ); |
||
233 | } else { |
||
234 | $hasRedirects = false; |
||
235 | } |
||
236 | |||
237 | if ( count( $err ) ) { |
||
238 | $out->addHTML( "<div class='errorbox'>\n" ); |
||
239 | $action_desc = $this->msg( 'action-move' )->plain(); |
||
240 | $out->addWikiMsg( 'permissionserrorstext-withaction', count( $err ), $action_desc ); |
||
241 | |||
242 | if ( count( $err ) == 1 ) { |
||
243 | $errMsg = $err[0]; |
||
244 | $errMsgName = array_shift( $errMsg ); |
||
245 | |||
246 | if ( $errMsgName == 'hookaborted' ) { |
||
247 | $out->addHTML( "<p>{$errMsg[0]}</p>\n" ); |
||
248 | } else { |
||
249 | $out->addWikiMsgArray( $errMsgName, $errMsg ); |
||
250 | } |
||
251 | } else { |
||
252 | $errStr = []; |
||
253 | |||
254 | foreach ( $err as $errMsg ) { |
||
255 | if ( $errMsg[0] == 'hookaborted' ) { |
||
256 | $errStr[] = $errMsg[1]; |
||
257 | } else { |
||
258 | $errMsgName = array_shift( $errMsg ); |
||
259 | $errStr[] = $this->msg( $errMsgName, $errMsg )->parse(); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | $out->addHTML( '<ul><li>' . implode( "</li>\n<li>", $errStr ) . "</li></ul>\n" ); |
||
264 | } |
||
265 | $out->addHTML( "</div>\n" ); |
||
266 | } |
||
267 | |||
268 | if ( $this->oldTitle->isProtected( 'move' ) ) { |
||
269 | # Is the title semi-protected? |
||
270 | if ( $this->oldTitle->isSemiProtected( 'move' ) ) { |
||
271 | $noticeMsg = 'semiprotectedpagemovewarning'; |
||
272 | $classes[] = 'mw-textarea-sprotected'; |
||
|
|||
273 | } else { |
||
274 | # Then it must be protected based on static groups (regular) |
||
275 | $noticeMsg = 'protectedpagemovewarning'; |
||
276 | $classes[] = 'mw-textarea-protected'; |
||
277 | } |
||
278 | $out->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" ); |
||
279 | $out->addWikiMsg( $noticeMsg ); |
||
280 | LogEventsList::showLogExtract( |
||
281 | $out, |
||
282 | 'protect', |
||
283 | $this->oldTitle, |
||
284 | '', |
||
285 | [ 'lim' => 1 ] |
||
286 | ); |
||
287 | $out->addHTML( "</div>\n" ); |
||
288 | } |
||
289 | |||
290 | // Byte limit (not string length limit) for wpReason and wpNewTitleMain |
||
291 | // is enforced in the mediawiki.special.movePage module |
||
292 | |||
293 | $immovableNamespaces = []; |
||
294 | foreach ( array_keys( $this->getLanguage()->getNamespaces() ) as $nsId ) { |
||
295 | if ( !MWNamespace::isMovable( $nsId ) ) { |
||
296 | $immovableNamespaces[] = $nsId; |
||
297 | } |
||
298 | } |
||
299 | |||
300 | $handler = ContentHandler::getForTitle( $this->oldTitle ); |
||
301 | |||
302 | $out->enableOOUI(); |
||
303 | $fields = []; |
||
304 | |||
305 | $fields[] = new OOUI\FieldLayout( |
||
306 | new MediaWiki\Widget\ComplexTitleInputWidget( [ |
||
307 | 'id' => 'wpNewTitle', |
||
308 | 'namespace' => [ |
||
309 | 'id' => 'wpNewTitleNs', |
||
310 | 'name' => 'wpNewTitleNs', |
||
311 | 'value' => $newTitle->getNamespace(), |
||
312 | 'exclude' => $immovableNamespaces, |
||
313 | ], |
||
314 | 'title' => [ |
||
315 | 'id' => 'wpNewTitleMain', |
||
316 | 'name' => 'wpNewTitleMain', |
||
317 | 'value' => $newTitle->getText(), |
||
318 | // Inappropriate, since we're expecting the user to input a non-existent page's title |
||
319 | 'suggestions' => false, |
||
320 | ], |
||
321 | 'infusable' => true, |
||
322 | ] ), |
||
323 | [ |
||
324 | 'label' => $this->msg( 'newtitle' )->text(), |
||
325 | 'align' => 'top', |
||
326 | ] |
||
327 | ); |
||
328 | |||
329 | $fields[] = new OOUI\FieldLayout( |
||
330 | new OOUI\TextInputWidget( [ |
||
331 | 'name' => 'wpReason', |
||
332 | 'id' => 'wpReason', |
||
333 | 'maxLength' => 200, |
||
334 | 'infusable' => true, |
||
335 | 'value' => $this->reason, |
||
336 | ] ), |
||
337 | [ |
||
338 | 'label' => $this->msg( 'movereason' )->text(), |
||
339 | 'align' => 'top', |
||
340 | ] |
||
341 | ); |
||
342 | |||
343 | if ( $considerTalk ) { |
||
344 | $fields[] = new OOUI\FieldLayout( |
||
345 | new OOUI\CheckboxInputWidget( [ |
||
346 | 'name' => 'wpMovetalk', |
||
347 | 'id' => 'wpMovetalk', |
||
348 | 'value' => '1', |
||
349 | 'selected' => $this->moveTalk, |
||
350 | ] ), |
||
351 | [ |
||
352 | 'label' => $this->msg( 'movetalk' )->text(), |
||
353 | 'help' => new OOUI\HtmlSnippet( $this->msg( 'movepagetalktext' )->parseAsBlock() ), |
||
354 | 'align' => 'inline', |
||
355 | 'infusable' => true, |
||
356 | 'id' => 'wpMovetalk-field', |
||
357 | ] |
||
358 | ); |
||
359 | } |
||
360 | |||
361 | if ( $user->isAllowed( 'suppressredirect' ) ) { |
||
362 | if ( $handler->supportsRedirects() ) { |
||
363 | $isChecked = $this->leaveRedirect; |
||
364 | $isDisabled = false; |
||
365 | } else { |
||
366 | $isChecked = false; |
||
367 | $isDisabled = true; |
||
368 | } |
||
369 | $fields[] = new OOUI\FieldLayout( |
||
370 | new OOUI\CheckboxInputWidget( [ |
||
371 | 'name' => 'wpLeaveRedirect', |
||
372 | 'id' => 'wpLeaveRedirect', |
||
373 | 'value' => '1', |
||
374 | 'selected' => $isChecked, |
||
375 | 'disabled' => $isDisabled, |
||
376 | ] ), |
||
377 | [ |
||
378 | 'label' => $this->msg( 'move-leave-redirect' )->text(), |
||
379 | 'align' => 'inline', |
||
380 | ] |
||
381 | ); |
||
382 | } |
||
383 | |||
384 | View Code Duplication | if ( $hasRedirects ) { |
|
385 | $fields[] = new OOUI\FieldLayout( |
||
386 | new OOUI\CheckboxInputWidget( [ |
||
387 | 'name' => 'wpFixRedirects', |
||
388 | 'id' => 'wpFixRedirects', |
||
389 | 'value' => '1', |
||
390 | 'selected' => $this->fixRedirects, |
||
391 | ] ), |
||
392 | [ |
||
393 | 'label' => $this->msg( 'fix-double-redirects' )->text(), |
||
394 | 'align' => 'inline', |
||
395 | ] |
||
396 | ); |
||
397 | } |
||
398 | |||
399 | if ( $canMoveSubpage ) { |
||
400 | $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' ); |
||
401 | $fields[] = new OOUI\FieldLayout( |
||
402 | new OOUI\CheckboxInputWidget( [ |
||
403 | 'name' => 'wpMovesubpages', |
||
404 | 'id' => 'wpMovesubpages', |
||
405 | 'value' => '1', |
||
406 | # Don't check the box if we only have talk subpages to |
||
407 | # move and we aren't moving the talk page. |
||
408 | 'selected' => $this->moveSubpages && ( $this->oldTitle->hasSubpages() || $this->moveTalk ), |
||
409 | ] ), |
||
410 | [ |
||
411 | 'label' => new OOUI\HtmlSnippet( |
||
412 | $this->msg( |
||
413 | ( $this->oldTitle->hasSubpages() |
||
414 | ? 'move-subpages' |
||
415 | : 'move-talk-subpages' ) |
||
416 | )->numParams( $maximumMovedPages )->params( $maximumMovedPages )->parse() |
||
417 | ), |
||
418 | 'align' => 'inline', |
||
419 | ] |
||
420 | ); |
||
421 | } |
||
422 | |||
423 | # Don't allow watching if user is not logged in |
||
424 | if ( $user->isLoggedIn() ) { |
||
425 | $watchChecked = $user->isLoggedIn() && ( $this->watch || $user->getBoolOption( 'watchmoves' ) |
||
426 | || $user->isWatched( $this->oldTitle ) ); |
||
427 | $fields[] = new OOUI\FieldLayout( |
||
428 | new OOUI\CheckboxInputWidget( [ |
||
429 | 'name' => 'wpWatch', |
||
430 | 'id' => 'watch', # ew |
||
431 | 'value' => '1', |
||
432 | 'selected' => $watchChecked, |
||
433 | ] ), |
||
434 | [ |
||
435 | 'label' => $this->msg( 'move-watch' )->text(), |
||
436 | 'align' => 'inline', |
||
437 | ] |
||
438 | ); |
||
439 | } |
||
440 | |||
441 | $hiddenFields = ''; |
||
442 | if ( $moveOverShared ) { |
||
443 | $hiddenFields .= Html::hidden( 'wpMoveOverSharedFile', '1' ); |
||
444 | } |
||
445 | |||
446 | View Code Duplication | if ( $deleteAndMove ) { |
|
447 | $fields[] = new OOUI\FieldLayout( |
||
448 | new OOUI\CheckboxInputWidget( [ |
||
449 | 'name' => 'wpDeleteAndMove', |
||
450 | 'id' => 'wpDeleteAndMove', |
||
451 | 'value' => '1', |
||
452 | ] ), |
||
453 | [ |
||
454 | 'label' => $this->msg( 'delete_and_move_confirm' )->text(), |
||
455 | 'align' => 'inline', |
||
456 | ] |
||
457 | ); |
||
458 | } |
||
459 | |||
460 | $fields[] = new OOUI\FieldLayout( |
||
461 | new OOUI\ButtonInputWidget( [ |
||
462 | 'name' => 'wpMove', |
||
463 | 'value' => $this->msg( 'movepagebtn' )->text(), |
||
464 | 'label' => $this->msg( 'movepagebtn' )->text(), |
||
465 | 'flags' => [ 'primary', 'progressive' ], |
||
466 | 'type' => 'submit', |
||
467 | ] ), |
||
468 | [ |
||
469 | 'align' => 'top', |
||
470 | ] |
||
471 | ); |
||
472 | |||
473 | $fieldset = new OOUI\FieldsetLayout( [ |
||
474 | 'label' => $this->msg( 'move-page-legend' )->text(), |
||
475 | 'id' => 'mw-movepage-table', |
||
476 | 'items' => $fields, |
||
477 | ] ); |
||
478 | |||
479 | $form = new OOUI\FormLayout( [ |
||
480 | 'method' => 'post', |
||
481 | 'action' => $this->getPageTitle()->getLocalURL( 'action=submit' ), |
||
482 | 'id' => 'movepage', |
||
483 | ] ); |
||
484 | $form->appendContent( |
||
485 | $fieldset, |
||
486 | new OOUI\HtmlSnippet( |
||
487 | $hiddenFields . |
||
488 | Html::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) . |
||
489 | Html::hidden( 'wpEditToken', $user->getEditToken() ) |
||
490 | ) |
||
491 | ); |
||
492 | |||
493 | $out->addHTML( |
||
494 | new OOUI\PanelLayout( [ |
||
495 | 'classes' => [ 'movepage-wrapper' ], |
||
496 | 'expanded' => false, |
||
497 | 'padded' => true, |
||
498 | 'framed' => true, |
||
499 | 'content' => $form, |
||
500 | ] ) |
||
501 | ); |
||
502 | |||
503 | $this->showLogFragment( $this->oldTitle ); |
||
504 | $this->showSubpages( $this->oldTitle ); |
||
505 | } |
||
506 | |||
860 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.