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 |
||
11 | abstract class Controller extends BaseController |
||
12 | { |
||
13 | use QueryHelperTrait, ErrorResponsesTrait; |
||
14 | |||
15 | protected $statusCode = 200; |
||
16 | protected $resourceKey = null; |
||
17 | protected $fractal, $transformer; |
||
|
|||
18 | |||
19 | public function __construct() |
||
25 | |||
26 | /** |
||
27 | * Sets the fractal transformer |
||
28 | * |
||
29 | * @param $transformer |
||
30 | * @return mixed |
||
31 | */ |
||
32 | public function setTransformer($transformer) |
||
38 | |||
39 | /** |
||
40 | * Sets resource key for fractal |
||
41 | * |
||
42 | * @param $resourceKey |
||
43 | * @return mixed |
||
44 | */ |
||
45 | public function setResourceKey($resourceKey) |
||
51 | |||
52 | /** |
||
53 | * Parses includes from either the header or query string. |
||
54 | * |
||
55 | * @return mixed |
||
56 | */ |
||
57 | protected function parseIncludes() |
||
69 | |||
70 | /** |
||
71 | * Returns the current status code. |
||
72 | * |
||
73 | * @return int |
||
74 | */ |
||
75 | protected function getStatusCode() |
||
79 | |||
80 | /** |
||
81 | * Sets the current status code. |
||
82 | * |
||
83 | * @param $statusCode |
||
84 | * @return $this |
||
85 | */ |
||
86 | protected function setStatusCode($statusCode) |
||
92 | |||
93 | /** |
||
94 | * Eager load any available includes and apply query parameters. |
||
95 | * |
||
96 | * @param $builder |
||
97 | * @return mixed |
||
98 | */ |
||
99 | protected function withIncludes($builder) |
||
107 | |||
108 | /** |
||
109 | * Returns a json response that contains the specified resource |
||
110 | * passed through fractal and optionally a transformer. |
||
111 | * |
||
112 | * @param $item |
||
113 | * @param null $callback |
||
114 | * @return \Illuminate\Http\JsonResponse |
||
115 | */ |
||
116 | View Code Duplication | protected function respondWithItem($item, $callback = null) |
|
127 | |||
128 | /** |
||
129 | * Returns a json response that indicates the resource was successfully created also |
||
130 | * returns the resource passed through fractal and optionally a transformer. |
||
131 | * |
||
132 | * @param $item |
||
133 | * @param null $callback |
||
134 | * @return \Illuminate\Http\JsonResponse |
||
135 | */ |
||
136 | View Code Duplication | protected function respondWithItemCreated($item, $callback = null) |
|
148 | |||
149 | /** |
||
150 | * Returns a json response that contains the specified collection |
||
151 | * passed through fractal and optionally a transformer. |
||
152 | * |
||
153 | * @param $collection |
||
154 | * @return \Illuminate\Http\JsonResponse |
||
155 | */ |
||
156 | protected function respondWithCollection($collection) |
||
162 | |||
163 | /** |
||
164 | * Returns a json response that contains the specified paginated collection |
||
165 | * passed through fractal and optionally a transformer. |
||
166 | * |
||
167 | * @param $builder |
||
168 | * @param int $perPage |
||
169 | * @return \Illuminate\Http\JsonResponse |
||
170 | */ |
||
171 | protected function respondWithPaginatedCollection($builder, $perPage = 10) |
||
184 | |||
185 | /** |
||
186 | * Returns an array of Query Parameters, not including pagination. |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | protected function getQueryParameters() |
||
194 | |||
195 | /** |
||
196 | * Returns a json response that contains the specified array, |
||
197 | * the current status code and optional headers. |
||
198 | * |
||
199 | * @param array $array |
||
200 | * @param array $headers |
||
201 | * @return \Illuminate\Http\JsonResponse |
||
202 | */ |
||
203 | protected function respondWithArray(array $array, array $headers = []) |
||
207 | |||
208 | /** |
||
209 | * Returns a response that indicates success but no content returned. |
||
210 | * |
||
211 | * @return \Illuminate\Http\Response |
||
212 | */ |
||
213 | protected function respondWithNoContent() |
||
217 | } |
||
218 |
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.