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 |
||
28 | class SuccessResponseBuilder extends ResponseBuilder |
||
29 | { |
||
30 | /** |
||
31 | * The manager responsible for transforming and serializing data. |
||
32 | * |
||
33 | * @var \Flugg\Responder\Contracts\Manager |
||
34 | */ |
||
35 | protected $manager; |
||
36 | |||
37 | /** |
||
38 | * The meta data appended to the serialized data. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $meta = []; |
||
43 | |||
44 | /** |
||
45 | * The Fractal resource instance containing the data and transformer. |
||
46 | * |
||
47 | * @var \League\Fractal\Resource\ResourceInterface |
||
48 | */ |
||
49 | protected $resource; |
||
50 | |||
51 | /** |
||
52 | * The resource factory used to generate resource instances. |
||
53 | * |
||
54 | * @var \Flugg\Responder\ResourceFactory |
||
55 | */ |
||
56 | protected $resourceFactory; |
||
57 | |||
58 | /** |
||
59 | * The HTTP status code for the response. |
||
60 | * |
||
61 | * @var int |
||
62 | */ |
||
63 | protected $statusCode = 200; |
||
64 | |||
65 | /** |
||
66 | * SuccessResponseBuilder constructor. |
||
67 | * |
||
68 | * @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory |
||
69 | * @param \Flugg\Responder\ResourceFactory $resourceFactory |
||
70 | * @param \League\Fractal\Manager $manager |
||
71 | */ |
||
72 | public function __construct(ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager) |
||
80 | |||
81 | /** |
||
82 | * Add data to the meta data appended to the response data. |
||
83 | * |
||
84 | * @param array $data |
||
85 | * @return self |
||
86 | */ |
||
87 | public function addMeta(array $data):SuccessResponseBuilder |
||
93 | |||
94 | /** |
||
95 | * Set the serializer used to serialize the resource data. |
||
96 | * |
||
97 | * @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
||
98 | * @return self |
||
99 | */ |
||
100 | public function serializer($serializer):SuccessResponseBuilder |
||
106 | |||
107 | /** |
||
108 | * Set the HTTP status code for the response. |
||
109 | * |
||
110 | * @param int $statusCode |
||
111 | * @return self |
||
112 | * @throws \InvalidArgumentException |
||
113 | */ |
||
114 | View Code Duplication | public function setStatus(int $statusCode):ResponseBuilder |
|
122 | |||
123 | /** |
||
124 | * Set the transformation data. This will set a new resource instance on the response |
||
125 | * builder depending on what type of data is provided. |
||
126 | * |
||
127 | * @param mixed|null $data |
||
128 | * @param callable|string|null $transformer |
||
129 | * @param string|null $resourceKey |
||
130 | * @return self |
||
131 | */ |
||
132 | public function transform($data = null, $transformer = null, string $resourceKey = null):SuccessResponseBuilder |
||
146 | |||
147 | /** |
||
148 | * Convert the response to an array. |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function toArray():array |
||
156 | |||
157 | /** |
||
158 | * Get the Fractal resource instance. |
||
159 | * |
||
160 | * @return \League\Fractal\Resource\ResourceInterface |
||
161 | */ |
||
162 | public function getResource():ResourceInterface |
||
166 | |||
167 | /** |
||
168 | * Get the Fractal manager responsible for transforming and serializing the data. |
||
169 | * |
||
170 | * @return \League\Fractal\Manager |
||
171 | */ |
||
172 | public function getManager():Manager |
||
176 | |||
177 | /** |
||
178 | * Resolve a serializer instance from the value. |
||
179 | * |
||
180 | * @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
||
181 | * @return \League\Fractal\Serializer\SerializerAbstract |
||
182 | * @throws \Flugg\Responder\Exceptions\InvalidSerializerException |
||
183 | */ |
||
184 | protected function resolveSerializer($serializer):SerializerAbstract |
||
196 | |||
197 | /** |
||
198 | * Resolve a model instance from the data. |
||
199 | * |
||
200 | * @param \Illuminate\Database\Eloquent\Model|array $data |
||
201 | * @return \Illuminate\Database\Eloquent\Model |
||
202 | * @throws \InvalidArgumentException |
||
203 | */ |
||
204 | protected function resolveModel($data):Model |
||
217 | |||
218 | /** |
||
219 | * Resolve a transformer. |
||
220 | * |
||
221 | * @param \Illuminate\Database\ELoquent\Model $model |
||
222 | * @param \Flugg\Responder\Transformer|callable|null $transformer |
||
223 | * @return \Flugg\Responder\Transformer|callable |
||
224 | * @throws \InvalidTransformerException |
||
225 | */ |
||
226 | protected function resolveTransformer(Model $model, $transformer = null) |
||
227 | { |
||
228 | if (is_null($transformer)) { |
||
229 | $transformer = $this->resolveTransformerFromModel($model); |
||
230 | } |
||
231 | |||
232 | if (is_string($transformer)) { |
||
233 | $transformer = new $transformer; |
||
234 | } |
||
235 | |||
236 | if ($transformer instanceof Transformer) { |
||
237 | $this->setRelations($transformer, $model); |
||
238 | } elseif (! is_callable($transformer)) { |
||
239 | throw new InvalidTransformerException($model); |
||
240 | } |
||
241 | |||
242 | return $transformer; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Resolve a transformer from the model. If the model is not transformable, a closure |
||
247 | * based transformer will be created instead, from the model's fillable attributes. |
||
248 | * |
||
249 | * @param \Illuminate\Database\ELoquent\Model $model |
||
250 | * @return \Flugg\Responder\Transformer|callable |
||
251 | */ |
||
252 | protected function resolveTransformerFromModel(Model $model) |
||
253 | { |
||
254 | if (! $model instanceof Transformable) { |
||
255 | return function () use ($model) { |
||
256 | return $model->toArray(); |
||
257 | }; |
||
258 | } |
||
259 | |||
260 | return $model::ransformer(); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Set relations on the transformer and parse them using the manager. |
||
265 | * |
||
266 | * @param \Flugg\Responder\Transformer $transformer |
||
267 | * @param \Illuminate\Database\Eloquent\Model $model |
||
268 | * @return void |
||
269 | */ |
||
270 | protected function setRelations(Transformer $transformer, Model $model) |
||
276 | |||
277 | /** |
||
278 | * Resolve eager loaded relations from the model. |
||
279 | * |
||
280 | * @param \Illuminate\Database\Eloquent\Model $model |
||
281 | * @return array |
||
282 | */ |
||
283 | protected function resolveRelations(Model $model):array |
||
287 | |||
288 | /** |
||
289 | * Resolve the resource key from the model. |
||
290 | * |
||
291 | * @param \Illuminate\Database\Eloquent\Model $model |
||
292 | * @param string|null $resourceKey |
||
293 | * @return string |
||
294 | */ |
||
295 | protected function resolveResourceKey(Model $model, string $resourceKey = null):string |
||
307 | |||
308 | /** |
||
309 | * Serialize the transformation data. |
||
310 | * |
||
311 | * @param ResourceInterface $resource |
||
312 | * @return array |
||
313 | */ |
||
314 | protected function serialize(ResourceInterface $resource):array |
||
318 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..