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 |
||
17 | abstract class AbstractAdapter implements AdapterInterface |
||
18 | { |
||
19 | /** |
||
20 | * The Serializer |
||
21 | * |
||
22 | * @var SerializerInterface |
||
23 | */ |
||
24 | protected $serializer; |
||
25 | |||
26 | /** |
||
27 | * The Normalizer |
||
28 | * |
||
29 | * @var NormalizerInterface |
||
30 | */ |
||
31 | protected $normalizer; |
||
32 | |||
33 | /** |
||
34 | * The Store to use for persistence operations. |
||
35 | * |
||
36 | * @var Store |
||
37 | */ |
||
38 | protected $store; |
||
39 | |||
40 | /** |
||
41 | * The REST configuration. |
||
42 | * |
||
43 | * @var Rest\RestConfiguration |
||
44 | */ |
||
45 | protected $config; |
||
46 | |||
47 | /** |
||
48 | * Constructor. |
||
49 | * |
||
50 | * @param SerializerInterface $serializer |
||
51 | * @param NormalizerInterface $normalizer |
||
52 | * @param StoreInterface $store |
||
53 | * @param Rest\RestConfiguration $config |
||
54 | */ |
||
55 | public function __construct(SerializerInterface $serializer, NormalizerInterface $normalizer, Store $store, Rest\RestConfiguration $config) |
||
62 | |||
63 | /** |
||
64 | * {@inheritDoc} |
||
65 | */ |
||
66 | abstract public function processRequest(Rest\RestRequest $request); |
||
67 | |||
68 | /** |
||
69 | * {@inheritDoc} |
||
70 | */ |
||
71 | public function findRecord($typeKey, $identifier) //, array $fields = [], array $inclusions = []) |
||
77 | |||
78 | /** |
||
79 | * {@inheritDoc} |
||
80 | */ |
||
81 | public function findAll($typeKey, array $identifiers = []) //, array $pagination = [], array $fields = [], array $inclusions = [], array $sort = []) |
||
87 | |||
88 | /** |
||
89 | * {@inheritDoc} |
||
90 | */ |
||
91 | public function findQuery($typeKey, array $criteria, array $fields = [], array $sort = [], array $inclusions =[], $offset = 0, $limit = 0) |
||
97 | |||
98 | /** |
||
99 | * {@inheritDoc} |
||
100 | */ |
||
101 | public function findRelationship($typeKey, $identifier, $fieldKey) |
||
111 | |||
112 | /** |
||
113 | * {@inheritDoc} |
||
114 | */ |
||
115 | View Code Duplication | public function createRecord($typeKey, Rest\RestPayload $payload) //, array $fields = [], array $inclusions = []) |
|
127 | |||
128 | /** |
||
129 | * {@inheritDoc} |
||
130 | */ |
||
131 | View Code Duplication | public function updateRecord($typeKey, $identifier, Rest\RestPayload $payload) // , array $fields = [], array $inclusions = []) |
|
142 | |||
143 | /** |
||
144 | * {@inheritDoc} |
||
145 | */ |
||
146 | public function deleteRecord($typeKey, $identifier) |
||
151 | |||
152 | /** |
||
153 | * {@inheritDoc} |
||
154 | */ |
||
155 | public function autocomplete($typeKey, $attributeKey, $searchValue, array $pagination = []) |
||
161 | |||
162 | /** |
||
163 | * {@inheritDoc} |
||
164 | */ |
||
165 | abstract public function buildUrl(EntityMetadata $metadata, $identifier, $relFieldKey = null, $isRelatedLink = false); |
||
166 | |||
167 | /** |
||
168 | * Validates a normalized create record payload. |
||
169 | * |
||
170 | * @param string $typeKey |
||
171 | * @param array $normalized |
||
172 | * @throws AdapterException On invalid create record payload. |
||
173 | */ |
||
174 | abstract protected function validateCreatePayload($typeKey, array $normalized); |
||
175 | |||
176 | /** |
||
177 | * Validates a normalized update record payload. |
||
178 | * |
||
179 | * @param string $typeKey |
||
180 | * @param string $identifier |
||
181 | * @param array $normalized |
||
182 | * @throws AdapterException On invalid update record payload. |
||
183 | */ |
||
184 | abstract protected function validateUpdatePayload($typeKey, $identifier, array $normalized); |
||
185 | |||
186 | /** |
||
187 | * Creates a RestResponse object based on common response parameters shared by this adapter. |
||
188 | * |
||
189 | * @param int $status |
||
190 | * @param Rest\RestPayload $payload |
||
191 | * @return Rest\RestResponse |
||
192 | */ |
||
193 | abstract protected function createRestResponse($status, Rest\RestPayload $payload = null); |
||
194 | |||
195 | /** |
||
196 | * {@inheritDoc} |
||
197 | */ |
||
198 | public function handleException(\Exception $e) |
||
215 | |||
216 | /** |
||
217 | * {@inheritDoc} |
||
218 | */ |
||
219 | public function getStore() |
||
223 | |||
224 | /** |
||
225 | * {@inheritDoc} |
||
226 | */ |
||
227 | public function getSerializer() |
||
231 | |||
232 | /** |
||
233 | * {@inheritDoc} |
||
234 | */ |
||
235 | public function getNormalizer() |
||
239 | |||
240 | /** |
||
241 | * {@inheritDoc} |
||
242 | */ |
||
243 | public function normalize(Rest\RestPayload $payload) |
||
247 | |||
248 | /** |
||
249 | * {@inheritDoc} |
||
250 | */ |
||
251 | public function serialize(Model $model = null) |
||
257 | |||
258 | /** |
||
259 | * {@inheritDoc} |
||
260 | */ |
||
261 | public function serializeArray(array $models) |
||
267 | |||
268 | /** |
||
269 | * {@inheritDoc} |
||
270 | */ |
||
271 | public function serializeCollection(Collection $collection) |
||
277 | |||
278 | /** |
||
279 | * Validates that the serialized value is support by a RestPayload. |
||
280 | * |
||
281 | * @param mixed $serialized |
||
282 | * @return bool |
||
283 | * @throws AdapterException On invalid serialized value. |
||
284 | */ |
||
285 | protected function validateSerialization($serialized) |
||
292 | |||
293 | /** |
||
294 | * {@inheritDoc} |
||
295 | */ |
||
296 | public function getEntityMetadata($typeKey) |
||
300 | } |
||
301 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.