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 Scope |
||
29 | { |
||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $scopeIdentifier; |
||
34 | |||
35 | /** |
||
36 | * @var \League\Fractal\Manager |
||
37 | */ |
||
38 | protected $manager; |
||
39 | |||
40 | /** |
||
41 | * @var ResourceInterface |
||
42 | */ |
||
43 | protected $resource; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $parentScopes = []; |
||
49 | |||
50 | /** |
||
51 | * Create a new scope instance. |
||
52 | * |
||
53 | * @param Manager $manager |
||
54 | * @param ResourceInterface $resource |
||
55 | * @param string $scopeIdentifier |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | 52 | public function __construct(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null) |
|
65 | |||
66 | /** |
||
67 | * Embed a scope as a child of the current scope. |
||
68 | * |
||
69 | * @internal |
||
70 | * |
||
71 | * @param string $scopeIdentifier |
||
72 | * @param ResourceInterface $resource |
||
73 | * |
||
74 | * @return \League\Fractal\Scope |
||
75 | */ |
||
76 | 27 | public function embedChildScope($scopeIdentifier, $resource) |
|
80 | |||
81 | /** |
||
82 | * Get the current identifier. |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 27 | public function getScopeIdentifier() |
|
90 | |||
91 | /** |
||
92 | * Get the unique identifier for this scope. |
||
93 | * |
||
94 | * @param string $appendIdentifier |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 23 | public function getIdentifier($appendIdentifier = null) |
|
104 | |||
105 | /** |
||
106 | * Getter for parentScopes. |
||
107 | * |
||
108 | * @return mixed |
||
109 | */ |
||
110 | 28 | public function getParentScopes() |
|
114 | |||
115 | /** |
||
116 | * Getter for resource. |
||
117 | * |
||
118 | * @return ResourceInterface |
||
119 | */ |
||
120 | 1 | public function getResource() |
|
124 | |||
125 | /** |
||
126 | * Getter for manager. |
||
127 | * |
||
128 | * @return \League\Fractal\Manager |
||
129 | */ |
||
130 | 23 | public function getManager() |
|
134 | |||
135 | /** |
||
136 | * Is Requested. |
||
137 | * |
||
138 | * Check if - in relation to the current scope - this specific segment is allowed. |
||
139 | * That means, if a.b.c is requested and the current scope is a.b, then c is allowed. If the current |
||
140 | * scope is a then c is not allowed, even if it is there and potentially transformable. |
||
141 | * |
||
142 | * @internal |
||
143 | * |
||
144 | * @param string $checkScopeSegment |
||
145 | * |
||
146 | * @return bool Returns the new number of elements in the array. |
||
147 | */ |
||
148 | 32 | View Code Duplication | public function isRequested($checkScopeSegment) |
161 | |||
162 | /** |
||
163 | * Is Excluded. |
||
164 | * |
||
165 | * Check if - in relation to the current scope - this specific segment should |
||
166 | * be excluded. That means, if a.b.c is excluded and the current scope is a.b, |
||
167 | * then c will not be allowed in the transformation whether it appears in |
||
168 | * the list of default or available, requested includes. |
||
169 | * |
||
170 | * @internal |
||
171 | * |
||
172 | * @param string $checkScopeSegment |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | 23 | View Code Duplication | public function isExcluded($checkScopeSegment) |
189 | |||
190 | /** |
||
191 | * Push Parent Scope. |
||
192 | * |
||
193 | * Push a scope identifier into parentScopes |
||
194 | * |
||
195 | * @internal |
||
196 | * |
||
197 | * @param string $identifierSegment |
||
198 | * |
||
199 | * @return int Returns the new number of elements in the array. |
||
200 | */ |
||
201 | 1 | public function pushParentScope($identifierSegment) |
|
205 | |||
206 | /** |
||
207 | * Set parent scopes. |
||
208 | * |
||
209 | * @internal |
||
210 | * |
||
211 | * @param string[] $parentScopes Value to set. |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | 27 | public function setParentScopes($parentScopes) |
|
221 | |||
222 | /** |
||
223 | * Convert the current data for this scope to an array. |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | 41 | public function toArray() |
|
273 | |||
274 | /** |
||
275 | * Convert the current data for this scope to JSON. |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | 29 | public function toJson() |
|
283 | |||
284 | /** |
||
285 | * Execute the resources transformer and return the data and included data. |
||
286 | * |
||
287 | * @internal |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | 41 | protected function executeResourceTransformers() |
|
316 | |||
317 | /** |
||
318 | * Serialize a resource |
||
319 | * |
||
320 | * @internal |
||
321 | * |
||
322 | * @param SerializerAbstract $serializer |
||
323 | * @param mixed $data |
||
324 | * |
||
325 | * @return array |
||
326 | */ |
||
327 | 39 | protected function serializeResource(SerializerAbstract $serializer, $data) |
|
341 | |||
342 | /** |
||
343 | * Fire the main transformer. |
||
344 | * |
||
345 | * @internal |
||
346 | * |
||
347 | * @param TransformerAbstract|callable $transformer |
||
348 | * @param mixed $data |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | 39 | protected function fireTransformer($transformer, $data) |
|
370 | |||
371 | /** |
||
372 | * Fire the included transformers. |
||
373 | * |
||
374 | * @internal |
||
375 | * |
||
376 | * @param \League\Fractal\TransformerAbstract $transformer |
||
377 | * @param mixed $data |
||
378 | * |
||
379 | * @return array |
||
380 | */ |
||
381 | 33 | protected function fireIncludedTransformers($transformer, $data) |
|
385 | |||
386 | /** |
||
387 | * Determine if a transformer has any available includes. |
||
388 | * |
||
389 | * @internal |
||
390 | * |
||
391 | * @param TransformerAbstract|callable $transformer |
||
392 | * |
||
393 | * @return bool |
||
394 | */ |
||
395 | 39 | protected function transformerHasIncludes($transformer) |
|
406 | |||
407 | /** |
||
408 | * Check, if this is the root scope. |
||
409 | * |
||
410 | * @return bool |
||
411 | */ |
||
412 | 25 | protected function isRootScope() |
|
416 | } |
||
417 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.