| Conditions | 11 |
| Paths | 23 |
| Total Lines | 77 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 82 | public function execute() |
||
| 83 | { |
||
| 84 | // set response header |
||
| 85 | header('Content-Type: text/plain; charset=utf-8'); |
||
| 86 | |||
| 87 | // check |
||
| 88 | if (!$this->checkAllowedIp()) { |
||
| 89 | return serialize($this->signer->encode([ |
||
| 90 | 'response' => [ |
||
| 91 | 'error' => 'IP not in allowed list: '.$_SERVER['REMOTE_ADDR'], |
||
| 92 | ], |
||
| 93 | ])); |
||
| 94 | } |
||
| 95 | |||
| 96 | // verify request token |
||
| 97 | if (!$this->verifyRequestToken()) { |
||
| 98 | return serialize($this->signer->encode([ |
||
| 99 | 'response' => [ |
||
| 100 | 'error' => 'invalid request token', |
||
| 101 | ], |
||
| 102 | ])); |
||
| 103 | } |
||
| 104 | |||
| 105 | // decode post payload |
||
| 106 | $data = $this->signer->decode( |
||
| 107 | $this->post |
||
| 108 | ); |
||
| 109 | |||
| 110 | // check client post is an array |
||
| 111 | if (!is_array($data)) { |
||
| 112 | return serialize($data); |
||
| 113 | } |
||
| 114 | |||
| 115 | // check data params array or set |
||
| 116 | if (!isset($data['params'])) { |
||
| 117 | $data['params'] = []; |
||
| 118 | } |
||
| 119 | |||
| 120 | // check data config array, set into scope |
||
| 121 | if (!empty($data['config'])) { |
||
| 122 | $this->config = $data['config']; |
||
| 123 | } |
||
| 124 | |||
| 125 | // check for empty |
||
| 126 | if (empty($data['component']) || empty($data['action'])) { |
||
| 127 | $error = empty($data['component']) ? 'component class' : 'action'; |
||
| 128 | return serialize($this->signer->encode([ |
||
| 129 | 'response' => [ |
||
| 130 | 'error' => $error.' cannot be empty', |
||
| 131 | ], |
||
| 132 | ])); |
||
| 133 | } |
||
| 134 | |||
| 135 | $class = '\\Plinker\\'.$data['component']; |
||
| 136 | |||
| 137 | if (class_exists($class)) { |
||
| 138 | $componentClass = new $class($this->config + $data + $this->post); |
||
| 139 | |||
| 140 | if (method_exists($componentClass, $data['action'])) { |
||
| 141 | $return = call_user_func( |
||
| 142 | [ |
||
| 143 | $componentClass, |
||
| 144 | $data['action'], |
||
| 145 | ], |
||
| 146 | $data['params'] |
||
| 147 | ); |
||
| 148 | } else { |
||
| 149 | $return = 'action not implemented'; |
||
| 150 | } |
||
| 151 | } else { |
||
| 152 | $return = 'not implemented'; |
||
| 153 | } |
||
| 154 | |||
| 155 | return serialize($this->signer->encode([ |
||
| 156 | 'response' => $return, |
||
| 157 | ])); |
||
| 158 | } |
||
| 159 | } |
||
| 160 |
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.