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 Controller extends BaseController |
||
| 13 | { |
||
| 14 | use QueryHelperTrait, ErrorResponsesTrait; |
||
| 15 | |||
| 16 | protected $statusCode = 200; |
||
| 17 | protected $resourceKey = null; |
||
| 18 | protected $fractal, $transformer; |
||
|
|
|||
| 19 | |||
| 20 | public function __construct() |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Sets the fractal transformer |
||
| 29 | * |
||
| 30 | * @param $transformer |
||
| 31 | * @return mixed |
||
| 32 | */ |
||
| 33 | public function setTransformer($transformer) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Sets resource key for fractal |
||
| 42 | * |
||
| 43 | * @param $resourceKey |
||
| 44 | * @return mixed |
||
| 45 | */ |
||
| 46 | public function setResourceKey($resourceKey) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Parses includes from either the header or query string. |
||
| 55 | * |
||
| 56 | * @return mixed |
||
| 57 | */ |
||
| 58 | protected function parseIncludes() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Returns the current status code. |
||
| 73 | * |
||
| 74 | * @return int |
||
| 75 | */ |
||
| 76 | protected function getStatusCode() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Sets the current status code. |
||
| 83 | * |
||
| 84 | * @param $statusCode |
||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | protected function setStatusCode($statusCode) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Eager load any available includes and apply query parameters. |
||
| 96 | * |
||
| 97 | * @param $builder |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | protected function withIncludes($builder) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns a json response that contains the specified resource |
||
| 111 | * passed through fractal and optionally a transformer. |
||
| 112 | * |
||
| 113 | * @param $item |
||
| 114 | * @param null $callback |
||
| 115 | * @return \Illuminate\Http\JsonResponse |
||
| 116 | */ |
||
| 117 | View Code Duplication | protected function respondWithItem($item, $callback = null) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Returns a json response that indicates the resource was successfully created also |
||
| 131 | * returns the resource passed through fractal and optionally a transformer. |
||
| 132 | * |
||
| 133 | * @param $item |
||
| 134 | * @param null $callback |
||
| 135 | * @return \Illuminate\Http\JsonResponse |
||
| 136 | */ |
||
| 137 | View Code Duplication | protected function respondWithItemCreated($item, $callback = null) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Returns a json response that contains the specified collection |
||
| 152 | * passed through fractal and optionally a transformer. |
||
| 153 | * |
||
| 154 | * @param $collection |
||
| 155 | * @return \Illuminate\Http\JsonResponse |
||
| 156 | */ |
||
| 157 | protected function respondWithCollection($collection) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns a json response that contains the specified paginated collection |
||
| 170 | * passed through fractal and optionally a transformer. |
||
| 171 | * |
||
| 172 | * @param $builder |
||
| 173 | * @param int $perPage |
||
| 174 | * @return \Illuminate\Http\JsonResponse |
||
| 175 | */ |
||
| 176 | protected function respondWithPaginatedCollection($builder, $perPage = 10) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Returns an array of Query Parameters, not including pagination. |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | protected function getQueryParameters() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns a json response that contains the specified array, |
||
| 202 | * the current status code and optional headers. |
||
| 203 | * |
||
| 204 | * @param array $array |
||
| 205 | * @param array $headers |
||
| 206 | * @return \Illuminate\Http\JsonResponse |
||
| 207 | */ |
||
| 208 | protected function respondWithArray(array $array, array $headers = []) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Returns a response that indicates success but no content returned. |
||
| 215 | * |
||
| 216 | * @return \Illuminate\Http\Response |
||
| 217 | */ |
||
| 218 | protected function respondWithNoContent() |
||
| 222 | } |
||
| 223 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.