Conditions | 14 |
Paths | 384 |
Total Lines | 91 |
Code Lines | 62 |
Lines | 12 |
Ratio | 13.19 % |
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 |
||
41 | public function execute() { |
||
42 | global $wgContLang; |
||
43 | |||
44 | $user = $this->getUser(); |
||
45 | $params = $this->extractRequestParams(); |
||
46 | |||
47 | if ( !$user->isAllowed( 'block' ) ) { |
||
48 | $this->dieUsageMsg( 'cantblock' ); |
||
49 | } |
||
50 | |||
51 | # bug 15810: blocked admins should have limited access here |
||
52 | View Code Duplication | if ( $user->isBlocked() ) { |
|
53 | $status = SpecialBlock::checkUnblockSelf( $params['user'], $user ); |
||
54 | if ( $status !== true ) { |
||
55 | $msg = $this->parseMsg( $status ); |
||
|
|||
56 | $this->dieUsage( |
||
57 | $msg['info'], |
||
58 | $msg['code'], |
||
59 | 0, |
||
60 | [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ] |
||
61 | ); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | $target = User::newFromName( $params['user'] ); |
||
66 | // Bug 38633 - if the target is a user (not an IP address), but it |
||
67 | // doesn't exist or is unusable, error. |
||
68 | if ( $target instanceof User && |
||
69 | ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) ) |
||
70 | ) { |
||
71 | $this->dieUsageMsg( [ 'nosuchuser', $params['user'] ] ); |
||
72 | } |
||
73 | |||
74 | if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) { |
||
75 | $this->dieUsageMsg( 'canthide' ); |
||
76 | } |
||
77 | if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) { |
||
78 | $this->dieUsageMsg( 'cantblock-email' ); |
||
79 | } |
||
80 | |||
81 | $data = [ |
||
82 | 'PreviousTarget' => $params['user'], |
||
83 | 'Target' => $params['user'], |
||
84 | 'Reason' => [ |
||
85 | $params['reason'], |
||
86 | 'other', |
||
87 | $params['reason'] |
||
88 | ], |
||
89 | 'Expiry' => $params['expiry'], |
||
90 | 'HardBlock' => !$params['anononly'], |
||
91 | 'CreateAccount' => $params['nocreate'], |
||
92 | 'AutoBlock' => $params['autoblock'], |
||
93 | 'DisableEmail' => $params['noemail'], |
||
94 | 'HideUser' => $params['hidename'], |
||
95 | 'DisableUTEdit' => !$params['allowusertalk'], |
||
96 | 'Reblock' => $params['reblock'], |
||
97 | 'Watch' => $params['watchuser'], |
||
98 | 'Confirm' => true, |
||
99 | ]; |
||
100 | |||
101 | $retval = SpecialBlock::processForm( $data, $this->getContext() ); |
||
102 | if ( $retval !== true ) { |
||
103 | // We don't care about multiple errors, just report one of them |
||
104 | $this->dieUsageMsg( $retval ); |
||
105 | } |
||
106 | |||
107 | list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] ); |
||
108 | $res['user'] = $params['user']; |
||
109 | $res['userID'] = $target instanceof User ? $target->getId() : 0; |
||
110 | |||
111 | $block = Block::newFromTarget( $target, null, true ); |
||
112 | if ( $block instanceof Block ) { |
||
113 | $res['expiry'] = $wgContLang->formatExpiry( $block->mExpiry, TS_ISO_8601, 'infinite' ); |
||
114 | $res['id'] = $block->getId(); |
||
115 | } else { |
||
116 | # should be unreachable |
||
117 | $res['expiry'] = ''; |
||
118 | $res['id'] = ''; |
||
119 | } |
||
120 | |||
121 | $res['reason'] = $params['reason']; |
||
122 | $res['anononly'] = $params['anononly']; |
||
123 | $res['nocreate'] = $params['nocreate']; |
||
124 | $res['autoblock'] = $params['autoblock']; |
||
125 | $res['noemail'] = $params['noemail']; |
||
126 | $res['hidename'] = $params['hidename']; |
||
127 | $res['allowusertalk'] = $params['allowusertalk']; |
||
128 | $res['watchuser'] = $params['watchuser']; |
||
129 | |||
130 | $this->getResult()->addValue( null, $this->getModuleName(), $res ); |
||
131 | } |
||
132 | |||
179 |
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.