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 OperationContext 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 OperationContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class OperationContext implements OperationContextInterface |
||
19 | { |
||
20 | const PROPERTIES_WILDCARD = '*'; |
||
21 | |||
22 | /** |
||
23 | * @var string[] |
||
24 | */ |
||
25 | private $filter = []; |
||
26 | |||
27 | /** |
||
28 | * @var boolean |
||
29 | */ |
||
30 | private $loadSecondaryTypeProperties = false; |
||
31 | |||
32 | /** |
||
33 | * @var boolean |
||
34 | */ |
||
35 | private $includeAcls = false; |
||
36 | |||
37 | /** |
||
38 | * @var boolean |
||
39 | */ |
||
40 | private $includeAllowableActions = true; |
||
41 | |||
42 | /** |
||
43 | * @var boolean |
||
44 | */ |
||
45 | private $includePolicies = false; |
||
46 | |||
47 | /** |
||
48 | * @var IncludeRelationships |
||
49 | */ |
||
50 | private $includeRelationships = null; |
||
51 | |||
52 | /** |
||
53 | * @var string[] |
||
54 | */ |
||
55 | private $renditionFilter = []; |
||
56 | |||
57 | /** |
||
58 | * @var boolean |
||
59 | */ |
||
60 | private $includePathSegments = true; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $orderBy = null; |
||
66 | |||
67 | /** |
||
68 | * @var boolean |
||
69 | */ |
||
70 | private $cacheEnabled = false; |
||
71 | |||
72 | /** |
||
73 | * @var integer |
||
74 | */ |
||
75 | private $maxItemsPerPage = 100; |
||
76 | |||
77 | /** |
||
78 | * Creates new Operation Context |
||
79 | */ |
||
80 | 61 | public function __construct() |
|
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 1 | public function isCacheEnabled() |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 9 | public function setCacheEnabled($cacheEnabled) |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 1 | public function getCacheKey() |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 1 | public function getFilter() |
|
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | 8 | View Code Duplication | public function setFilter(array $propertyFilters) |
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | 2 | public function isIncludeAcls() |
|
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | 2 | public function setIncludeAcls($includeAcls) |
|
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | 2 | public function isIncludeAllowableActions() |
|
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | 2 | public function setIncludeAllowableActions($includeAllowableActions) |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 2 | public function isIncludePathSegments() |
|
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | 2 | public function setIncludePathSegments($includePathSegments) |
|
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | 2 | public function isIncludePolicies() |
|
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | 2 | public function setIncludePolicies($includePolicies) |
|
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | 2 | public function getIncludeRelationships() |
|
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | 2 | public function setIncludeRelationships(IncludeRelationships $includeRelationships) |
|
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | 3 | public function loadSecondaryTypeProperties() |
|
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | 2 | public function setLoadSecondaryTypeProperties($loadSecondaryTypeProperties) |
|
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | 1 | public function getMaxItemsPerPage() |
|
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | */ |
||
275 | 2 | public function setMaxItemsPerPage($maxItemsPerPage) |
|
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | 1 | public function getOrderBy() |
|
292 | |||
293 | /** |
||
294 | * {@inheritdoc} |
||
295 | */ |
||
296 | 1 | public function setOrderBy($orderBy) |
|
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | 1 | public function getRenditionFilter() |
|
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | 61 | View Code Duplication | public function setRenditionFilter(array $renditionFilter) |
338 | |||
339 | |||
340 | /** |
||
341 | * {@inheritdoc} |
||
342 | */ |
||
343 | 4 | public function getQueryFilterString() |
|
364 | |||
365 | /** |
||
366 | * {@inheritdoc} |
||
367 | */ |
||
368 | 3 | public function getRenditionFilterString() |
|
376 | |||
377 | /** |
||
378 | * {@inheritdoc} |
||
379 | */ |
||
380 | 1 | public function setFilterString($propertyFilter) |
|
390 | |||
391 | /** |
||
392 | * {@inheritdoc} |
||
393 | */ |
||
394 | 1 | public function setRenditionFilterString($renditionFilter) |
|
404 | } |
||
405 |
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.