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 |
||
41 | class IronicSerialiser implements IObjectSerialiser |
||
42 | { |
||
43 | /** |
||
44 | * The service implementation. |
||
45 | * |
||
46 | * @var IService |
||
47 | */ |
||
48 | protected $service; |
||
49 | |||
50 | /** |
||
51 | * Request description instance describes OData request the |
||
52 | * the client has submitted and result of the request. |
||
53 | * |
||
54 | * @var RequestDescription |
||
55 | */ |
||
56 | protected $request; |
||
57 | |||
58 | /** |
||
59 | * Collection of complex type instances used for cycle detection. |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $complexTypeInstanceCollection; |
||
64 | |||
65 | /** |
||
66 | * Absolute service Uri. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $absoluteServiceUri; |
||
71 | |||
72 | /** |
||
73 | * Absolute service Uri with slash. |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $absoluteServiceUriWithSlash; |
||
78 | |||
79 | /** |
||
80 | * Holds reference to segment stack being processed. |
||
81 | * |
||
82 | * @var SegmentStack |
||
83 | */ |
||
84 | protected $stack; |
||
85 | |||
86 | /** |
||
87 | * Lightweight stack tracking for recursive descent fill |
||
88 | */ |
||
89 | private $lightStack = []; |
||
90 | |||
91 | private $modelSerialiser; |
||
92 | |||
93 | /** |
||
94 | * @param IService $service Reference to the data service instance |
||
95 | * @param RequestDescription $request Type instance describing the client submitted request |
||
96 | */ |
||
97 | public function __construct(IService $service, RequestDescription $request = null) |
||
98 | { |
||
99 | $this->service = $service; |
||
100 | $this->request = $request; |
||
101 | $this->absoluteServiceUri = $service->getHost()->getAbsoluteServiceUri()->getUrlAsString(); |
||
102 | $this->absoluteServiceUriWithSlash = rtrim($this->absoluteServiceUri, '/') . '/'; |
||
103 | $this->stack = new SegmentStack($request); |
||
104 | $this->complexTypeInstanceCollection = []; |
||
105 | $this->modelSerialiser = new ModelSerialiser(); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Write a top level entry resource. |
||
110 | * |
||
111 | * @param mixed $entryObject Reference to the entry object to be written |
||
112 | * |
||
113 | * @return ODataEntry |
||
114 | */ |
||
115 | public function writeTopLevelElement(QueryResult $entryObject) |
||
116 | { |
||
117 | if (!isset($entryObject->results)) { |
||
118 | array_pop($this->lightStack); |
||
119 | return null; |
||
120 | } |
||
121 | |||
122 | $this->loadStackIfEmpty(); |
||
123 | |||
124 | $stackCount = count($this->lightStack); |
||
125 | $topOfStack = $this->lightStack[$stackCount-1]; |
||
126 | $resourceType = $this->getService()->getProvidersWrapper()->resolveResourceType($topOfStack[0]); |
||
127 | $rawProp = $resourceType->getAllProperties(); |
||
128 | $relProp = []; |
||
129 | $nonRelProp = []; |
||
130 | foreach ($rawProp as $prop) { |
||
131 | if ($prop->getResourceType() instanceof ResourceEntityType) { |
||
132 | $relProp[] = $prop; |
||
133 | } else { |
||
134 | $nonRelProp[$prop->getName()] = $prop; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | $resourceSet = $resourceType->getCustomState(); |
||
139 | assert($resourceSet instanceof ResourceSet); |
||
140 | $title = $resourceType->getName(); |
||
141 | $type = $resourceType->getFullName(); |
||
142 | |||
143 | $relativeUri = $this->getEntryInstanceKey( |
||
144 | $entryObject->results, |
||
145 | $resourceType, |
||
146 | $resourceSet->getName() |
||
147 | ); |
||
148 | $absoluteUri = rtrim($this->absoluteServiceUri, '/') . '/' . $relativeUri; |
||
149 | |||
150 | list($mediaLink, $mediaLinks) = $this->writeMediaData($entryObject->results, $type, $relativeUri, $resourceType); |
||
151 | |||
152 | $propertyContent = $this->writePrimitiveProperties($entryObject->results, $nonRelProp); |
||
153 | |||
154 | $links = []; |
||
155 | foreach ($relProp as $prop) { |
||
156 | $nuLink = new ODataLink(); |
||
157 | $propKind = $prop->getKind(); |
||
158 | |||
159 | assert( |
||
160 | ResourcePropertyKind::RESOURCESET_REFERENCE == $propKind |
||
161 | || ResourcePropertyKind::RESOURCE_REFERENCE == $propKind, |
||
162 | '$propKind != ResourcePropertyKind::RESOURCESET_REFERENCE &&' |
||
163 | .' $propKind != ResourcePropertyKind::RESOURCE_REFERENCE' |
||
164 | ); |
||
165 | $propTail = ResourcePropertyKind::RESOURCE_REFERENCE == $propKind ? 'entry' : 'feed'; |
||
166 | $propType = 'application/atom+xml;type='.$propTail; |
||
167 | $propName = $prop->getName(); |
||
168 | $nuLink->title = $propName; |
||
169 | $nuLink->name = ODataConstants::ODATA_RELATED_NAMESPACE . $propName; |
||
170 | $nuLink->url = $relativeUri . '/' . $propName; |
||
171 | $nuLink->type = $propType; |
||
172 | |||
173 | $navProp = new ODataNavigationPropertyInfo($prop, $this->shouldExpandSegment($propName)); |
||
174 | if ($navProp->expanded) { |
||
175 | $this->expandNavigationProperty($entryObject, $prop, $nuLink, $propKind, $propName); |
||
176 | } |
||
177 | |||
178 | $links[] = $nuLink; |
||
179 | } |
||
180 | |||
181 | $odata = new ODataEntry(); |
||
182 | $odata->resourceSetName = $resourceSet->getName(); |
||
183 | $odata->id = $absoluteUri; |
||
184 | $odata->title = $title; |
||
185 | $odata->type = $type; |
||
186 | $odata->propertyContent = $propertyContent; |
||
187 | $odata->isMediaLinkEntry = $resourceType->isMediaLinkEntry(); |
||
188 | $odata->editLink = $relativeUri; |
||
189 | $odata->mediaLink = $mediaLink; |
||
190 | $odata->mediaLinks = $mediaLinks; |
||
191 | $odata->links = $links; |
||
192 | |||
193 | $newCount = count($this->lightStack); |
||
194 | assert($newCount == $stackCount, "Should have $stackCount elements in stack, have $newCount elements"); |
||
195 | array_pop($this->lightStack); |
||
196 | return $odata; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Write top level feed element. |
||
201 | * |
||
202 | * @param array &$entryObjects Array of entry resources to be written |
||
203 | * |
||
204 | * @return ODataFeed |
||
205 | */ |
||
206 | public function writeTopLevelElements(QueryResult &$entryObjects) |
||
207 | { |
||
208 | assert(is_array($entryObjects->results), '!is_array($entryObjects->results)'); |
||
209 | |||
210 | $this->loadStackIfEmpty(); |
||
211 | $setName = $this->getRequest()->getTargetResourceSetWrapper()->getName(); |
||
212 | |||
213 | $title = $this->getRequest()->getContainerName(); |
||
214 | $relativeUri = $this->getRequest()->getIdentifier(); |
||
215 | $absoluteUri = $this->getRequest()->getRequestUrl()->getUrlAsString(); |
||
216 | |||
217 | $selfLink = new ODataLink(); |
||
218 | $selfLink->name = 'self'; |
||
219 | $selfLink->title = $relativeUri; |
||
220 | $selfLink->url = $relativeUri; |
||
221 | |||
222 | $odata = new ODataFeed(); |
||
223 | $odata->title = $title; |
||
224 | $odata->id = $absoluteUri; |
||
225 | $odata->selfLink = $selfLink; |
||
226 | |||
227 | if ($this->getRequest()->queryType == QueryType::ENTITIES_WITH_COUNT()) { |
||
228 | $odata->rowCount = $this->getRequest()->getCountValue(); |
||
229 | } |
||
230 | foreach ($entryObjects->results as $entry) { |
||
231 | if (!$entry instanceof QueryResult) { |
||
232 | $query = new QueryResult(); |
||
233 | $query->results = $entry; |
||
234 | } else { |
||
235 | $query = $entry; |
||
236 | } |
||
237 | $odata->entries[] = $this->writeTopLevelElement($query); |
||
238 | } |
||
239 | |||
240 | $resourceSet = $this->getRequest()->getTargetResourceSetWrapper()->getResourceSet(); |
||
241 | $requestTop = $this->getRequest()->getTopOptionCount(); |
||
242 | $pageSize = $this->getService()->getConfiguration()->getEntitySetPageSize($resourceSet); |
||
243 | $requestTop = (null == $requestTop) ? $pageSize + 1 : $requestTop; |
||
|
|||
244 | |||
245 | if (true === $entryObjects->hasMore && $requestTop > $pageSize) { |
||
246 | $stackSegment = $setName; |
||
247 | $lastObject = end($entryObjects->results); |
||
248 | $segment = $this->getNextLinkUri($lastObject, $absoluteUri); |
||
249 | $nextLink = new ODataLink(); |
||
250 | $nextLink->name = ODataConstants::ATOM_LINK_NEXT_ATTRIBUTE_STRING; |
||
251 | $nextLink->url = rtrim($this->absoluteServiceUri, '/') . '/' . $stackSegment . $segment; |
||
252 | $odata->nextPageLink = $nextLink; |
||
253 | } |
||
254 | |||
255 | return $odata; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Write top level url element. |
||
260 | * |
||
261 | * @param mixed $entryObject The entry resource whose url to be written |
||
262 | * |
||
263 | * @return ODataURL |
||
264 | */ |
||
265 | public function writeUrlElement(QueryResult $entryObject) |
||
266 | { |
||
267 | $url = new ODataURL(); |
||
268 | if (!is_null($entryObject->results)) { |
||
269 | $currentResourceType = $this->getCurrentResourceSetWrapper()->getResourceType(); |
||
270 | $relativeUri = $this->getEntryInstanceKey( |
||
271 | $entryObject->results, |
||
272 | $currentResourceType, |
||
273 | $this->getCurrentResourceSetWrapper()->getName() |
||
274 | ); |
||
275 | |||
276 | $url->url = rtrim($this->absoluteServiceUri, '/') . '/' . $relativeUri; |
||
277 | } |
||
278 | |||
279 | return $url; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Write top level url collection. |
||
284 | * |
||
285 | * @param array $entryObjects Array of entry resources |
||
286 | * whose url to be written |
||
287 | * |
||
288 | * @return ODataURLCollection |
||
289 | */ |
||
290 | public function writeUrlElements(QueryResult $entryObjects) |
||
291 | { |
||
292 | $urls = new ODataURLCollection(); |
||
293 | if (!empty($entryObjects->results)) { |
||
294 | $i = 0; |
||
295 | foreach ($entryObjects->results as $entryObject) { |
||
296 | $urls->urls[$i] = $this->writeUrlElement($entryObject); |
||
297 | ++$i; |
||
298 | } |
||
299 | |||
300 | if ($i > 0 && true === $entryObjects->hasMore) { |
||
301 | $stackSegment = $this->getRequest()->getTargetResourceSetWrapper()->getName(); |
||
302 | $lastObject = end($entryObjects->results); |
||
303 | $segment = $this->getNextLinkUri($lastObject, $this->getRequest()->getRequestUrl()->getUrlAsString()); |
||
304 | $nextLink = new ODataLink(); |
||
305 | $nextLink->name = ODataConstants::ATOM_LINK_NEXT_ATTRIBUTE_STRING; |
||
306 | $nextLink->url = rtrim($this->absoluteServiceUri, '/') . '/' . $stackSegment . $segment; |
||
307 | $urls->nextPageLink = $nextLink; |
||
308 | } |
||
309 | } |
||
310 | |||
311 | if ($this->getRequest()->queryType == QueryType::ENTITIES_WITH_COUNT()) { |
||
312 | $urls->count = $this->getRequest()->getCountValue(); |
||
313 | } |
||
314 | |||
315 | return $urls; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Write top level complex resource. |
||
320 | * |
||
321 | * @param mixed &$complexValue The complex object to be |
||
322 | * written |
||
323 | * @param string $propertyName The name of the |
||
324 | * complex property |
||
325 | * @param ResourceType &$resourceType Describes the type of |
||
326 | * complex object |
||
327 | * |
||
328 | * @return ODataPropertyContent |
||
329 | */ |
||
330 | public function writeTopLevelComplexObject(QueryResult &$complexValue, $propertyName, ResourceType &$resourceType) |
||
331 | { |
||
332 | $result = $complexValue->results; |
||
333 | |||
334 | $propertyContent = new ODataPropertyContent(); |
||
335 | $odataProperty = new ODataProperty(); |
||
336 | $odataProperty->name = $propertyName; |
||
337 | $odataProperty->typeName = $resourceType->getFullName(); |
||
338 | if (null != $result) { |
||
339 | $internalContent = $this->writeComplexValue($resourceType, $result); |
||
340 | $odataProperty->value = $internalContent; |
||
341 | } |
||
342 | |||
343 | $propertyContent->properties[] = $odataProperty; |
||
344 | |||
345 | return $propertyContent; |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Write top level bag resource. |
||
350 | * |
||
351 | * @param mixed &$BagValue The bag object to be |
||
352 | * written |
||
353 | * @param string $propertyName The name of the |
||
354 | * bag property |
||
355 | * @param ResourceType &$resourceType Describes the type of |
||
356 | * bag object |
||
357 | * @return ODataPropertyContent |
||
358 | */ |
||
359 | public function writeTopLevelBagObject(QueryResult &$BagValue, $propertyName, ResourceType &$resourceType) |
||
372 | |||
373 | /** |
||
374 | * Write top level primitive value. |
||
375 | * |
||
376 | * @param mixed &$primitiveValue The primitive value to be |
||
377 | * written |
||
378 | * @param ResourceProperty &$resourceProperty Resource property describing the |
||
379 | * primitive property to be written |
||
380 | * @return ODataPropertyContent |
||
381 | */ |
||
382 | public function writeTopLevelPrimitive(QueryResult &$primitiveValue, ResourceProperty &$resourceProperty = null) |
||
383 | { |
||
384 | assert(null != $resourceProperty, "Resource property must not be null"); |
||
385 | $propertyContent = new ODataPropertyContent(); |
||
386 | |||
387 | $odataProperty = new ODataProperty(); |
||
388 | $odataProperty->name = $resourceProperty->getName(); |
||
389 | $iType = $resourceProperty->getInstanceType(); |
||
390 | assert($iType instanceof IType, get_class($iType)); |
||
391 | $odataProperty->typeName = $iType->getFullTypeName(); |
||
392 | if (null == $primitiveValue->results) { |
||
393 | $odataProperty->value = null; |
||
394 | } else { |
||
395 | $rType = $resourceProperty->getResourceType()->getInstanceType(); |
||
396 | $odataProperty->value = $this->primitiveToString($rType, $primitiveValue->results); |
||
397 | } |
||
398 | |||
399 | $propertyContent->properties[] = $odataProperty; |
||
400 | |||
401 | return $propertyContent; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Gets reference to the request submitted by client. |
||
406 | * |
||
407 | * @return RequestDescription |
||
408 | */ |
||
409 | public function getRequest() |
||
415 | |||
416 | /** |
||
417 | * Sets reference to the request submitted by client. |
||
418 | * |
||
419 | * @param RequestDescription $request |
||
420 | */ |
||
421 | public function setRequest(RequestDescription $request) |
||
426 | |||
427 | /** |
||
428 | * Gets the data service instance. |
||
429 | * |
||
430 | * @return IService |
||
431 | */ |
||
432 | public function getService() |
||
436 | |||
437 | /** |
||
438 | * Gets the segment stack instance. |
||
439 | * |
||
440 | * @return SegmentStack |
||
441 | */ |
||
442 | public function getStack() |
||
446 | |||
447 | protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
||
474 | |||
475 | /** |
||
476 | * @param $entryObject |
||
477 | * @param $type |
||
478 | * @param $relativeUri |
||
479 | * @param $resourceType |
||
480 | * @return array |
||
481 | */ |
||
482 | protected function writeMediaData($entryObject, $type, $relativeUri, ResourceType $resourceType) |
||
520 | |||
521 | /** |
||
522 | * Gets collection of projection nodes under the current node. |
||
523 | * |
||
524 | * @return ProjectionNode[]|ExpandedProjectionNode[]|null List of nodes |
||
525 | * describing projections for the current segment, If this method returns |
||
526 | * null it means no projections are to be applied and the entire resource |
||
527 | * for the current segment should be serialized, If it returns non-null |
||
528 | * only the properties described by the returned projection segments should |
||
529 | * be serialized |
||
530 | */ |
||
531 | protected function getProjectionNodes() |
||
540 | |||
541 | /** |
||
542 | * Find a 'ExpandedProjectionNode' instance in the projection tree |
||
543 | * which describes the current segment. |
||
544 | * |
||
545 | * @return ExpandedProjectionNode|null |
||
546 | */ |
||
547 | protected function getCurrentExpandedProjectionNode() |
||
575 | |||
576 | /** |
||
577 | * Check whether to expand a navigation property or not. |
||
578 | * |
||
579 | * @param string $navigationPropertyName Name of naviagtion property in question |
||
580 | * |
||
581 | * @return bool True if the given navigation should be |
||
582 | * explanded otherwise false |
||
583 | */ |
||
584 | protected function shouldExpandSegment($navigationPropertyName) |
||
596 | |||
597 | /** |
||
598 | * Wheter next link is needed for the current resource set (feed) |
||
599 | * being serialized. |
||
600 | * |
||
601 | * @param int $resultSetCount Number of entries in the current |
||
602 | * resource set |
||
603 | * |
||
604 | * @return bool true if the feed must have a next page link |
||
605 | */ |
||
606 | protected function needNextPageLink($resultSetCount) |
||
621 | |||
622 | /** |
||
623 | * Resource set wrapper for the resource being serialized. |
||
624 | * |
||
625 | * @return ResourceSetWrapper |
||
626 | */ |
||
627 | protected function getCurrentResourceSetWrapper() |
||
634 | |||
635 | /** |
||
636 | * Get next page link from the given entity instance. |
||
637 | * |
||
638 | * @param mixed &$lastObject Last object serialized to be |
||
639 | * used for generating $skiptoken |
||
640 | * @param string $absoluteUri Absolute response URI |
||
641 | * |
||
642 | * @return string for the link for next page |
||
643 | */ |
||
644 | protected function getNextLinkUri(&$lastObject, $absoluteUri) |
||
661 | |||
662 | /** |
||
663 | * Builds the string corresponding to query parameters for top level results |
||
664 | * (result set identified by the resource path) to be put in next page link. |
||
665 | * |
||
666 | * @return string|null string representing the query parameters in the URI |
||
667 | * query parameter format, NULL if there |
||
668 | * is no query parameters |
||
669 | * required for the next link of top level result set |
||
670 | */ |
||
671 | protected function getNextPageLinkQueryParametersForRootResourceSet() |
||
707 | |||
708 | private function loadStackIfEmpty() |
||
715 | |||
716 | /** |
||
717 | * Convert the given primitive value to string. |
||
718 | * Note: This method will not handle null primitive value. |
||
719 | * |
||
720 | * @param IType &$primitiveResourceType Type of the primitive property |
||
721 | * whose value need to be converted |
||
722 | * @param mixed $primitiveValue Primitive value to convert |
||
723 | * |
||
724 | * @return string |
||
725 | */ |
||
726 | private function primitiveToString(IType &$type, $primitiveValue) |
||
742 | |||
743 | /** |
||
744 | * @param $entryObject |
||
745 | * @param $nonRelProp |
||
746 | * @return ODataPropertyContent |
||
747 | */ |
||
748 | private function writePrimitiveProperties($entryObject, $nonRelProp) |
||
766 | |||
767 | /** |
||
768 | * @param $entryObject |
||
769 | * @param $prop |
||
770 | * @param $nuLink |
||
771 | * @param $propKind |
||
772 | * @param $propName |
||
773 | */ |
||
774 | private function expandNavigationProperty(QueryResult $entryObject, $prop, $nuLink, $propKind, $propName) |
||
802 | |||
803 | /** |
||
804 | * Gets the data service instance. |
||
805 | * |
||
806 | * @return IService |
||
807 | */ |
||
808 | public function setService(IService $service) |
||
814 | |||
815 | /** |
||
816 | * @param ResourceType $resourceType |
||
817 | * @param $result |
||
818 | * @return ODataBagContent |
||
819 | */ |
||
820 | protected function writeBagValue(ResourceType &$resourceType, $result) |
||
821 | { |
||
846 | |||
847 | /** |
||
848 | * @param ResourceType $resourceType |
||
849 | * @param object $result |
||
850 | * @param string $propertyName |
||
851 | * @return ODataPropertyContent |
||
852 | */ |
||
853 | protected function writeComplexValue(ResourceType &$resourceType, &$result, $propertyName = null) |
||
897 | |||
898 | public static function isMatchPrimitive($resourceKind) |
||
908 | } |
||
909 |