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 IronicSerialiser 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 IronicSerialiser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class IronicSerialiser implements IObjectSerialiser |
||
32 | { |
||
33 | /** |
||
34 | * The service implementation. |
||
35 | * |
||
36 | * @var IService |
||
37 | */ |
||
38 | protected $service; |
||
39 | |||
40 | /** |
||
41 | * Request description instance describes OData request the |
||
42 | * the client has submitted and result of the request. |
||
43 | * |
||
44 | * @var RequestDescription |
||
45 | */ |
||
46 | protected $request; |
||
47 | |||
48 | /** |
||
49 | * Collection of complex type instances used for cycle detection. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $complexTypeInstanceCollection; |
||
54 | |||
55 | /** |
||
56 | * Absolute service Uri. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $absoluteServiceUri; |
||
61 | |||
62 | /** |
||
63 | * Absolute service Uri with slash. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $absoluteServiceUriWithSlash; |
||
68 | |||
69 | /** |
||
70 | * Holds reference to segment stack being processed. |
||
71 | * |
||
72 | * @var SegmentStack |
||
73 | */ |
||
74 | protected $stack; |
||
75 | |||
76 | /** |
||
77 | * Lightweight stack tracking for recursive descent fill |
||
78 | */ |
||
79 | private $lightStack = []; |
||
80 | |||
81 | /** |
||
82 | * @param IService $service Reference to the data service instance |
||
83 | * @param RequestDescription $request Type instance describing the client submitted request |
||
84 | */ |
||
85 | public function __construct(IService $service, RequestDescription $request = null) |
||
94 | |||
95 | /** |
||
96 | * Write a top level entry resource. |
||
97 | * |
||
98 | * @param mixed $entryObject Reference to the entry object to be written |
||
99 | * |
||
100 | * @return ODataEntry |
||
101 | */ |
||
102 | public function writeTopLevelElement($entryObject) |
||
207 | |||
208 | /** |
||
209 | * Write top level feed element. |
||
210 | * |
||
211 | * @param array &$entryObjects Array of entry resources to be written |
||
212 | * |
||
213 | * @return ODataFeed |
||
214 | */ |
||
215 | public function writeTopLevelElements(&$entryObjects) |
||
260 | |||
261 | /** |
||
262 | * Write top level url element. |
||
263 | * |
||
264 | * @param mixed $entryObject The entry resource whose url to be written |
||
265 | * |
||
266 | * @return ODataURL |
||
267 | */ |
||
268 | public function writeUrlElement($entryObject) |
||
284 | |||
285 | /** |
||
286 | * Write top level url collection. |
||
287 | * |
||
288 | * @param array $entryObjects Array of entry resources |
||
289 | * whose url to be written |
||
290 | * |
||
291 | * @return ODataURLCollection |
||
292 | */ |
||
293 | public function writeUrlElements($entryObjects) |
||
320 | |||
321 | /** |
||
322 | * Write top level complex resource. |
||
323 | * |
||
324 | * @param mixed &$complexValue The complex object to be |
||
325 | * written |
||
326 | * @param string $propertyName The name of the |
||
327 | * complex property |
||
328 | * @param ResourceType &$resourceType Describes the type of |
||
329 | * complex object |
||
330 | * |
||
331 | * @return ODataPropertyContent |
||
332 | * @codeCoverageIgnore |
||
333 | */ |
||
334 | public function writeTopLevelComplexObject(&$complexValue, $propertyName, ResourceType &$resourceType) |
||
338 | |||
339 | /** |
||
340 | * Write top level bag resource. |
||
341 | * |
||
342 | * @param mixed &$BagValue The bag object to be |
||
343 | * written |
||
344 | * @param string $propertyName The name of the |
||
345 | * bag property |
||
346 | * @param ResourceType &$resourceType Describes the type of |
||
347 | * bag object |
||
348 | * @codeCoverageIgnore |
||
349 | * @return ODataPropertyContent |
||
350 | */ |
||
351 | public function writeTopLevelBagObject(&$BagValue, $propertyName, ResourceType &$resourceType) |
||
355 | |||
356 | /** |
||
357 | * Write top level primitive value. |
||
358 | * |
||
359 | * @param mixed &$primitiveValue The primitve value to be |
||
360 | * written |
||
361 | * @param ResourceProperty &$resourceProperty Resource property |
||
362 | * describing the |
||
363 | * primitive property |
||
364 | * to be written |
||
365 | * @codeCoverageIgnore |
||
366 | * @return ODataPropertyContent |
||
367 | */ |
||
368 | public function writeTopLevelPrimitive(&$primitiveValue, ResourceProperty &$resourceProperty = null) |
||
372 | |||
373 | /** |
||
374 | * Gets reference to the request submitted by client. |
||
375 | * |
||
376 | * @return RequestDescription |
||
377 | */ |
||
378 | public function getRequest() |
||
384 | |||
385 | /** |
||
386 | * Sets reference to the request submitted by client. |
||
387 | * |
||
388 | * @param RequestDescription $request |
||
389 | */ |
||
390 | public function setRequest(RequestDescription $request) |
||
395 | |||
396 | /** |
||
397 | * Gets the data service instance. |
||
398 | * |
||
399 | * @return IService |
||
400 | */ |
||
401 | public function getService() |
||
405 | |||
406 | /** |
||
407 | * Gets the segment stack instance. |
||
408 | * |
||
409 | * @return SegmentStack |
||
410 | */ |
||
411 | public function getStack() |
||
415 | |||
416 | protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
||
443 | |||
444 | /** |
||
445 | * @param $entryObject |
||
446 | * @param $type |
||
447 | * @param $relativeUri |
||
448 | * @param $resourceType |
||
449 | * @return array |
||
450 | */ |
||
451 | protected function writeMediaData($entryObject, $type, $relativeUri, ResourceType $resourceType) |
||
489 | |||
490 | /** |
||
491 | * Gets collection of projection nodes under the current node. |
||
492 | * |
||
493 | * @return ProjectionNode[]|ExpandedProjectionNode[]|null List of nodes |
||
494 | * describing projections for the current segment, If this method returns |
||
495 | * null it means no projections are to be applied and the entire resource |
||
496 | * for the current segment should be serialized, If it returns non-null |
||
497 | * only the properties described by the returned projection segments should |
||
498 | * be serialized |
||
499 | */ |
||
500 | protected function getProjectionNodes() |
||
509 | |||
510 | /** |
||
511 | * Find a 'ExpandedProjectionNode' instance in the projection tree |
||
512 | * which describes the current segment. |
||
513 | * |
||
514 | * @return ExpandedProjectionNode|null |
||
515 | */ |
||
516 | protected function getCurrentExpandedProjectionNode() |
||
544 | |||
545 | /** |
||
546 | * Check whether to expand a navigation property or not. |
||
547 | * |
||
548 | * @param string $navigationPropertyName Name of naviagtion property in question |
||
549 | * |
||
550 | * @return bool True if the given navigation should be |
||
551 | * explanded otherwise false |
||
552 | */ |
||
553 | protected function shouldExpandSegment($navigationPropertyName) |
||
565 | |||
566 | /** |
||
567 | * Wheter next link is needed for the current resource set (feed) |
||
568 | * being serialized. |
||
569 | * |
||
570 | * @param int $resultSetCount Number of entries in the current |
||
571 | * resource set |
||
572 | * |
||
573 | * @return bool true if the feed must have a next page link |
||
574 | */ |
||
575 | protected function needNextPageLink($resultSetCount) |
||
591 | |||
592 | /** |
||
593 | * Resource set wrapper for the resource being serialized. |
||
594 | * |
||
595 | * @return ResourceSetWrapper |
||
596 | */ |
||
597 | protected function getCurrentResourceSetWrapper() |
||
604 | |||
605 | /** |
||
606 | * Get next page link from the given entity instance. |
||
607 | * |
||
608 | * @param mixed &$lastObject Last object serialized to be |
||
609 | * used for generating $skiptoken |
||
610 | * @param string $absoluteUri Absolute response URI |
||
611 | * |
||
612 | * @return string for the link for next page |
||
613 | */ |
||
614 | protected function getNextLinkUri(&$lastObject, $absoluteUri) |
||
623 | } |
||
624 |
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.