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) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Gets reference to the request submitted by client. |
||
| 404 | * |
||
| 405 | * @return RequestDescription |
||
| 406 | */ |
||
| 407 | public function getRequest() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Sets reference to the request submitted by client. |
||
| 416 | * |
||
| 417 | * @param RequestDescription $request |
||
| 418 | */ |
||
| 419 | public function setRequest(RequestDescription $request) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Gets the data service instance. |
||
| 427 | * |
||
| 428 | * @return IService |
||
| 429 | */ |
||
| 430 | public function getService() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Gets the segment stack instance. |
||
| 437 | * |
||
| 438 | * @return SegmentStack |
||
| 439 | */ |
||
| 440 | public function getStack() |
||
| 444 | |||
| 445 | protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param $entryObject |
||
| 475 | * @param $type |
||
| 476 | * @param $relativeUri |
||
| 477 | * @param $resourceType |
||
| 478 | * @return array |
||
| 479 | */ |
||
| 480 | protected function writeMediaData($entryObject, $type, $relativeUri, ResourceType $resourceType) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Gets collection of projection nodes under the current node. |
||
| 521 | * |
||
| 522 | * @return ProjectionNode[]|ExpandedProjectionNode[]|null List of nodes |
||
| 523 | * describing projections for the current segment, If this method returns |
||
| 524 | * null it means no projections are to be applied and the entire resource |
||
| 525 | * for the current segment should be serialized, If it returns non-null |
||
| 526 | * only the properties described by the returned projection segments should |
||
| 527 | * be serialized |
||
| 528 | */ |
||
| 529 | protected function getProjectionNodes() |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Find a 'ExpandedProjectionNode' instance in the projection tree |
||
| 541 | * which describes the current segment. |
||
| 542 | * |
||
| 543 | * @return ExpandedProjectionNode|null |
||
| 544 | */ |
||
| 545 | protected function getCurrentExpandedProjectionNode() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Check whether to expand a navigation property or not. |
||
| 576 | * |
||
| 577 | * @param string $navigationPropertyName Name of naviagtion property in question |
||
| 578 | * |
||
| 579 | * @return bool True if the given navigation should be |
||
| 580 | * explanded otherwise false |
||
| 581 | */ |
||
| 582 | protected function shouldExpandSegment($navigationPropertyName) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Wheter next link is needed for the current resource set (feed) |
||
| 597 | * being serialized. |
||
| 598 | * |
||
| 599 | * @param int $resultSetCount Number of entries in the current |
||
| 600 | * resource set |
||
| 601 | * |
||
| 602 | * @return bool true if the feed must have a next page link |
||
| 603 | */ |
||
| 604 | protected function needNextPageLink($resultSetCount) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Resource set wrapper for the resource being serialized. |
||
| 622 | * |
||
| 623 | * @return ResourceSetWrapper |
||
| 624 | */ |
||
| 625 | protected function getCurrentResourceSetWrapper() |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Get next page link from the given entity instance. |
||
| 635 | * |
||
| 636 | * @param mixed &$lastObject Last object serialized to be |
||
| 637 | * used for generating $skiptoken |
||
| 638 | * @param string $absoluteUri Absolute response URI |
||
| 639 | * |
||
| 640 | * @return string for the link for next page |
||
| 641 | */ |
||
| 642 | protected function getNextLinkUri(&$lastObject, $absoluteUri) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Builds the string corresponding to query parameters for top level results |
||
| 662 | * (result set identified by the resource path) to be put in next page link. |
||
| 663 | * |
||
| 664 | * @return string|null string representing the query parameters in the URI |
||
| 665 | * query parameter format, NULL if there |
||
| 666 | * is no query parameters |
||
| 667 | * required for the next link of top level result set |
||
| 668 | */ |
||
| 669 | protected function getNextPageLinkQueryParametersForRootResourceSet() |
||
| 705 | |||
| 706 | private function loadStackIfEmpty() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Convert the given primitive value to string. |
||
| 716 | * Note: This method will not handle null primitive value. |
||
| 717 | * |
||
| 718 | * @param IType &$primitiveResourceType Type of the primitive property |
||
| 719 | * whose value need to be converted |
||
| 720 | * @param mixed $primitiveValue Primitive value to convert |
||
| 721 | * |
||
| 722 | * @return string |
||
| 723 | */ |
||
| 724 | private function primitiveToString(IType &$type, $primitiveValue) |
||
| 740 | |||
| 741 | /** |
||
| 742 | * @param $entryObject |
||
| 743 | * @param $nonRelProp |
||
| 744 | * @return ODataPropertyContent |
||
| 745 | */ |
||
| 746 | private function writePrimitiveProperties($entryObject, $nonRelProp) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * @param $entryObject |
||
| 767 | * @param $prop |
||
| 768 | * @param $nuLink |
||
| 769 | * @param $propKind |
||
| 770 | * @param $propName |
||
| 771 | */ |
||
| 772 | private function expandNavigationProperty(QueryResult $entryObject, $prop, $nuLink, $propKind, $propName) |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Gets the data service instance. |
||
| 803 | * |
||
| 804 | * @return IService |
||
| 805 | */ |
||
| 806 | public function setService(IService $service) |
||
| 812 | |||
| 813 | /** |
||
| 814 | * @param ResourceType $resourceType |
||
| 815 | * @param $result |
||
| 816 | * @return ODataBagContent |
||
| 817 | */ |
||
| 818 | protected function writeBagValue(ResourceType &$resourceType, $result) |
||
| 843 | |||
| 844 | /** |
||
| 845 | * @param ResourceType $resourceType |
||
| 846 | * @param object $result |
||
| 847 | * @param string $propertyName |
||
| 848 | * @return ODataPropertyContent |
||
| 849 | */ |
||
| 850 | protected function writeComplexValue(ResourceType &$resourceType, &$result, $propertyName = null) |
||
| 893 | |||
| 894 | public static function isMatchPrimitive($resourceKind) |
||
| 904 | } |
||
| 905 |