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 ObjectModelSerializer 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 ObjectModelSerializer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class ObjectModelSerializer extends ObjectModelSerializerBase |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Creates new instance of ObjectModelSerializer. |
||
| 29 | * |
||
| 30 | * @param IService $service |
||
| 31 | * @param RequestDescription $request the request submitted by the client |
||
| 32 | */ |
||
| 33 | public function __construct(IService $service, RequestDescription $request) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Write a top level entry resource. |
||
| 40 | * |
||
| 41 | * @param mixed $entryObject Reference to the entry object to be written |
||
| 42 | * |
||
| 43 | * @return ODataEntry |
||
| 44 | */ |
||
| 45 | public function writeTopLevelElement($entryObject) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Write top level feed element. |
||
| 72 | * |
||
| 73 | * @param array &$entryObjects Array of entry resources to be written |
||
| 74 | * |
||
| 75 | * @return ODataFeed |
||
| 76 | */ |
||
| 77 | public function writeTopLevelElements(&$entryObjects) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Write top level url element. |
||
| 119 | * |
||
| 120 | * @param mixed $entryObject The entry resource whose url to be written |
||
| 121 | * |
||
| 122 | * @return ODataURL |
||
| 123 | */ |
||
| 124 | public function writeUrlElement($entryObject) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Write top level url collection. |
||
| 143 | * |
||
| 144 | * @param array $entryObjects Array of entry resources |
||
| 145 | * whose url to be written |
||
| 146 | * |
||
| 147 | * @return ODataURLCollection |
||
| 148 | */ |
||
| 149 | public function writeUrlElements($entryObjects) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Write top level complex resource. |
||
| 176 | * |
||
| 177 | * @param mixed &$complexValue The complex object to be |
||
| 178 | * written |
||
| 179 | * @param string $propertyName The name of the |
||
| 180 | * complex property |
||
| 181 | * @param ResourceType &$resourceType Describes the type of |
||
| 182 | * complex object |
||
| 183 | * |
||
| 184 | * @return ODataPropertyContent |
||
| 185 | */ |
||
| 186 | public function writeTopLevelComplexObject( |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Write top level bag resource. |
||
| 205 | * |
||
| 206 | * @param mixed &$BagValue The bag object to be |
||
| 207 | * written |
||
| 208 | * @param string $propertyName The name of the |
||
| 209 | * bag property |
||
| 210 | * @param ResourceType &$resourceType Describes the type of |
||
| 211 | * bag object |
||
| 212 | * |
||
| 213 | * @return ODataPropertyContent |
||
| 214 | */ |
||
| 215 | public function writeTopLevelBagObject( |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Write top level primitive value. |
||
| 234 | * |
||
| 235 | * @param mixed &$primitiveValue The primitve value to be |
||
| 236 | * written |
||
| 237 | * @param ResourceProperty &$resourceProperty Resource property |
||
| 238 | * describing the |
||
| 239 | * primitive property |
||
| 240 | * to be written |
||
| 241 | * |
||
| 242 | * @return ODataPropertyContent |
||
| 243 | */ |
||
| 244 | public function writeTopLevelPrimitive( |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Write an entry element. |
||
| 261 | * |
||
| 262 | * @param mixed $entryObject Object representing entry element |
||
| 263 | * @param ResourceType $resourceType Expected type of the entry object |
||
| 264 | * @param string $absoluteUri Absolute uri of the entry element |
||
| 265 | * @param string $relativeUri Relative uri of the entry element |
||
| 266 | * |
||
| 267 | * @return ODataEntry |
||
| 268 | */ |
||
| 269 | private function _writeEntryElement( |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Writes the feed elements. |
||
| 322 | * |
||
| 323 | * @param array &$entryObjects Array of entries in the feed element |
||
| 324 | * @param ResourceType &$resourceType The resource type of the f the elements |
||
| 325 | * in the collection |
||
| 326 | * @param string $title Title of the feed element |
||
| 327 | * @param string $absoluteUri Absolute uri representing the feed element |
||
| 328 | * @param string $relativeUri Relative uri representing the feed element |
||
| 329 | * @param ODataFeed &$feed Feed to write to |
||
| 330 | */ |
||
| 331 | private function _writeFeedElements( |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Write values of properties of given entry (resource) or complex object. |
||
| 364 | * |
||
| 365 | * @param mixed $customObject Entity or complex object |
||
| 366 | * with properties |
||
| 367 | * to write out |
||
| 368 | * @param ResourceType &$resourceType Resource type describing |
||
| 369 | * the metadata of |
||
| 370 | * the custom object |
||
| 371 | * @param string $absoluteUri Absolute uri for the given |
||
| 372 | * entry object |
||
| 373 | * NULL for complex object |
||
| 374 | * @param string $relativeUri Relative uri for the given |
||
| 375 | * custom object |
||
| 376 | * @param ODataEntry ODataEntry|null ODataEntry instance to |
||
| 377 | * place links and |
||
| 378 | * expansion of the |
||
| 379 | * entry object, |
||
| 380 | * NULL for complex object |
||
| 381 | * @param ODataPropertyContent &$odataPropertyContent ODataPropertyContent |
||
| 382 | * instance in which |
||
| 383 | * to place the values |
||
| 384 | */ |
||
| 385 | private function _writeObjectProperties( |
||
| 386 | $customObject, |
||
| 387 | ResourceType & $resourceType, |
||
| 388 | $absoluteUri, |
||
| 389 | $relativeUri, |
||
| 390 | &$odataEntry, |
||
| 391 | ODataPropertyContent & $odataPropertyContent |
||
| 392 | ) { |
||
| 393 | $resourceTypeKind = $resourceType->getResourceTypeKind(); |
||
| 394 | if (is_null($absoluteUri) == ($resourceTypeKind == ResourceTypeKind::ENTITY) |
||
| 395 | ) { |
||
| 396 | throw ODataException::createInternalServerError( |
||
| 397 | Messages::badProviderInconsistentEntityOrComplexTypeUsage( |
||
| 398 | $resourceType->getName() |
||
| 399 | ) |
||
| 400 | ); |
||
| 401 | } |
||
| 402 | |||
| 403 | assert( |
||
| 404 | (($resourceTypeKind == ResourceTypeKind::ENTITY) && ($odataEntry instanceof ODataEntry)) |
||
| 405 | || (($resourceTypeKind == ResourceTypeKind::COMPLEX) && is_null($odataEntry)), |
||
| 406 | '!(($resourceTypeKind == ResourceTypeKind::ENTITY) && ($odataEntry instanceof ODataEntry))' |
||
| 407 | .' && !(($resourceTypeKind == ResourceTypeKind::COMPLEX) && is_null($odataEntry))' |
||
| 408 | ); |
||
| 409 | $projectionNodes = null; |
||
| 410 | $navigationProperties = null; |
||
| 411 | if ($resourceTypeKind == ResourceTypeKind::ENTITY) { |
||
| 412 | $projectionNodes = $this->getProjectionNodes(); |
||
| 413 | $navigationProperties = array(); |
||
| 414 | } |
||
| 415 | |||
| 416 | if (is_null($projectionNodes)) { |
||
| 417 | list($odataPropertyContent, $navigationProperties) = $this->writeObjectPropertiesUnexpanded( |
||
| 418 | $customObject, |
||
| 419 | $resourceType, |
||
| 420 | $relativeUri, |
||
| 421 | $odataPropertyContent, |
||
| 422 | $resourceTypeKind, |
||
| 423 | $navigationProperties |
||
| 424 | ); |
||
| 425 | |||
| 426 | } else { //This is the code path to handle projected properties of Entry |
||
| 427 | list($navigationProperties, $odataPropertyContent) = $this->writeObjectPropertiesExpanded( |
||
| 428 | $customObject, |
||
| 429 | $resourceType, |
||
| 430 | $relativeUri, |
||
| 431 | $odataPropertyContent, |
||
| 432 | $projectionNodes, |
||
| 433 | $navigationProperties |
||
| 434 | ); |
||
| 435 | } |
||
| 436 | |||
| 437 | if (!is_null($navigationProperties)) { |
||
| 438 | //Write out navigation properties (deferred or inline) |
||
| 439 | foreach ($navigationProperties as $navigationPropertyInfo) { |
||
| 440 | $propertyName = $navigationPropertyInfo->resourceProperty->getName(); |
||
| 441 | $type = $navigationPropertyInfo->resourceProperty->getKind() == ResourcePropertyKind::RESOURCE_REFERENCE ? |
||
| 442 | 'application/atom+xml;type=entry' : 'application/atom+xml;type=feed'; |
||
| 443 | $link = new ODataLink(); |
||
| 444 | $link->name = ODataConstants::ODATA_RELATED_NAMESPACE . $propertyName; |
||
| 445 | $link->title = $propertyName; |
||
| 446 | $link->type = $type; |
||
| 447 | $link->url = $relativeUri . '/' . $propertyName; |
||
| 448 | |||
| 449 | if ($navigationPropertyInfo->expanded) { |
||
| 450 | $propertyRelativeUri = $relativeUri . '/' . $propertyName; |
||
| 451 | $propertyAbsoluteUri = trim($absoluteUri, '/') . '/' . $propertyName; |
||
| 452 | $needPop = $this->pushSegmentForNavigationProperty($navigationPropertyInfo->resourceProperty); |
||
| 453 | $navigationPropertyKind = $navigationPropertyInfo->resourceProperty->getKind(); |
||
| 454 | assert( |
||
| 455 | $navigationPropertyKind == ResourcePropertyKind::RESOURCESET_REFERENCE |
||
| 456 | || $navigationPropertyKind == ResourcePropertyKind::RESOURCE_REFERENCE, |
||
| 457 | '$navigationPropertyKind != ResourcePropertyKind::RESOURCESET_REFERENCE |
||
| 458 | && $navigationPropertyKind != ResourcePropertyKind::RESOURCE_REFERENCE' |
||
| 459 | ); |
||
| 460 | $currentResourceSetWrapper = $this->getCurrentResourceSetWrapper(); |
||
| 461 | assert(!is_null($currentResourceSetWrapper), 'is_null($currentResourceSetWrapper)'); |
||
| 462 | $link->isExpanded = true; |
||
| 463 | if (!is_null($navigationPropertyInfo->value)) { |
||
| 464 | $currentResourceType = $currentResourceSetWrapper->getResourceType(); |
||
| 465 | if ($navigationPropertyKind == ResourcePropertyKind::RESOURCESET_REFERENCE) { |
||
| 466 | $inlineFeed = new ODataFeed(); |
||
| 467 | $link->isCollection = true; |
||
| 468 | |||
| 469 | $this->_writeFeedElements( |
||
| 470 | $navigationPropertyInfo->value, |
||
| 471 | $currentResourceType, |
||
| 472 | $propertyName, |
||
| 473 | $propertyAbsoluteUri, |
||
| 474 | $propertyRelativeUri, |
||
| 475 | $inlineFeed |
||
| 476 | ); |
||
| 477 | $link->expandedResult = $inlineFeed; |
||
| 478 | } else { |
||
| 479 | $link->isCollection = false; |
||
| 480 | $link->expandedResult = $this->_writeEntryElement( |
||
| 481 | $navigationPropertyInfo->value, |
||
| 482 | $currentResourceType, |
||
| 483 | $propertyAbsoluteUri, |
||
| 484 | $propertyRelativeUri |
||
| 485 | ); |
||
| 486 | } |
||
| 487 | } else { |
||
| 488 | $link->expandedResult = null; |
||
| 489 | } |
||
| 490 | |||
| 491 | $this->popSegment($needPop); |
||
| 492 | } |
||
| 493 | |||
| 494 | $odataEntry->links[] = $link; |
||
| 495 | } |
||
| 496 | } |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Writes a primitive value and related information to the given |
||
| 501 | * ODataProperty instance. |
||
| 502 | * |
||
| 503 | * @param mixed &$primitiveValue The primitive value to write |
||
| 504 | * @param ResourceProperty &$resourceProperty The metadata of the primitive |
||
| 505 | * property value |
||
| 506 | * @param ODataProperty &$odataProperty ODataProperty instance to which |
||
| 507 | * the primitive value and related |
||
| 508 | * information to write out |
||
| 509 | * |
||
| 510 | * @throws ODataException If given value is not primitive |
||
| 511 | */ |
||
| 512 | private function _writePrimitiveValue( |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Write value of a complex object. |
||
| 534 | * |
||
| 535 | * @param mixed &$complexValue Complex object to write |
||
| 536 | * @param string $propertyName Name of the |
||
| 537 | * complex property |
||
| 538 | * whose value need |
||
| 539 | * to be written |
||
| 540 | * @param ResourceType &$resourceType Expected type |
||
| 541 | * of the property |
||
| 542 | * @param string $relativeUri Relative uri for the |
||
| 543 | * complex type element |
||
| 544 | * @param ODataPropertyContent &$odataPropertyContent Content to write to |
||
| 545 | */ |
||
| 546 | private function _writeComplexValue( |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Write value of a bag instance. |
||
| 577 | * |
||
| 578 | * @param array/NULL &$BagValue Bag value to write |
||
| 579 | * @param string $propertyName Property name of the bag |
||
| 580 | * @param ResourceType &$resourceType Type describing the |
||
| 581 | * bag value |
||
| 582 | * @param string $relativeUri Relative Url to the bag |
||
| 583 | * @param ODataPropertyContent &$odataPropertyContent On return, this object |
||
| 584 | * will hold bag value which |
||
| 585 | * can be used by writers |
||
| 586 | */ |
||
| 587 | private function _writeBagValue( |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Write media resource metadata (for MLE and Named Streams). |
||
| 638 | * |
||
| 639 | * @param mixed $entryObject The entry instance being serialized |
||
| 640 | * @param ResourceType &$resourceType Resource type of the entry instance |
||
| 641 | * @param string $title Title for the current |
||
| 642 | * current entry instance |
||
| 643 | * @param string $relativeUri Relative uri for the |
||
| 644 | * current entry instance |
||
| 645 | * @param ODataEntry &$odataEntry OData entry to write to |
||
| 646 | */ |
||
| 647 | private function _writeMediaResourceMetadata( |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Convert the given primitive value to string. |
||
| 691 | * Note: This method will not handle null primitive value. |
||
| 692 | * |
||
| 693 | * @param ResourceType &$primitiveResourceType Type of the primitive property |
||
| 694 | * whose value need to be converted |
||
| 695 | * @param mixed $primitiveValue Primitive value to convert |
||
| 696 | * @return string |
||
| 697 | |||
| 698 | */ |
||
| 699 | private function _primitiveToString( |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Write value of a complex object. |
||
| 720 | * Note: This method will not handle null complex value. |
||
| 721 | * |
||
| 722 | * @param mixed &$complexValue Complex object to write |
||
| 723 | * @param string $propertyName Name of the |
||
| 724 | * complex property |
||
| 725 | * whose value |
||
| 726 | * need to be written |
||
| 727 | * @param ResourceType &$resourceType Expected type of the |
||
| 728 | * property |
||
| 729 | * @param string $relativeUri Relative uri for the |
||
| 730 | * complex type element |
||
| 731 | * @param ODataPropertyContent &$odataPropertyContent Content to write to |
||
| 732 | * |
||
| 733 | * @return ResourceType The actual type of the complex object |
||
| 734 | */ |
||
| 735 | private function _complexObjectToContent( |
||
| 736 | &$complexValue, |
||
| 737 | $propertyName, |
||
| 738 | ResourceType & $resourceType, |
||
| 739 | $relativeUri, |
||
| 740 | ODataPropertyContent & $odataPropertyContent |
||
| 741 | ) { |
||
| 742 | $count = count($this->complexTypeInstanceCollection); |
||
| 743 | for ($i = 0; $i < $count; ++$i) { |
||
| 744 | if ($this->complexTypeInstanceCollection[$i] === $complexValue) { |
||
| 745 | throw new InvalidOperationException( |
||
| 746 | Messages::objectModelSerializerLoopsNotAllowedInComplexTypes($propertyName) |
||
| 747 | ); |
||
| 748 | } |
||
| 749 | } |
||
| 750 | |||
| 751 | $this->complexTypeInstanceCollection[$count] = &$complexValue; |
||
| 752 | |||
| 753 | //TODO function to resolve actual type from $resourceType |
||
| 754 | $actualType = $resourceType; |
||
| 755 | $odataEntry = null; |
||
| 756 | $this->_writeObjectProperties( |
||
| 757 | $complexValue, |
||
| 758 | $actualType, |
||
| 759 | null, |
||
| 760 | $relativeUri, |
||
| 761 | $odataEntry, |
||
| 762 | $odataPropertyContent |
||
| 763 | ); |
||
| 764 | unset($this->complexTypeInstanceCollection[$count]); |
||
| 765 | |||
| 766 | return $actualType; |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * @param $customObject |
||
| 771 | * @param ResourceType $resourceType |
||
| 772 | * @param $relativeUri |
||
| 773 | * @param ODataPropertyContent $odataPropertyContent |
||
| 774 | * @param $resourceTypeKind |
||
| 775 | * @param $navigationProperties |
||
| 776 | * @return array |
||
| 777 | * @throws ODataException |
||
| 778 | */ |
||
| 779 | private function writeObjectPropertiesUnexpanded( |
||
| 877 | |||
| 878 | /** |
||
| 879 | * @param $customObject |
||
| 880 | * @param ResourceType $resourceType |
||
| 881 | * @param $relativeUri |
||
| 882 | * @param ODataPropertyContent $odataPropertyContent |
||
| 883 | * @param $projectionNodes |
||
| 884 | * @param $navigationProperties |
||
| 885 | * @return array |
||
| 886 | * @throws ODataException |
||
| 887 | */ |
||
| 888 | private function writeObjectPropertiesExpanded( |
||
| 963 | } |
||
| 964 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.