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 | */ |
||
225 | protected function resolveTransformer(Model $model, $transformer = null) |
||
235 | |||
236 | /** |
||
237 | * Resolve a transformer from the model. If the model is not transformable, a closure |
||
238 | * based transformer will be created instead, from the model's fillable attributes. |
||
239 | * |
||
240 | * @param \Illuminate\Database\ELoquent\Model $model |
||
241 | * @return \Flugg\Responder\Transformer|callable |
||
242 | */ |
||
243 | protected function resolveTransformerFromModel(Model $model) |
||
253 | |||
254 | /** |
||
255 | * Parse a transformer class and set relations. |
||
256 | * |
||
257 | * @param \Flugg\Responder\Transformer|callable $transformer |
||
258 | * @return \Flugg\Responder\Transformer|callable |
||
259 | * @throws \InvalidTransformerException |
||
260 | */ |
||
261 | protected function parseTransformer($transformer) |
||
274 | |||
275 | /** |
||
276 | * Resolve eager loaded relations from the model. |
||
277 | * |
||
278 | * @param \Illuminate\Database\Eloquent\Model $model |
||
279 | * @return array |
||
280 | */ |
||
281 | protected function resolveRelations(Model $model):array |
||
285 | |||
286 | /** |
||
287 | * Resolve the resource key from the model. |
||
288 | * |
||
289 | * @param \Illuminate\Database\Eloquent\Model $model |
||
290 | * @param string|null $resourceKey |
||
291 | * @return string |
||
292 | */ |
||
293 | protected function resolveResourceKey(Model $model, string $resourceKey = null):string |
||
305 | |||
306 | /** |
||
307 | * Serialize the transformation data. |
||
308 | * |
||
309 | * @param ResourceInterface $resource |
||
310 | * @return array |
||
311 | */ |
||
312 | protected function serialize(ResourceInterface $resource):array |
||
316 | } |
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..