| Conditions | 10 |
| Paths | 18 |
| Total Lines | 60 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 102 | protected function response($code, $metadata = null, $message = null, $provider) |
||
| 103 | { |
||
| 104 | switch ($code) { |
||
| 105 | // Error Codes |
||
| 106 | case 400: |
||
| 107 | $envelope = 'error'; |
||
| 108 | $description = 'Bad Request'; |
||
| 109 | break; |
||
| 110 | case 401: |
||
| 111 | $envelope = 'error'; |
||
| 112 | $description = 'Unauthorized'; |
||
| 113 | break; |
||
| 114 | case 403: |
||
| 115 | $envelope = 'error'; |
||
| 116 | $description = 'Forbidden'; |
||
| 117 | break; |
||
| 118 | case 404: |
||
| 119 | $envelope = 'error'; |
||
| 120 | $description = 'Not Found'; |
||
| 121 | break; |
||
| 122 | case 500: |
||
| 123 | $envelope = 'error'; |
||
| 124 | $description = 'Something went wrong'; |
||
| 125 | break; |
||
| 126 | // Success Codes |
||
| 127 | case 200: |
||
| 128 | $envelope = 'data'; |
||
| 129 | $description = 'OK'; |
||
| 130 | break; |
||
| 131 | case 201: |
||
| 132 | $envelope = 'data'; |
||
| 133 | $description = 'Created'; |
||
| 134 | break; |
||
| 135 | case 202: |
||
| 136 | $envelope = 'data'; |
||
| 137 | $description = 'Accepted'; |
||
| 138 | break; |
||
| 139 | default: |
||
| 140 | $envelope = 'data'; |
||
| 141 | $description = ''; |
||
| 142 | break; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (is_null($message)) { |
||
| 146 | $message = $description; |
||
| 147 | } |
||
| 148 | // HTTP Response |
||
| 149 | $response = [ |
||
| 150 | $envelope => [ |
||
| 151 | 'code' => $code, |
||
| 152 | 'message' => $message, |
||
| 153 | 'provider' => $provider, |
||
| 154 | 'metadata' => $metadata |
||
| 155 | ], |
||
| 156 | ]; |
||
| 157 | |||
| 158 | $response = json_encode($response, JSON_PRETTY_PRINT); |
||
| 159 | |||
| 160 | return $response; |
||
| 161 | } |
||
| 162 | |||
| 191 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: