| Conditions | 9 |
| Paths | 11 |
| Total Lines | 51 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 114 | public function handlePostData(&$params) { |
||
| 115 | global $INFO; |
||
| 116 | global $lang; |
||
| 117 | global $INPUT; |
||
| 118 | |||
| 119 | // Get and validate parameters. |
||
| 120 | if(!isset($params['target'])) { |
||
| 121 | throw new \Exception('no subscription target given'); |
||
| 122 | } |
||
| 123 | $target = $params['target']; |
||
| 124 | $valid_styles = array('every', 'digest'); |
||
| 125 | if(substr($target, -1, 1) === ':') { |
||
| 126 | // Allow “list” subscribe style since the target is a namespace. |
||
| 127 | $valid_styles[] = 'list'; |
||
| 128 | } |
||
| 129 | $style = valid_input_set( |
||
| 130 | 'style', $valid_styles, $params, |
||
| 131 | 'invalid subscription style given' |
||
| 132 | ); |
||
| 133 | $action = valid_input_set( |
||
| 134 | 'action', array('subscribe', 'unsubscribe'), |
||
| 135 | $params, 'invalid subscription action given' |
||
| 136 | ); |
||
| 137 | |||
| 138 | // Check other conditions. |
||
| 139 | if($action === 'subscribe') { |
||
| 140 | if($INFO['userinfo']['mail'] === '') { |
||
| 141 | throw new \Exception($lang['subscr_subscribe_noaddress']); |
||
| 142 | } |
||
| 143 | } elseif($action === 'unsubscribe') { |
||
| 144 | $is = false; |
||
| 145 | foreach($INFO['subscribed'] as $subscr) { |
||
| 146 | if($subscr['target'] === $target) { |
||
| 147 | $is = true; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | if($is === false) { |
||
| 151 | throw new \Exception( |
||
| 152 | sprintf( |
||
| 153 | $lang['subscr_not_subscribed'], |
||
| 154 | $INPUT->server->str('REMOTE_USER'), |
||
| 155 | prettyprint_id($target) |
||
| 156 | ) |
||
| 157 | ); |
||
| 158 | } |
||
| 159 | // subscription_set deletes a subscription if style = null. |
||
| 160 | $style = null; |
||
| 161 | } |
||
| 162 | |||
| 163 | $params = compact('target', 'style', 'action'); |
||
| 164 | } |
||
| 165 | |||
| 167 |