Conditions | 15 |
Paths | 2304 |
Total Lines | 54 |
Code Lines | 36 |
Lines | 18 |
Ratio | 33.33 % |
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 |
||
38 | public function execute() { |
||
39 | $user = $this->getUser(); |
||
40 | $params = $this->extractRequestParams(); |
||
41 | |||
42 | if ( is_null( $params['id'] ) && is_null( $params['user'] ) ) { |
||
43 | $this->dieUsageMsg( 'unblock-notarget' ); |
||
44 | } |
||
45 | if ( !is_null( $params['id'] ) && !is_null( $params['user'] ) ) { |
||
46 | $this->dieUsageMsg( 'unblock-idanduser' ); |
||
47 | } |
||
48 | |||
49 | if ( !$user->isAllowed( 'block' ) ) { |
||
50 | $this->dieUsageMsg( 'cantunblock' ); |
||
51 | } |
||
52 | # bug 15810: blocked admins should have limited access here |
||
53 | View Code Duplication | if ( $user->isBlocked() ) { |
|
54 | $status = SpecialBlock::checkUnblockSelf( $params['user'], $user ); |
||
55 | if ( $status !== true ) { |
||
56 | $msg = $this->parseMsg( $status ); |
||
|
|||
57 | $this->dieUsage( |
||
58 | $msg['info'], |
||
59 | $msg['code'], |
||
60 | 0, |
||
61 | [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ] |
||
62 | ); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | // Check if user can add tags |
||
67 | View Code Duplication | if ( !is_null( $params['tags'] ) ) { |
|
68 | $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user ); |
||
69 | if ( !$ableToTag->isOK() ) { |
||
70 | $this->dieStatus( $ableToTag ); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | $data = [ |
||
75 | 'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}", |
||
76 | 'Reason' => $params['reason'], |
||
77 | 'Tags' => $params['tags'] |
||
78 | ]; |
||
79 | $block = Block::newFromTarget( $data['Target'] ); |
||
80 | $retval = SpecialUnblock::processUnblock( $data, $this->getContext() ); |
||
81 | if ( $retval !== true ) { |
||
82 | $this->dieUsageMsg( $retval[0] ); |
||
83 | } |
||
84 | |||
85 | $res['id'] = $block->getId(); |
||
86 | $target = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget(); |
||
87 | $res['user'] = $target instanceof User ? $target->getName() : $target; |
||
88 | $res['userid'] = $target instanceof User ? $target->getId() : 0; |
||
89 | $res['reason'] = $params['reason']; |
||
90 | $this->getResult()->addValue( null, $this->getModuleName(), $res ); |
||
91 | } |
||
92 | |||
132 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.