| Conditions | 26 |
| Paths | 11520 |
| Total Lines | 115 |
| Code Lines | 69 |
| Lines | 18 |
| Ratio | 15.65 % |
| 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 |
||
| 33 | public function execute() { |
||
| 34 | $this->useTransactionalTimeLimit(); |
||
| 35 | |||
| 36 | $user = $this->getUser(); |
||
| 37 | $params = $this->extractRequestParams(); |
||
| 38 | |||
| 39 | $this->requireOnlyOneParameter( $params, 'from', 'fromid' ); |
||
| 40 | |||
| 41 | View Code Duplication | if ( isset( $params['from'] ) ) { |
|
| 42 | $fromTitle = Title::newFromText( $params['from'] ); |
||
| 43 | if ( !$fromTitle || $fromTitle->isExternal() ) { |
||
| 44 | $this->dieUsageMsg( [ 'invalidtitle', $params['from'] ] ); |
||
| 45 | } |
||
| 46 | } elseif ( isset( $params['fromid'] ) ) { |
||
| 47 | $fromTitle = Title::newFromID( $params['fromid'] ); |
||
| 48 | if ( !$fromTitle ) { |
||
| 49 | $this->dieUsageMsg( [ 'nosuchpageid', $params['fromid'] ] ); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | if ( !$fromTitle->exists() ) { |
||
|
|
|||
| 54 | $this->dieUsageMsg( 'notanarticle' ); |
||
| 55 | } |
||
| 56 | $fromTalk = $fromTitle->getTalkPage(); |
||
| 57 | |||
| 58 | $toTitle = Title::newFromText( $params['to'] ); |
||
| 59 | if ( !$toTitle || $toTitle->isExternal() ) { |
||
| 60 | $this->dieUsageMsg( [ 'invalidtitle', $params['to'] ] ); |
||
| 61 | } |
||
| 62 | $toTalk = $toTitle->getTalkPage(); |
||
| 63 | |||
| 64 | if ( $toTitle->getNamespace() == NS_FILE |
||
| 65 | && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle ) |
||
| 66 | && wfFindFile( $toTitle ) |
||
| 67 | ) { |
||
| 68 | if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) { |
||
| 69 | $this->dieUsageMsg( 'sharedfile-exists' ); |
||
| 70 | } elseif ( !$user->isAllowed( 'reupload-shared' ) ) { |
||
| 71 | $this->dieUsageMsg( 'cantoverwrite-sharedfile' ); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | // Rate limit |
||
| 76 | if ( $user->pingLimiter( 'move' ) ) { |
||
| 77 | $this->dieUsageMsg( 'actionthrottledtext' ); |
||
| 78 | } |
||
| 79 | |||
| 80 | // Move the page |
||
| 81 | $toTitleExists = $toTitle->exists(); |
||
| 82 | $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'] ); |
||
| 83 | if ( !$status->isOK() ) { |
||
| 84 | $this->dieStatus( $status ); |
||
| 85 | } |
||
| 86 | |||
| 87 | $r = [ |
||
| 88 | 'from' => $fromTitle->getPrefixedText(), |
||
| 89 | 'to' => $toTitle->getPrefixedText(), |
||
| 90 | 'reason' => $params['reason'] |
||
| 91 | ]; |
||
| 92 | |||
| 93 | // NOTE: we assume that if the old title exists, it's because it was re-created as |
||
| 94 | // a redirect to the new title. This is not safe, but what we did before was |
||
| 95 | // even worse: we just determined whether a redirect should have been created, |
||
| 96 | // and reported that it was created if it should have, without any checks. |
||
| 97 | // Also note that isRedirect() is unreliable because of bug 37209. |
||
| 98 | $r['redirectcreated'] = $fromTitle->exists(); |
||
| 99 | |||
| 100 | $r['moveoverredirect'] = $toTitleExists; |
||
| 101 | |||
| 102 | // Move the talk page |
||
| 103 | if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) { |
||
| 104 | $toTalkExists = $toTalk->exists(); |
||
| 105 | $status = $this->movePage( $fromTalk, $toTalk, $params['reason'], !$params['noredirect'] ); |
||
| 106 | if ( $status->isOK() ) { |
||
| 107 | $r['talkfrom'] = $fromTalk->getPrefixedText(); |
||
| 108 | $r['talkto'] = $toTalk->getPrefixedText(); |
||
| 109 | $r['talkmoveoverredirect'] = $toTalkExists; |
||
| 110 | } else { |
||
| 111 | // We're not gonna dieUsage() on failure, since we already changed something |
||
| 112 | $error = $this->getErrorFromStatus( $status ); |
||
| 113 | $r['talkmove-error-code'] = $error[0]; |
||
| 114 | $r['talkmove-error-info'] = $error[1]; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | $result = $this->getResult(); |
||
| 119 | |||
| 120 | // Move subpages |
||
| 121 | if ( $params['movesubpages'] ) { |
||
| 122 | $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle, |
||
| 123 | $params['reason'], $params['noredirect'] ); |
||
| 124 | ApiResult::setIndexedTagName( $r['subpages'], 'subpage' ); |
||
| 125 | |||
| 126 | if ( $params['movetalk'] ) { |
||
| 127 | $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk, |
||
| 128 | $params['reason'], $params['noredirect'] ); |
||
| 129 | ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' ); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | $watch = 'preferences'; |
||
| 134 | View Code Duplication | if ( isset( $params['watchlist'] ) ) { |
|
| 135 | $watch = $params['watchlist']; |
||
| 136 | } elseif ( $params['watch'] ) { |
||
| 137 | $watch = 'watch'; |
||
| 138 | } elseif ( $params['unwatch'] ) { |
||
| 139 | $watch = 'unwatch'; |
||
| 140 | } |
||
| 141 | |||
| 142 | // Watch pages |
||
| 143 | $this->setWatch( $watch, $fromTitle, 'watchmoves' ); |
||
| 144 | $this->setWatch( $watch, $toTitle, 'watchmoves' ); |
||
| 145 | |||
| 146 | $result->addValue( null, $this->getModuleName(), $r ); |
||
| 147 | } |
||
| 148 | |||
| 265 |
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: