| Conditions | 11 |
| Paths | 23 |
| Total Lines | 77 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 |
||
| 73 | public function execute() |
||
| 74 | { |
||
| 75 | // set response header |
||
| 76 | header('Content-Type: text/plain; charset=utf-8'); |
||
| 77 | |||
| 78 | // check allowed ips |
||
| 79 | if (!$this->checkAllowedIps()) { |
||
| 80 | return serialize($this->signer->encode([ |
||
| 81 | 'response' => [ |
||
| 82 | 'error' => 'IP not in allowed list: '.$_SERVER['REMOTE_ADDR'], |
||
| 83 | ] |
||
| 84 | ])); |
||
| 85 | } |
||
| 86 | |||
| 87 | // verify request token |
||
| 88 | if (!$this->verifyRequestToken()) { |
||
| 89 | return serialize($this->signer->encode([ |
||
| 90 | 'response' => [ |
||
| 91 | 'error' => 'invalid request token', |
||
| 92 | ] |
||
| 93 | ])); |
||
| 94 | } |
||
| 95 | |||
| 96 | // decode post payload |
||
| 97 | $data = $this->signer->decode( |
||
| 98 | $this->post |
||
| 99 | ); |
||
| 100 | |||
| 101 | // check client post is an array |
||
| 102 | if (!is_array($data)) { |
||
| 103 | return serialize($data); |
||
| 104 | } |
||
| 105 | |||
| 106 | // check data params array or set |
||
| 107 | if (!isset($data['params'])) { |
||
| 108 | $data['params'] = []; |
||
| 109 | } |
||
| 110 | |||
| 111 | // check data config array, set into scope |
||
| 112 | if (!empty($data['config'])) { |
||
| 113 | $this->config = $data['config']; |
||
| 114 | } |
||
| 115 | |||
| 116 | // check for empty |
||
| 117 | if (empty($data['component']) || empty($data['action'])) { |
||
| 118 | $error = empty($data['component']) ? 'component class' : 'action'; |
||
| 119 | return serialize($this->signer->encode([ |
||
| 120 | 'response' => [ |
||
| 121 | 'error' => $error.' cannot be empty', |
||
| 122 | ] |
||
| 123 | ])); |
||
| 124 | } |
||
| 125 | |||
| 126 | $class = '\\Plinker\\'.$data['component']; |
||
| 127 | |||
| 128 | if (class_exists($class)) { |
||
| 129 | $componentClass = new $class($this->config + $data + $this->post); |
||
| 130 | |||
| 131 | if (method_exists($componentClass, $data['action'])) { |
||
| 132 | $return = call_user_func( |
||
| 133 | [ |
||
| 134 | $componentClass, |
||
| 135 | $data['action'] |
||
| 136 | ], |
||
| 137 | $data['params'] |
||
| 138 | ); |
||
| 139 | } else { |
||
| 140 | $return = 'action not implemented'; |
||
| 141 | } |
||
| 142 | } else { |
||
| 143 | $return = 'not implemented'; |
||
| 144 | } |
||
| 145 | |||
| 146 | return serialize($this->signer->encode([ |
||
| 147 | 'response' => $return, |
||
| 148 | ])); |
||
| 149 | } |
||
| 150 | } |
||
| 151 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.