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 |
||
36 | class IronicSerialiser implements IObjectSerialiser |
||
37 | { |
||
38 | /** |
||
39 | * The service implementation. |
||
40 | * |
||
41 | * @var IService |
||
42 | */ |
||
43 | protected $service; |
||
44 | |||
45 | /** |
||
46 | * Request description instance describes OData request the |
||
47 | * the client has submitted and result of the request. |
||
48 | * |
||
49 | * @var RequestDescription |
||
50 | */ |
||
51 | protected $request; |
||
52 | |||
53 | /** |
||
54 | * Collection of complex type instances used for cycle detection. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $complexTypeInstanceCollection; |
||
59 | |||
60 | /** |
||
61 | * Absolute service Uri. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $absoluteServiceUri; |
||
66 | |||
67 | /** |
||
68 | * Absolute service Uri with slash. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $absoluteServiceUriWithSlash; |
||
73 | |||
74 | /** |
||
75 | * Holds reference to segment stack being processed. |
||
76 | * |
||
77 | * @var SegmentStack |
||
78 | */ |
||
79 | protected $stack; |
||
80 | |||
81 | /** |
||
82 | * Lightweight stack tracking for recursive descent fill |
||
83 | */ |
||
84 | private $lightStack = []; |
||
85 | |||
86 | private $modelSerialiser; |
||
87 | |||
88 | /** |
||
89 | * @param IService $service Reference to the data service instance |
||
90 | * @param RequestDescription $request Type instance describing the client submitted request |
||
91 | */ |
||
92 | public function __construct(IService $service, RequestDescription $request = null) |
||
102 | |||
103 | /** |
||
104 | * Write a top level entry resource. |
||
105 | * |
||
106 | * @param mixed $entryObject Reference to the entry object to be written |
||
107 | * |
||
108 | * @return ODataEntry |
||
109 | */ |
||
110 | public function writeTopLevelElement($entryObject) |
||
193 | |||
194 | /** |
||
195 | * Write top level feed element. |
||
196 | * |
||
197 | * @param array &$entryObjects Array of entry resources to be written |
||
198 | * |
||
199 | * @return ODataFeed |
||
200 | */ |
||
201 | public function writeTopLevelElements(&$entryObjects) |
||
242 | |||
243 | /** |
||
244 | * Write top level url element. |
||
245 | * |
||
246 | * @param mixed $entryObject The entry resource whose url to be written |
||
247 | * |
||
248 | * @return ODataURL |
||
249 | */ |
||
250 | public function writeUrlElement($entryObject) |
||
266 | |||
267 | /** |
||
268 | * Write top level url collection. |
||
269 | * |
||
270 | * @param array $entryObjects Array of entry resources |
||
271 | * whose url to be written |
||
272 | * |
||
273 | * @return ODataURLCollection |
||
274 | */ |
||
275 | public function writeUrlElements($entryObjects) |
||
302 | |||
303 | /** |
||
304 | * Write top level complex resource. |
||
305 | * |
||
306 | * @param mixed &$complexValue The complex object to be |
||
307 | * written |
||
308 | * @param string $propertyName The name of the |
||
309 | * complex property |
||
310 | * @param ResourceType &$resourceType Describes the type of |
||
311 | * complex object |
||
312 | * |
||
313 | * @return ODataPropertyContent |
||
314 | * @codeCoverageIgnore |
||
315 | */ |
||
316 | public function writeTopLevelComplexObject(&$complexValue, $propertyName, ResourceType &$resourceType) |
||
320 | |||
321 | /** |
||
322 | * Write top level bag resource. |
||
323 | * |
||
324 | * @param mixed &$BagValue The bag object to be |
||
325 | * written |
||
326 | * @param string $propertyName The name of the |
||
327 | * bag property |
||
328 | * @param ResourceType &$resourceType Describes the type of |
||
329 | * bag object |
||
330 | * @codeCoverageIgnore |
||
331 | * @return ODataPropertyContent |
||
332 | */ |
||
333 | public function writeTopLevelBagObject(&$BagValue, $propertyName, ResourceType &$resourceType) |
||
337 | |||
338 | /** |
||
339 | * Write top level primitive value. |
||
340 | * |
||
341 | * @param mixed &$primitiveValue The primitve value to be |
||
342 | * written |
||
343 | * @param ResourceProperty &$resourceProperty Resource property |
||
344 | * describing the |
||
345 | * primitive property |
||
346 | * to be written |
||
347 | * @codeCoverageIgnore |
||
348 | * @return ODataPropertyContent |
||
349 | */ |
||
350 | public function writeTopLevelPrimitive(&$primitiveValue, ResourceProperty &$resourceProperty = null) |
||
354 | |||
355 | /** |
||
356 | * Gets reference to the request submitted by client. |
||
357 | * |
||
358 | * @return RequestDescription |
||
359 | */ |
||
360 | public function getRequest() |
||
366 | |||
367 | /** |
||
368 | * Sets reference to the request submitted by client. |
||
369 | * |
||
370 | * @param RequestDescription $request |
||
371 | */ |
||
372 | public function setRequest(RequestDescription $request) |
||
377 | |||
378 | /** |
||
379 | * Gets the data service instance. |
||
380 | * |
||
381 | * @return IService |
||
382 | */ |
||
383 | public function getService() |
||
387 | |||
388 | /** |
||
389 | * Gets the segment stack instance. |
||
390 | * |
||
391 | * @return SegmentStack |
||
392 | */ |
||
393 | public function getStack() |
||
397 | |||
398 | protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
||
425 | |||
426 | /** |
||
427 | * @param $entryObject |
||
428 | * @param $type |
||
429 | * @param $relativeUri |
||
430 | * @param $resourceType |
||
431 | * @return array |
||
432 | */ |
||
433 | protected function writeMediaData($entryObject, $type, $relativeUri, ResourceType $resourceType) |
||
471 | |||
472 | /** |
||
473 | * Gets collection of projection nodes under the current node. |
||
474 | * |
||
475 | * @return ProjectionNode[]|ExpandedProjectionNode[]|null List of nodes |
||
476 | * describing projections for the current segment, If this method returns |
||
477 | * null it means no projections are to be applied and the entire resource |
||
478 | * for the current segment should be serialized, If it returns non-null |
||
479 | * only the properties described by the returned projection segments should |
||
480 | * be serialized |
||
481 | */ |
||
482 | protected function getProjectionNodes() |
||
491 | |||
492 | /** |
||
493 | * Find a 'ExpandedProjectionNode' instance in the projection tree |
||
494 | * which describes the current segment. |
||
495 | * |
||
496 | * @return ExpandedProjectionNode|null |
||
497 | */ |
||
498 | protected function getCurrentExpandedProjectionNode() |
||
526 | |||
527 | /** |
||
528 | * Check whether to expand a navigation property or not. |
||
529 | * |
||
530 | * @param string $navigationPropertyName Name of naviagtion property in question |
||
531 | * |
||
532 | * @return bool True if the given navigation should be |
||
533 | * explanded otherwise false |
||
534 | */ |
||
535 | protected function shouldExpandSegment($navigationPropertyName) |
||
547 | |||
548 | /** |
||
549 | * Wheter next link is needed for the current resource set (feed) |
||
550 | * being serialized. |
||
551 | * |
||
552 | * @param int $resultSetCount Number of entries in the current |
||
553 | * resource set |
||
554 | * |
||
555 | * @return bool true if the feed must have a next page link |
||
556 | */ |
||
557 | protected function needNextPageLink($resultSetCount) |
||
572 | |||
573 | /** |
||
574 | * Resource set wrapper for the resource being serialized. |
||
575 | * |
||
576 | * @return ResourceSetWrapper |
||
577 | */ |
||
578 | protected function getCurrentResourceSetWrapper() |
||
585 | |||
586 | /** |
||
587 | * Get next page link from the given entity instance. |
||
588 | * |
||
589 | * @param mixed &$lastObject Last object serialized to be |
||
590 | * used for generating $skiptoken |
||
591 | * @param string $absoluteUri Absolute response URI |
||
592 | * |
||
593 | * @return string for the link for next page |
||
594 | */ |
||
595 | protected function getNextLinkUri(&$lastObject, $absoluteUri) |
||
610 | |||
611 | /** |
||
612 | * Builds the string corresponding to query parameters for top level results |
||
613 | * (result set identified by the resource path) to be put in next page link. |
||
614 | * |
||
615 | * @return string|null string representing the query parameters in the URI |
||
616 | * query parameter format, NULL if there |
||
617 | * is no query parameters |
||
618 | * required for the next link of top level result set |
||
619 | */ |
||
620 | protected function getNextPageLinkQueryParametersForRootResourceSet() |
||
654 | |||
655 | private function loadStackIfEmpty() |
||
662 | |||
663 | /** |
||
664 | * Convert the given primitive value to string. |
||
665 | * Note: This method will not handle null primitive value. |
||
666 | * |
||
667 | * @param IType &$primitiveResourceType Type of the primitive property |
||
668 | * whose value need to be converted |
||
669 | * @param mixed $primitiveValue Primitive value to convert |
||
670 | * |
||
671 | * @return string |
||
672 | */ |
||
673 | private function primitiveToString(IType &$type, $primitiveValue) |
||
689 | |||
690 | /** |
||
691 | * @param $entryObject |
||
692 | * @param $nonRelProp |
||
693 | * @return ODataPropertyContent |
||
694 | */ |
||
695 | private function writePrimitiveProperties($entryObject, $nonRelProp) |
||
713 | |||
714 | /** |
||
715 | * @param $entryObject |
||
716 | * @param $prop |
||
717 | * @param $nuLink |
||
718 | * @param $propKind |
||
719 | * @param $propName |
||
720 | */ |
||
721 | private function expandNavigationProperty($entryObject, $prop, $nuLink, $propKind, $propName) |
||
747 | |||
748 | /** |
||
749 | * Gets the data service instance. |
||
750 | * |
||
751 | * @return IService |
||
752 | */ |
||
753 | public function setService(IService $service) |
||
759 | } |
||
760 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.