| Conditions | 7 |
| Paths | 15 |
| Total Lines | 54 |
| Code Lines | 34 |
| 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 |
||
| 45 | protected function handleSubscribeData() { |
||
| 46 | global $lang; |
||
| 47 | global $INFO; |
||
| 48 | global $ID; |
||
| 49 | global $INPUT; |
||
| 50 | |||
| 51 | // get and preprocess data. |
||
| 52 | $params = array(); |
||
| 53 | foreach(array('target', 'style', 'action') as $param) { |
||
| 54 | if($INPUT->has("sub_$param")) { |
||
| 55 | $params[$param] = $INPUT->str("sub_$param"); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | // any action given? if not just return and show the subscription page |
||
| 60 | if(empty($params['action']) || !checkSecurityToken()) return $this->actionname; |
||
| 61 | |||
| 62 | // Handle POST data, may throw exception. |
||
| 63 | trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post'); |
||
| 64 | |||
| 65 | $target = $params['target']; |
||
| 66 | $style = $params['style']; |
||
| 67 | $action = $params['action']; |
||
| 68 | |||
| 69 | // Perform action. |
||
| 70 | $sub = new \Subscription(); |
||
| 71 | if($action == 'unsubscribe') { |
||
| 72 | $ok = $sub->remove($target, $INPUT->server->str('REMOTE_USER'), $style); |
||
| 73 | } else { |
||
| 74 | $ok = $sub->add($target, $INPUT->server->str('REMOTE_USER'), $style); |
||
| 75 | } |
||
| 76 | |||
| 77 | if($ok) { |
||
| 78 | msg( |
||
| 79 | sprintf( |
||
| 80 | $lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']), |
||
| 81 | prettyprint_id($target) |
||
| 82 | ), 1 |
||
| 83 | ); |
||
| 84 | act_redirect($ID, $this->actionname); |
||
| 85 | } else { |
||
| 86 | throw new \Exception( |
||
| 87 | sprintf( |
||
| 88 | $lang["subscr_{$action}_error"], |
||
| 89 | hsc($INFO['userinfo']['name']), |
||
| 90 | prettyprint_id($target) |
||
| 91 | ) |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | |||
| 95 | // Assure that we have valid data if act_redirect somehow fails. should never be reached |
||
| 96 | $INFO['subscribed'] = $sub->user_subscription(); |
||
| 97 | return 'show'; |
||
| 98 | } |
||
| 99 | |||
| 101 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.