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 | * Sets resource key for fractal |
||
51 | * |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function setResourceKey($resourceKey) { |
||
58 | |||
59 | /** |
||
60 | * Parses includes from either the header or query string. |
||
61 | * |
||
62 | * @return mixed |
||
63 | */ |
||
64 | protected function parseIncludes() |
||
76 | |||
77 | /** |
||
78 | * Returns the current status code. |
||
79 | * |
||
80 | * @return int |
||
81 | */ |
||
82 | protected function getStatusCode() |
||
86 | |||
87 | /** |
||
88 | * Sets the current status code. |
||
89 | * |
||
90 | * @param $statusCode |
||
91 | * @return $this |
||
92 | */ |
||
93 | protected function setStatusCode($statusCode) |
||
99 | |||
100 | private function prepareBuilder($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 | * @param $callback |
||
156 | * @return \Illuminate\Http\JsonResponse |
||
157 | */ |
||
158 | protected function respondWithCollection($collection) |
||
164 | |||
165 | /** |
||
166 | * Returns a json response that contains the specified paginated collection |
||
167 | * passed through fractal and optionally a transformer. |
||
168 | * |
||
169 | * @param $builder |
||
170 | * @param $callback |
||
171 | * @param int $perPage |
||
172 | * @return \Illuminate\Http\JsonResponse |
||
173 | */ |
||
174 | protected function respondWithPaginatedCollection($builder = null, $perPage = 10) |
||
187 | |||
188 | /** |
||
189 | * Returns an array of Query Parameters, not including pagination. |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | protected function getQueryParameters() |
||
197 | |||
198 | /** |
||
199 | * Returns a json response that contains the specified array, |
||
200 | * the current status code and optional headers. |
||
201 | * |
||
202 | * @param array $array |
||
203 | * @param array $headers |
||
204 | * @return \Illuminate\Http\JsonResponse |
||
205 | */ |
||
206 | protected function respondWithArray(array $array, array $headers = []) |
||
210 | |||
211 | /** |
||
212 | * Returns a response that indicates success but no content returned. |
||
213 | * |
||
214 | * @return \Illuminate\Http\Response |
||
215 | */ |
||
216 | protected function respondWithNoContent() |
||
220 | } |
||
221 |
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: