| Conditions | 12 |
| Paths | 50 |
| Total Lines | 52 |
| Code Lines | 33 |
| Lines | 22 |
| Ratio | 42.31 % |
| 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 | $params = $this->extractRequestParams(); |
||
| 37 | |||
| 38 | $this->requireOnlyOneParameter( $params, 'from', 'fromid' ); |
||
| 39 | $this->requireOnlyOneParameter( $params, 'to', 'toid' ); |
||
| 40 | |||
| 41 | // Get page objects (nonexistant pages get caught in MergeHistory::isValidMerge()) |
||
| 42 | View Code Duplication | if ( isset( $params['from'] ) ) { |
|
| 43 | $fromTitle = Title::newFromText( $params['from'] ); |
||
| 44 | if ( !$fromTitle || $fromTitle->isExternal() ) { |
||
| 45 | $this->dieUsageMsg( [ 'invalidtitle', $params['from'] ] ); |
||
| 46 | } |
||
| 47 | } elseif ( isset( $params['fromid'] ) ) { |
||
| 48 | $fromTitle = Title::newFromID( $params['fromid'] ); |
||
| 49 | if ( !$fromTitle ) { |
||
| 50 | $this->dieUsageMsg( [ 'nosuchpageid', $params['fromid'] ] ); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | View Code Duplication | if ( isset( $params['to'] ) ) { |
|
| 55 | $toTitle = Title::newFromText( $params['to'] ); |
||
| 56 | if ( !$toTitle || $toTitle->isExternal() ) { |
||
| 57 | $this->dieUsageMsg( [ 'invalidtitle', $params['to'] ] ); |
||
| 58 | } |
||
| 59 | } elseif ( isset( $params['toid'] ) ) { |
||
| 60 | $toTitle = Title::newFromID( $params['toid'] ); |
||
| 61 | if ( !$toTitle ) { |
||
| 62 | $this->dieUsageMsg( [ 'nosuchpageid', $params['toid'] ] ); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | $reason = $params['reason']; |
||
| 67 | $timestamp = $params['timestamp']; |
||
| 68 | |||
| 69 | // Merge! |
||
| 70 | $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason ); |
||
| 71 | if ( !$status->isOK() ) { |
||
| 72 | $this->dieStatus( $status ); |
||
| 73 | } |
||
| 74 | |||
| 75 | $r = [ |
||
| 76 | 'from' => $fromTitle->getPrefixedText(), |
||
| 77 | 'to' => $toTitle->getPrefixedText(), |
||
| 78 | 'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ), |
||
| 79 | 'reason' => $params['reason'] |
||
| 80 | ]; |
||
| 81 | $result = $this->getResult(); |
||
| 82 | |||
| 83 | $result->addValue( null, $this->getModuleName(), $r ); |
||
| 84 | } |
||
| 85 | |||
| 143 |
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: