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 |
||
13 | abstract class Controller extends BaseController |
||
14 | { |
||
15 | use QueryHelperTrait, ErrorResponsesTrait; |
||
16 | |||
17 | protected $statusCode = 200; |
||
18 | protected $fractal; |
||
19 | |||
20 | public function __construct() |
||
28 | |||
29 | /** |
||
30 | * Sets the fractal transformer |
||
31 | * |
||
32 | * @return mixed |
||
33 | */ |
||
34 | public function setTransformer($transformer) { |
||
38 | |||
39 | /** |
||
40 | * Sets model builder |
||
41 | * |
||
42 | * @return mixed |
||
43 | */ |
||
44 | public function setModel($model) { |
||
48 | |||
49 | /** |
||
50 | * Calls authorization methods on the default traits |
||
51 | * |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function shouldAuthorize() { |
||
58 | |||
59 | /** |
||
60 | * Sets resource key for fractal |
||
61 | * |
||
62 | * @return mixed |
||
63 | */ |
||
64 | public function setResourceKey($resourceKey) { |
||
68 | |||
69 | /** |
||
70 | * Parses includes from either the header or query string. |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | protected function parseIncludes() |
||
86 | |||
87 | /** |
||
88 | * Returns the current status code. |
||
89 | * |
||
90 | * @return int |
||
91 | */ |
||
92 | protected function getStatusCode() |
||
96 | |||
97 | /** |
||
98 | * Sets the current status code. |
||
99 | * |
||
100 | * @param $statusCode |
||
101 | * @return $this |
||
102 | */ |
||
103 | protected function setStatusCode($statusCode) |
||
109 | |||
110 | private function prepareBuilder($builder) |
||
118 | |||
119 | /** |
||
120 | * Returns a json response that contains the specified resource |
||
121 | * passed through fractal and optionally a transformer. |
||
122 | * |
||
123 | * @param $item |
||
124 | * @param null $callback |
||
125 | * @return \Illuminate\Http\JsonResponse |
||
126 | */ |
||
127 | View Code Duplication | protected function respondWithItem($item, $callback = null) |
|
138 | |||
139 | /** |
||
140 | * Returns a json response that indicates the resource was successfully created also |
||
141 | * returns the resource passed through fractal and optionally a transformer. |
||
142 | * |
||
143 | * @param $item |
||
144 | * @param null $callback |
||
145 | * @return \Illuminate\Http\JsonResponse |
||
146 | */ |
||
147 | View Code Duplication | protected function respondWithItemCreated($item, $callback = null) |
|
159 | |||
160 | /** |
||
161 | * Returns a json response that contains the specified collection |
||
162 | * passed through fractal and optionally a transformer. |
||
163 | * |
||
164 | * @param $collection |
||
165 | * @param $callback |
||
166 | * @return \Illuminate\Http\JsonResponse |
||
167 | */ |
||
168 | protected function respondWithCollection($collection) |
||
174 | |||
175 | /** |
||
176 | * Returns a json response that contains the specified paginated collection |
||
177 | * passed through fractal and optionally a transformer. |
||
178 | * |
||
179 | * @param $builder |
||
180 | * @param $callback |
||
181 | * @param int $perPage |
||
182 | * @return \Illuminate\Http\JsonResponse |
||
183 | */ |
||
184 | protected function respondWithPaginatedCollection($builder = null, $perPage = 10) |
||
197 | |||
198 | /** |
||
199 | * Returns an array of Query Parameters, not including pagination. |
||
200 | * |
||
201 | * @return array |
||
202 | */ |
||
203 | protected function getQueryParameters() |
||
207 | |||
208 | /** |
||
209 | * Returns a json response that contains the specified array, |
||
210 | * the current status code and optional headers. |
||
211 | * |
||
212 | * @param array $array |
||
213 | * @param array $headers |
||
214 | * @return \Illuminate\Http\JsonResponse |
||
215 | */ |
||
216 | protected function respondWithArray(array $array, array $headers = []) |
||
220 | |||
221 | /** |
||
222 | * Returns a response that indicates success but no content returned. |
||
223 | * |
||
224 | * @return \Illuminate\Http\Response |
||
225 | */ |
||
226 | protected function respondWithNoContent() |
||
230 | } |
||
231 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: