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:
Complex classes like Scope often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Scope, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Scope implements \JsonSerializable |
||
30 | { |
||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $availableIncludes = []; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $scopeIdentifier; |
||
40 | |||
41 | /** |
||
42 | * @var \League\Fractal\Manager |
||
43 | */ |
||
44 | protected $manager; |
||
45 | |||
46 | /** |
||
47 | * @var ResourceInterface |
||
48 | */ |
||
49 | protected $resource; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $parentScopes = []; |
||
55 | |||
56 | /** |
||
57 | * Create a new scope instance. |
||
58 | * |
||
59 | * @param Manager $manager |
||
60 | * @param ResourceInterface $resource |
||
61 | * @param string $scopeIdentifier |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | 76 | public function __construct(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null) |
|
71 | |||
72 | /** |
||
73 | * Embed a scope as a child of the current scope. |
||
74 | * |
||
75 | * @internal |
||
76 | * |
||
77 | * @param string $scopeIdentifier |
||
78 | * @param ResourceInterface $resource |
||
79 | * |
||
80 | * @return \League\Fractal\Scope |
||
81 | */ |
||
82 | 36 | public function embedChildScope($scopeIdentifier, $resource) |
|
86 | |||
87 | /** |
||
88 | * Get the current identifier. |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | 38 | public function getScopeIdentifier() |
|
96 | |||
97 | /** |
||
98 | * Get the unique identifier for this scope. |
||
99 | * |
||
100 | * @param string $appendIdentifier |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | 32 | public function getIdentifier($appendIdentifier = null) |
|
110 | |||
111 | /** |
||
112 | * Getter for parentScopes. |
||
113 | * |
||
114 | * @return mixed |
||
115 | */ |
||
116 | 38 | public function getParentScopes() |
|
120 | |||
121 | /** |
||
122 | * Getter for resource. |
||
123 | * |
||
124 | * @return ResourceInterface |
||
125 | */ |
||
126 | 33 | public function getResource() |
|
130 | |||
131 | /** |
||
132 | * Getter for manager. |
||
133 | * |
||
134 | * @return \League\Fractal\Manager |
||
135 | */ |
||
136 | 32 | public function getManager() |
|
140 | |||
141 | /** |
||
142 | * Is Requested. |
||
143 | * |
||
144 | * Check if - in relation to the current scope - this specific segment is allowed. |
||
145 | * That means, if a.b.c is requested and the current scope is a.b, then c is allowed. If the current |
||
146 | * scope is a then c is not allowed, even if it is there and potentially transformable. |
||
147 | * |
||
148 | * @internal |
||
149 | * |
||
150 | * @param string $checkScopeSegment |
||
151 | * |
||
152 | * @return bool Returns the new number of elements in the array. |
||
153 | */ |
||
154 | 44 | View Code Duplication | public function isRequested($checkScopeSegment) |
|
|||
155 | { |
||
156 | 44 | if ($this->parentScopes) { |
|
157 | 21 | $scopeArray = array_slice($this->parentScopes, 1); |
|
158 | 21 | array_push($scopeArray, $this->scopeIdentifier, $checkScopeSegment); |
|
159 | 21 | } else { |
|
160 | 44 | $scopeArray = [$checkScopeSegment]; |
|
161 | } |
||
162 | |||
163 | 44 | $scopeString = implode('.', (array) $scopeArray); |
|
164 | |||
165 | 44 | return in_array($scopeString, $this->manager->getRequestedIncludes()); |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Is Excluded. |
||
170 | * |
||
171 | * Check if - in relation to the current scope - this specific segment should |
||
172 | * be excluded. That means, if a.b.c is excluded and the current scope is a.b, |
||
173 | * then c will not be allowed in the transformation whether it appears in |
||
174 | * the list of default or available, requested includes. |
||
175 | * |
||
176 | * @internal |
||
177 | * |
||
178 | * @param string $checkScopeSegment |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | 32 | View Code Duplication | public function isExcluded($checkScopeSegment) |
183 | { |
||
184 | 32 | if ($this->parentScopes) { |
|
185 | 7 | $scopeArray = array_slice($this->parentScopes, 1); |
|
186 | 7 | array_push($scopeArray, $this->scopeIdentifier, $checkScopeSegment); |
|
187 | 7 | } else { |
|
188 | 32 | $scopeArray = [$checkScopeSegment]; |
|
189 | } |
||
190 | |||
191 | 32 | $scopeString = implode('.', (array) $scopeArray); |
|
192 | |||
193 | 32 | return in_array($scopeString, $this->manager->getRequestedExcludes()); |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * Push Parent Scope. |
||
198 | * |
||
199 | * Push a scope identifier into parentScopes |
||
200 | * |
||
201 | * @internal |
||
202 | * |
||
203 | * @param string $identifierSegment |
||
204 | * |
||
205 | * @return int Returns the new number of elements in the array. |
||
206 | */ |
||
207 | 1 | public function pushParentScope($identifierSegment) |
|
211 | |||
212 | /** |
||
213 | * Set parent scopes. |
||
214 | * |
||
215 | * @internal |
||
216 | * |
||
217 | * @param string[] $parentScopes Value to set. |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | 37 | public function setParentScopes($parentScopes) |
|
227 | |||
228 | /** |
||
229 | * Convert the current data for this scope to an array. |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | 70 | public function toArray() |
|
294 | |||
295 | /** |
||
296 | * @return array |
||
297 | */ |
||
298 | 41 | public function jsonSerialize() |
|
302 | |||
303 | /** |
||
304 | * Convert the current data for this scope to JSON. |
||
305 | * |
||
306 | * @param int $options |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | 40 | public function toJson($options = 0) |
|
314 | |||
315 | /** |
||
316 | * Transformer a primitive resource |
||
317 | * |
||
318 | * @return mixed |
||
319 | */ |
||
320 | 2 | public function transformPrimitiveResource() |
|
342 | |||
343 | /** |
||
344 | * Execute the resources transformer and return the data and included data. |
||
345 | * |
||
346 | * @internal |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | 62 | protected function executeResourceTransformers() |
|
375 | |||
376 | /** |
||
377 | * Serialize a resource |
||
378 | * |
||
379 | * @internal |
||
380 | * |
||
381 | * @param SerializerAbstract $serializer |
||
382 | * @param mixed $data |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | 60 | protected function serializeResource(SerializerAbstract $serializer, $data) |
|
400 | |||
401 | /** |
||
402 | * Fire the main transformer. |
||
403 | * |
||
404 | * @internal |
||
405 | * |
||
406 | * @param TransformerAbstract|callable $transformer |
||
407 | * @param mixed $data |
||
408 | * |
||
409 | * @return array |
||
410 | */ |
||
411 | 58 | protected function fireTransformer($transformer, $data) |
|
432 | |||
433 | /** |
||
434 | * Fire the included transformers. |
||
435 | * |
||
436 | * @internal |
||
437 | * |
||
438 | * @param \League\Fractal\TransformerAbstract $transformer |
||
439 | * @param mixed $data |
||
440 | * |
||
441 | * @return array |
||
442 | */ |
||
443 | 49 | protected function fireIncludedTransformers($transformer, $data) |
|
449 | |||
450 | /** |
||
451 | * Determine if a transformer has any available includes. |
||
452 | * |
||
453 | * @internal |
||
454 | * |
||
455 | * @param TransformerAbstract|callable $transformer |
||
456 | * |
||
457 | * @return bool |
||
458 | */ |
||
459 | 58 | protected function transformerHasIncludes($transformer) |
|
470 | |||
471 | /** |
||
472 | * Check, if this is the root scope. |
||
473 | * |
||
474 | * @return bool |
||
475 | */ |
||
476 | 38 | protected function isRootScope() |
|
480 | |||
481 | /** |
||
482 | * Filter the provided data with the requested filter fieldset for |
||
483 | * the scope resource |
||
484 | * |
||
485 | * @internal |
||
486 | * |
||
487 | * @param array $data |
||
488 | * |
||
489 | * @return array |
||
490 | */ |
||
491 | 58 | protected function filterFieldsets(array $data) |
|
507 | |||
508 | /** |
||
509 | * Return the requested filter fieldset for the scope resource |
||
510 | * |
||
511 | * @internal |
||
512 | * |
||
513 | * @return \League\Fractal\ParamBag|null |
||
514 | */ |
||
515 | 58 | protected function getFilterFieldset() |
|
519 | |||
520 | /** |
||
521 | * Check if there are requested filter fieldsets for the scope resource. |
||
522 | * |
||
523 | * @internal |
||
524 | * |
||
525 | * @return bool |
||
526 | */ |
||
527 | 58 | protected function hasFilterFieldset() |
|
531 | |||
532 | /** |
||
533 | * Return the scope resource type. |
||
534 | * |
||
535 | * @internal |
||
536 | * |
||
537 | * @return string |
||
538 | */ |
||
539 | 58 | protected function getResourceType() |
|
543 | } |
||
544 |
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.