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 |
||
49 | class IronicSerialiser implements IObjectSerialiser |
||
50 | { |
||
51 | const PK = 'PrimaryKey'; |
||
52 | |||
53 | private $propertiesCache = []; |
||
54 | |||
55 | /** |
||
56 | * @var RootProjectionNode |
||
57 | */ |
||
58 | private $rootNode = null; |
||
59 | |||
60 | /** |
||
61 | * The service implementation. |
||
62 | * |
||
63 | * @var IService |
||
64 | */ |
||
65 | protected $service; |
||
66 | |||
67 | /** |
||
68 | * Request description instance describes OData request the |
||
69 | * the client has submitted and result of the request. |
||
70 | * |
||
71 | * @var RequestDescription |
||
72 | */ |
||
73 | protected $request; |
||
74 | |||
75 | /** |
||
76 | * Collection of complex type instances used for cycle detection. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $complexTypeInstanceCollection; |
||
81 | |||
82 | /** |
||
83 | * Absolute service Uri. |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $absoluteServiceUri; |
||
88 | |||
89 | /** |
||
90 | * Absolute service Uri with slash. |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $absoluteServiceUriWithSlash; |
||
95 | |||
96 | /** |
||
97 | * Holds reference to segment stack being processed. |
||
98 | * |
||
99 | * @var SegmentStack |
||
100 | */ |
||
101 | protected $stack; |
||
102 | |||
103 | /** |
||
104 | * Lightweight stack tracking for recursive descent fill. |
||
105 | */ |
||
106 | protected $lightStack = []; |
||
107 | |||
108 | /** |
||
109 | * @var ModelSerialiser |
||
110 | */ |
||
111 | private $modelSerialiser; |
||
112 | |||
113 | |||
114 | |||
115 | /** |
||
116 | * @var IMetadataProvider |
||
117 | */ |
||
118 | private $metaProvider; |
||
119 | |||
120 | /* |
||
121 | * Update time to insert into ODataEntry/ODataFeed fields |
||
122 | * @var Carbon; |
||
123 | */ |
||
124 | private $updated; |
||
125 | |||
126 | /* |
||
127 | * Has base URI already been written out during serialisation? |
||
128 | * @var bool; |
||
129 | */ |
||
130 | private $isBaseWritten = false; |
||
131 | |||
132 | /** |
||
133 | * @param IService $service Reference to the data service instance |
||
134 | * @param RequestDescription|null $request Type instance describing the client submitted request |
||
135 | */ |
||
136 | public function __construct(IService $service, RequestDescription $request = null) |
||
147 | |||
148 | /** |
||
149 | * Write a top level entry resource. |
||
150 | * |
||
151 | * @param QueryResult $entryObject Reference to the entry object to be written |
||
152 | * |
||
153 | * @return ODataEntry|null |
||
154 | */ |
||
155 | public function writeTopLevelElement(QueryResult $entryObject) |
||
156 | { |
||
157 | if (!isset($entryObject->results)) { |
||
158 | array_pop($this->lightStack); |
||
159 | return null; |
||
160 | } |
||
161 | assert($entryObject instanceof QueryResult, get_class($entryObject)); |
||
162 | assert($entryObject->results instanceof Model, get_class($entryObject->results)); |
||
163 | $this->loadStackIfEmpty(); |
||
164 | |||
165 | $baseURI = $this->isBaseWritten ? null : $this->absoluteServiceUriWithSlash; |
||
166 | $this->isBaseWritten = true; |
||
167 | |||
168 | $stackCount = count($this->lightStack); |
||
169 | $topOfStack = $this->lightStack[$stackCount-1]; |
||
170 | $payloadClass = get_class($entryObject->results); |
||
171 | $resourceType = $this->getService()->getProvidersWrapper()->resolveResourceType($topOfStack['type']); |
||
172 | |||
173 | // need gubbinz to unpack an abstract resource type |
||
174 | if ($resourceType->isAbstract()) { |
||
175 | $derived = $this->getMetadata()->getDerivedTypes($resourceType); |
||
176 | assert(0 < count($derived)); |
||
177 | foreach ($derived as $rawType) { |
||
178 | if (!$rawType->isAbstract()) { |
||
179 | $name = $rawType->getInstanceType()->getName(); |
||
180 | if ($payloadClass == $name) { |
||
181 | $resourceType = $rawType; |
||
182 | break; |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | // despite all set up, checking, etc, if we haven't picked a concrete resource type, |
||
187 | // wheels have fallen off, so blow up |
||
188 | assert(!$resourceType->isAbstract(), 'Concrete resource type not selected for payload ' . $payloadClass); |
||
189 | } |
||
190 | |||
191 | // make sure we're barking up right tree |
||
192 | assert($resourceType instanceof ResourceEntityType, get_class($resourceType)); |
||
193 | $targClass = $resourceType->getInstanceType()->getName(); |
||
194 | assert( |
||
195 | $entryObject->results instanceof $targClass, |
||
196 | 'Object being serialised not instance of expected class, ' . $targClass . ', is actually ' . $payloadClass |
||
197 | ); |
||
198 | |||
199 | if (!array_key_exists($targClass, $this->propertiesCache)) { |
||
200 | $rawProp = $resourceType->getAllProperties(); |
||
201 | $relProp = []; |
||
202 | $nonRelProp = []; |
||
203 | foreach ($rawProp as $prop) { |
||
204 | $propType = $prop->getResourceType(); |
||
205 | if ($propType instanceof ResourceEntityType) { |
||
206 | $relProp[] = $prop; |
||
207 | } else { |
||
208 | $nonRelProp[$prop->getName()] = ['prop' => $prop, 'type' => $propType->getInstanceType()]; |
||
209 | } |
||
210 | } |
||
211 | $this->propertiesCache[$targClass] = ['rel' => $relProp, 'nonRel' => $nonRelProp]; |
||
212 | } |
||
213 | unset($relProp); |
||
214 | unset($nonRelProp); |
||
215 | $relProp = $this->propertiesCache[$targClass]['rel']; |
||
216 | $nonRelProp = $this->propertiesCache[$targClass]['nonRel']; |
||
217 | |||
218 | $resourceSet = $resourceType->getCustomState(); |
||
219 | assert($resourceSet instanceof ResourceSet); |
||
220 | $title = $resourceType->getName(); |
||
221 | $type = $resourceType->getFullName(); |
||
222 | |||
223 | $relativeUri = $this->getEntryInstanceKey( |
||
224 | $entryObject->results, |
||
225 | $resourceType, |
||
226 | $resourceSet->getName() |
||
227 | ); |
||
228 | $absoluteUri = rtrim($this->absoluteServiceUri, '/') . '/' . $relativeUri; |
||
229 | |||
230 | list($mediaLink, $mediaLinks) = $this->writeMediaData( |
||
231 | $entryObject->results, |
||
232 | $type, |
||
233 | $relativeUri, |
||
234 | $resourceType |
||
235 | ); |
||
236 | |||
237 | $propertyContent = $this->writePrimitiveProperties($entryObject->results, $nonRelProp); |
||
238 | |||
239 | $links = []; |
||
240 | foreach ($relProp as $prop) { |
||
241 | $nuLink = new ODataLink(); |
||
242 | $propKind = $prop->getKind(); |
||
243 | |||
244 | assert( |
||
245 | ResourcePropertyKind::RESOURCESET_REFERENCE == $propKind |
||
246 | || ResourcePropertyKind::RESOURCE_REFERENCE == $propKind, |
||
247 | '$propKind != ResourcePropertyKind::RESOURCESET_REFERENCE &&' |
||
248 | .' $propKind != ResourcePropertyKind::RESOURCE_REFERENCE' |
||
249 | ); |
||
250 | $propTail = ResourcePropertyKind::RESOURCE_REFERENCE == $propKind ? 'entry' : 'feed'; |
||
251 | $propType = 'application/atom+xml;type=' . $propTail; |
||
252 | $propName = $prop->getName(); |
||
253 | $nuLink->title = $propName; |
||
254 | $nuLink->name = ODataConstants::ODATA_RELATED_NAMESPACE . $propName; |
||
255 | $nuLink->url = $relativeUri . '/' . $propName; |
||
256 | $nuLink->type = $propType; |
||
257 | |||
258 | $navProp = new ODataNavigationPropertyInfo($prop, $this->shouldExpandSegment($propName)); |
||
259 | if ($navProp->expanded) { |
||
260 | $this->expandNavigationProperty($entryObject, $prop, $nuLink, $propKind, $propName); |
||
261 | } |
||
262 | |||
263 | $links[] = $nuLink; |
||
264 | } |
||
265 | |||
266 | $odata = new ODataEntry(); |
||
267 | $odata->resourceSetName = $resourceSet->getName(); |
||
268 | $odata->id = $absoluteUri; |
||
269 | $odata->title = new ODataTitle($title); |
||
270 | $odata->type = new ODataCategory($type); |
||
271 | $odata->propertyContent = $propertyContent; |
||
272 | $odata->isMediaLinkEntry = $resourceType->isMediaLinkEntry(); |
||
273 | $odata->editLink = new ODataLink(); |
||
274 | $odata->editLink->url = $relativeUri; |
||
275 | $odata->editLink->name = 'edit'; |
||
276 | $odata->editLink->title = $title; |
||
277 | $odata->mediaLink = $mediaLink; |
||
278 | $odata->mediaLinks = $mediaLinks; |
||
279 | $odata->links = $links; |
||
280 | $odata->updated = $this->getUpdated()->format(DATE_ATOM); |
||
281 | $odata->baseURI = $baseURI; |
||
282 | |||
283 | $newCount = count($this->lightStack); |
||
284 | assert( |
||
285 | $newCount == $stackCount, |
||
286 | 'Should have ' . $stackCount . ' elements in stack, have ' . $newCount . ' elements' |
||
287 | ); |
||
288 | $this->lightStack[$newCount-1]['count']--; |
||
289 | if (0 == $this->lightStack[$newCount-1]['count']) { |
||
290 | array_pop($this->lightStack); |
||
291 | } |
||
292 | return $odata; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Write top level feed element. |
||
297 | * |
||
298 | * @param QueryResult &$entryObjects Array of entry resources to be written |
||
299 | * |
||
300 | * @return ODataFeed |
||
301 | */ |
||
302 | public function writeTopLevelElements(QueryResult & $entryObjects) |
||
359 | |||
360 | /** |
||
361 | * Write top level url element. |
||
362 | * |
||
363 | * @param QueryResult $entryObject The entry resource whose url to be written |
||
364 | * |
||
365 | * @return ODataURL |
||
366 | */ |
||
367 | public function writeUrlElement(QueryResult $entryObject) |
||
383 | |||
384 | /** |
||
385 | * Write top level url collection. |
||
386 | * |
||
387 | * @param QueryResult $entryObjects Array of entry resources whose url to be written |
||
388 | * |
||
389 | * @return ODataURLCollection |
||
390 | */ |
||
391 | public function writeUrlElements(QueryResult $entryObjects) |
||
424 | |||
425 | /** |
||
426 | * Write top level complex resource. |
||
427 | * |
||
428 | * @param QueryResult &$complexValue The complex object to be written |
||
429 | * @param string $propertyName The name of the complex property |
||
430 | * @param ResourceType &$resourceType Describes the type of complex object |
||
431 | * |
||
432 | * @return ODataPropertyContent |
||
433 | */ |
||
434 | public function writeTopLevelComplexObject(QueryResult & $complexValue, $propertyName, ResourceType & $resourceType) |
||
451 | |||
452 | /** |
||
453 | * Write top level bag resource. |
||
454 | * |
||
455 | * @param QueryResult &$BagValue The bag object to be |
||
456 | * written |
||
457 | * @param string $propertyName The name of the |
||
458 | * bag property |
||
459 | * @param ResourceType &$resourceType Describes the type of |
||
460 | * bag object |
||
461 | * |
||
462 | * @return ODataPropertyContent |
||
463 | */ |
||
464 | public function writeTopLevelBagObject(QueryResult & $BagValue, $propertyName, ResourceType & $resourceType) |
||
477 | |||
478 | /** |
||
479 | * Write top level primitive value. |
||
480 | * |
||
481 | * @param QueryResult &$primitiveValue The primitive value to be |
||
482 | * written |
||
483 | * @param ResourceProperty &$resourceProperty Resource property describing the |
||
484 | * primitive property to be written |
||
485 | * @return ODataPropertyContent |
||
486 | */ |
||
487 | public function writeTopLevelPrimitive(QueryResult & $primitiveValue, ResourceProperty & $resourceProperty = null) |
||
509 | |||
510 | /** |
||
511 | * Gets reference to the request submitted by client. |
||
512 | * |
||
513 | * @return RequestDescription |
||
514 | */ |
||
515 | public function getRequest() |
||
521 | |||
522 | /** |
||
523 | * Sets reference to the request submitted by client. |
||
524 | * |
||
525 | * @param RequestDescription $request |
||
526 | */ |
||
527 | public function setRequest(RequestDescription $request) |
||
532 | |||
533 | /** |
||
534 | * Gets the data service instance. |
||
535 | * |
||
536 | * @return IService |
||
537 | */ |
||
538 | public function getService() |
||
542 | |||
543 | /** |
||
544 | * Gets the segment stack instance. |
||
545 | * |
||
546 | * @return SegmentStack |
||
547 | */ |
||
548 | public function getStack() |
||
552 | |||
553 | /** |
||
554 | * Get update timestamp. |
||
555 | * |
||
556 | * @return Carbon |
||
557 | */ |
||
558 | public function getUpdated() |
||
562 | |||
563 | protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
||
590 | |||
591 | /** |
||
592 | * @param $entryObject |
||
593 | * @param $type |
||
594 | * @param $relativeUri |
||
595 | * @param $resourceType |
||
596 | * @return array<ODataMediaLink|null|array> |
||
597 | */ |
||
598 | protected function writeMediaData($entryObject, $type, $relativeUri, ResourceType $resourceType) |
||
636 | |||
637 | /** |
||
638 | * Gets collection of projection nodes under the current node. |
||
639 | * |
||
640 | * @return ProjectionNode[]|ExpandedProjectionNode[]|null List of nodes describing projections for the current |
||
641 | * segment, If this method returns null it means no |
||
642 | * projections are to be applied and the entire resource for |
||
643 | * the current segment should be serialized, If it returns |
||
644 | * non-null only the properties described by the returned |
||
645 | * projection segments should be serialized |
||
646 | */ |
||
647 | protected function getProjectionNodes() |
||
656 | |||
657 | /** |
||
658 | * Find a 'ExpandedProjectionNode' instance in the projection tree |
||
659 | * which describes the current segment. |
||
660 | * |
||
661 | * @return null|RootProjectionNode|ExpandedProjectionNode |
||
662 | */ |
||
663 | protected function getCurrentExpandedProjectionNode() |
||
664 | { |
||
665 | if (null === $this->rootNode) { |
||
666 | $this->rootNode = $this->getRequest()->getRootProjectionNode(); |
||
667 | } |
||
668 | $expandedProjectionNode = $this->rootNode; |
||
669 | if (null === $expandedProjectionNode) { |
||
670 | return null; |
||
671 | } else { |
||
672 | $segmentNames = $this->getLightStack(); |
||
673 | $depth = count($segmentNames); |
||
674 | // $depth == 1 means serialization of root entry |
||
675 | //(the resource identified by resource path) is going on, |
||
676 | //so control won't get into the below for loop. |
||
677 | //we will directly return the root node, |
||
678 | //which is 'ExpandedProjectionNode' |
||
679 | // for resource identified by resource path. |
||
680 | if (0 != $depth) { |
||
681 | for ($i = 1; $i < $depth; ++$i) { |
||
682 | $expandedProjectionNode = $expandedProjectionNode->findNode($segmentNames[$i]['prop']); |
||
683 | assert(null !== $expandedProjectionNode, 'is_null($expandedProjectionNode)'); |
||
684 | assert( |
||
685 | $expandedProjectionNode instanceof ExpandedProjectionNode, |
||
686 | '$expandedProjectionNode not instanceof ExpandedProjectionNode' |
||
687 | ); |
||
688 | } |
||
689 | } |
||
690 | } |
||
691 | |||
692 | return $expandedProjectionNode; |
||
693 | } |
||
694 | |||
695 | /** |
||
696 | * Check whether to expand a navigation property or not. |
||
697 | * |
||
698 | * @param string $navigationPropertyName Name of naviagtion property in question |
||
699 | * |
||
700 | * @return bool True if the given navigation should be expanded, otherwise false |
||
701 | */ |
||
702 | protected function shouldExpandSegment($navigationPropertyName) |
||
713 | |||
714 | /** |
||
715 | * Wheter next link is needed for the current resource set (feed) |
||
716 | * being serialized. |
||
717 | * |
||
718 | * @param int $resultSetCount Number of entries in the current |
||
719 | * resource set |
||
720 | * |
||
721 | * @return bool true if the feed must have a next page link |
||
722 | */ |
||
723 | protected function needNextPageLink($resultSetCount) |
||
738 | |||
739 | /** |
||
740 | * Resource set wrapper for the resource being serialized. |
||
741 | * |
||
742 | * @return ResourceSetWrapper |
||
743 | */ |
||
744 | protected function getCurrentResourceSetWrapper() |
||
751 | |||
752 | /** |
||
753 | * Get next page link from the given entity instance. |
||
754 | * |
||
755 | * @param mixed &$lastObject Last object serialized to be |
||
756 | * used for generating |
||
757 | * $skiptoken |
||
758 | * @throws ODataException |
||
759 | * @return string for the link for next page |
||
760 | */ |
||
761 | protected function getNextLinkUri(&$lastObject) |
||
778 | |||
779 | /** |
||
780 | * Builds the string corresponding to query parameters for top level results |
||
781 | * (result set identified by the resource path) to be put in next page link. |
||
782 | * |
||
783 | * @return string|null string representing the query parameters in the URI |
||
784 | * query parameter format, NULL if there |
||
785 | * is no query parameters |
||
786 | * required for the next link of top level result set |
||
787 | */ |
||
788 | protected function getNextPageLinkQueryParametersForRootResourceSet() |
||
789 | { |
||
790 | $queryParameterString = null; |
||
791 | foreach ([ODataConstants::HTTPQUERY_STRING_FILTER, |
||
792 | ODataConstants::HTTPQUERY_STRING_EXPAND, |
||
793 | ODataConstants::HTTPQUERY_STRING_ORDERBY, |
||
794 | ODataConstants::HTTPQUERY_STRING_INLINECOUNT, |
||
795 | ODataConstants::HTTPQUERY_STRING_SELECT, ] as $queryOption) { |
||
796 | $value = $this->getService()->getHost()->getQueryStringItem($queryOption); |
||
797 | if (null !== $value) { |
||
798 | if (null !== $queryParameterString) { |
||
799 | $queryParameterString = $queryParameterString . '&'; |
||
800 | } |
||
801 | |||
802 | $queryParameterString .= $queryOption . '=' . $value; |
||
803 | } |
||
804 | } |
||
805 | |||
806 | $topCountValue = $this->getRequest()->getTopOptionCount(); |
||
807 | if (null !== $topCountValue) { |
||
808 | $remainingCount = $topCountValue-$this->getRequest()->getTopCount(); |
||
809 | if (0 < $remainingCount) { |
||
810 | if (null !== $queryParameterString) { |
||
811 | $queryParameterString .= '&'; |
||
812 | } |
||
813 | |||
814 | $queryParameterString .= ODataConstants::HTTPQUERY_STRING_TOP . '=' . $remainingCount; |
||
815 | } |
||
816 | } |
||
817 | |||
818 | if (null !== $queryParameterString) { |
||
819 | $queryParameterString .= '&'; |
||
820 | } |
||
821 | |||
822 | return $queryParameterString; |
||
823 | } |
||
824 | |||
825 | private function loadStackIfEmpty() |
||
832 | |||
833 | /** |
||
834 | * Convert the given primitive value to string. |
||
835 | * Note: This method will not handle null primitive value. |
||
836 | * |
||
837 | * @param IType &$primitiveResourceType Type of the primitive property |
||
838 | * whose value need to be converted |
||
839 | * @param mixed $primitiveValue Primitive value to convert |
||
840 | * |
||
841 | * @return string |
||
842 | */ |
||
843 | private function primitiveToString(IType & $type, $primitiveValue) |
||
866 | |||
867 | /** |
||
868 | * @param $entryObject |
||
869 | * @param $nonRelProp |
||
870 | * @return ODataPropertyContent |
||
871 | */ |
||
872 | private function writePrimitiveProperties(Model $entryObject, $nonRelProp) |
||
890 | |||
891 | /** |
||
892 | * @param $entryObject |
||
893 | * @param $prop |
||
894 | * @param $nuLink |
||
895 | * @param $propKind |
||
896 | * @param $propName |
||
897 | */ |
||
898 | private function expandNavigationProperty(QueryResult $entryObject, $prop, $nuLink, $propKind, $propName) |
||
935 | |||
936 | /** |
||
937 | * Gets the data service instance. |
||
938 | * |
||
939 | * @return void |
||
940 | */ |
||
941 | public function setService(IService $service) |
||
947 | |||
948 | /** |
||
949 | * @param ResourceType $resourceType |
||
950 | * @param $result |
||
951 | * @return ODataBagContent|null |
||
952 | */ |
||
953 | protected function writeBagValue(ResourceType & $resourceType, $result) |
||
979 | |||
980 | /** |
||
981 | * @param ResourceType $resourceType |
||
982 | * @param object $result |
||
983 | * @param string|null $propertyName |
||
984 | * @return ODataPropertyContent |
||
985 | */ |
||
986 | protected function writeComplexValue(ResourceType & $resourceType, &$result, $propertyName = null) |
||
1031 | |||
1032 | public static function isMatchPrimitive($resourceKind) |
||
1042 | |||
1043 | /* |
||
1044 | * @return IMetadataProvider |
||
1045 | */ |
||
1046 | protected function getMetadata() |
||
1053 | |||
1054 | /** |
||
1055 | * @return array |
||
1056 | */ |
||
1057 | protected function getLightStack() |
||
1061 | |||
1062 | /** |
||
1063 | * @return ModelSerialiser |
||
1064 | */ |
||
1065 | public function getModelSerialiser() |
||
1069 | } |
||
1070 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.