Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ApiMove often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApiMove, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ApiMove extends ApiBase { |
||
| 32 | |||
| 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 | |||
| 149 | /** |
||
| 150 | * @param Title $from |
||
| 151 | * @param Title $to |
||
| 152 | * @param string $reason |
||
| 153 | * @param bool $createRedirect |
||
| 154 | * @return Status |
||
| 155 | */ |
||
| 156 | protected function movePage( Title $from, Title $to, $reason, $createRedirect ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param Title $fromTitle |
||
| 178 | * @param Title $toTitle |
||
| 179 | * @param string $reason |
||
| 180 | * @param bool $noredirect |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) { |
||
| 205 | |||
| 206 | public function mustBePosted() { |
||
| 209 | |||
| 210 | public function isWriteMode() { |
||
| 213 | |||
| 214 | public function getAllowedParams() { |
||
| 248 | |||
| 249 | public function needsToken() { |
||
| 252 | |||
| 253 | protected function getExamplesMessages() { |
||
| 260 | |||
| 261 | public function getHelpUrls() { |
||
| 264 | } |
||
| 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: