Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 12 | abstract class AbstractEndpointController implements |
||
| 13 | DatabaseAwareInterface |
||
| 14 | { |
||
| 15 | use DatabaseAwareTrait; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Contains the repository used for interfacing with the database |
||
| 19 | * |
||
| 20 | * @var Mixed | Ps2alerts\Api\Repository\AbstractEndpointRepository |
||
| 21 | */ |
||
| 22 | protected $repository; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Stores the status code |
||
| 26 | * |
||
| 27 | * @var integer |
||
| 28 | */ |
||
| 29 | protected $statusCode = 200; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Holds Fractal |
||
| 33 | * |
||
| 34 | * @var \League\Fractal |
||
| 35 | */ |
||
| 36 | protected $fractal; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Holds the transformer we're going to use |
||
| 40 | * |
||
| 41 | * @var mixed|\Leage\Fractal\TransformerAbstract |
||
| 42 | */ |
||
| 43 | protected $transformer; |
||
| 44 | |||
| 45 | const CODE_WRONG_ARGS = 'API-MALFORMED-REQUEST'; |
||
| 46 | const CODE_NOT_FOUND = 'API-NOT-FOUND'; |
||
| 47 | const CODE_INTERNAL_ERROR = 'API-DOH'; |
||
| 48 | const CODE_UNAUTHORIZED = 'API-UNAUTHORIZED'; |
||
| 49 | const CODE_FORBIDDEN = 'API-DENIED'; |
||
| 50 | const CODE_EMPTY = 'API-EMPTY'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Getter for statusCode |
||
| 54 | * |
||
| 55 | * @return int |
||
| 56 | */ |
||
| 57 | public function getStatusCode() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Setter for statusCode |
||
| 64 | * |
||
| 65 | * @param int $statusCode Value to set |
||
| 66 | * |
||
| 67 | * @return self |
||
| 68 | */ |
||
| 69 | public function setStatusCode($statusCode) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Master function to split out appropiate calls |
||
| 77 | * |
||
| 78 | * @param string $kind The kind of data we wish to return |
||
| 79 | * @param array $data The data itself |
||
| 80 | * @param \League\Fractal\TransformerAbstract $callback The transformer class to call |
||
| 81 | * @param \Symfony\Component\HttpFoundation\Request $request The request itself |
||
| 82 | * @param \Symfony\Component\HttpFoundation\Response $response The response object to eventually call |
||
| 83 | * |
||
| 84 | * @return \Symfony\Component\HttpFoundation\Response Eventually |
||
| 85 | */ |
||
| 86 | protected function respond($kind, $data, $callback, Request $request, Response $response) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Builds an item response in Fractal then hands off to the responder |
||
| 105 | * |
||
| 106 | * @param array $item The item to transform |
||
| 107 | * @param mixed $callback The Transformer to pass through to Fractal |
||
| 108 | * @param \Symfony\Component\HttpFoundation\Response The client's response |
||
| 109 | * |
||
| 110 | * @return array The formatted array |
||
| 111 | */ |
||
| 112 | View Code Duplication | protected function respondWithItem($item, $callback, Response $response) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Builds a collection of items from Fractal then hands off to the responder |
||
| 122 | * |
||
| 123 | * @param array $collection The collection to transform |
||
| 124 | * @param mixed $callback The Transformer to pass through to Fractal |
||
| 125 | * @param \Symfony\Component\HttpFoundation\Response The client's response |
||
| 126 | * |
||
| 127 | * @return array The formatted array |
||
| 128 | */ |
||
| 129 | View Code Duplication | protected function respondWithCollection($collection, $callback, Response $response) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * The final step where the formatted array is now sent back as a response in JSON form |
||
| 139 | * |
||
| 140 | * @param \Symfony\Component\HttpFoundation\Response $response |
||
| 141 | * @param array $array The formatted array |
||
| 142 | * |
||
| 143 | * @return \Symfony\Component\HttpFoundation\Response The final response |
||
| 144 | */ |
||
| 145 | protected function respondWithArray(Response $response, array $array) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Responds gracefully with an error. |
||
| 157 | * |
||
| 158 | * @param \Symfony\Component\HttpFoundation\Response $response |
||
| 159 | * @param string $message Response message to put in the error |
||
| 160 | * @param int $errorCode Error code to set |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | protected function respondWithError(Response $response, $message, $errorCode) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Generates a response with a 404 HTTP error and a given message. |
||
| 185 | * |
||
| 186 | * @param \Symfony\Component\HttpFoundation\Response $response |
||
| 187 | * @param string $message |
||
| 188 | * |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | public function errorEmpty(Response $response, $message = 'No data / Empty') |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Generates a Response with a 403 HTTP header and a given message. |
||
| 199 | * |
||
| 200 | * @param \Symfony\Component\HttpFoundation\Response $response |
||
| 201 | * @param string $message |
||
| 202 | * |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function errorForbidden(Response $response, $message = 'Forbidden') |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Generates a Response with a 500 HTTP header and a given message. |
||
| 213 | * |
||
| 214 | * @param \Symfony\Component\HttpFoundation\Response $response |
||
| 215 | * @param string $message |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function errorInternalError(Response $response, $message = 'Internal Error') |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Generates a Response with a 404 HTTP header and a given message. |
||
| 227 | * |
||
| 228 | * @param \Symfony\Component\HttpFoundation\Response $response |
||
| 229 | * @param string $message |
||
| 230 | * |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | public function errorNotFound(Response $response, $message = 'Resource Not Found') |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Generates a Response with a 401 HTTP header and a given message. |
||
| 241 | * |
||
| 242 | * @param \Symfony\Component\HttpFoundation\Response $response |
||
| 243 | * @param string $message |
||
| 244 | * |
||
| 245 | * @return void |
||
| 246 | */ |
||
| 247 | public function errorUnauthorized(Response $response, $message = 'Unauthorized') |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Generates a Response with a 400 HTTP header and a given message. |
||
| 255 | * |
||
| 256 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 257 | */ |
||
| 258 | public function errorWrongArgs(Response $response, $message = 'Wrong Arguments') |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Reads any requested includes and adds them to the item / collection |
||
| 266 | * |
||
| 267 | * @param Symfony\Component\HttpFoundation\Request |
||
| 268 | * |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | public function getIncludesFromRequest(Request $request) |
||
| 279 | } |
||
| 280 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.