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 AtomODataWriter 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 AtomODataWriter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class AtomODataWriter implements IODataWriter |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Writer to which output (CSDL Document) is sent. |
||
| 28 | * |
||
| 29 | * @var \XMLWriter |
||
| 30 | */ |
||
| 31 | public $xmlWriter; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The service base uri. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $baseUri; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Construct a new instance of AtomODataWriter. |
||
| 42 | * |
||
| 43 | * @param string $absoluteServiceUri The absolute service Uri |
||
| 44 | */ |
||
| 45 | public function __construct($absoluteServiceUri) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Determines if the given writer is capable of writing the response or not. |
||
| 61 | * |
||
| 62 | * @param Version $responseVersion the OData version of the response |
||
| 63 | * @param string $contentType the Content Type of the response |
||
| 64 | * |
||
| 65 | * @return bool true if the writer can handle the response, false otherwise |
||
| 66 | */ |
||
| 67 | public function canHandle(Version $responseVersion, $contentType) |
||
| 68 | { |
||
| 69 | $parts = explode(';', $contentType); |
||
| 70 | |||
| 71 | //first 2 parts are for service documents, second part is for Resources |
||
| 72 | //TODO: i'm not sold about this first part not being constrained to v1 (or maybe v2).. |
||
|
|
|||
| 73 | //but it's how WS DS works. See #94 |
||
| 74 | return in_array(MimeTypes::MIME_APPLICATION_XML, $parts) |
||
| 75 | || in_array(MimeTypes::MIME_APPLICATION_ATOMSERVICE, $parts) |
||
| 76 | || in_array(MimeTypes::MIME_APPLICATION_ATOM, $parts); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Write the given OData model in a specific response format. |
||
| 81 | * |
||
| 82 | * @param ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content |
||
| 83 | * |
||
| 84 | * @return AtomODataWriter |
||
| 85 | */ |
||
| 86 | public function write($model) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param ODataURL $url the url to write |
||
| 113 | * |
||
| 114 | * @return AtomODataWriter |
||
| 115 | */ |
||
| 116 | protected function writeURL(ODataURL $url) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Begin write odata links. |
||
| 128 | * |
||
| 129 | * @param ODataURLCollection $urls Object of ODataUrlCollection to start writing collection of url |
||
| 130 | * |
||
| 131 | * @return AtomODataWriter |
||
| 132 | */ |
||
| 133 | public function writeUrlCollection(ODataURLCollection $urls) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Begin write OData Feed. |
||
| 171 | * |
||
| 172 | * @param ODataFeed $feed Object of OData feed to start writing feed |
||
| 173 | * @param bool $isTopLevel indicates if this is the top level feed in the response |
||
| 174 | * |
||
| 175 | * @return AtomODataWriter |
||
| 176 | */ |
||
| 177 | protected function writeFeed(ODataFeed $feed, $isTopLevel = false) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Write top level entry. |
||
| 219 | * |
||
| 220 | * @param ODataEntry $entry Object of ODataEntry |
||
| 221 | * @param bool $isTopLevel |
||
| 222 | * |
||
| 223 | * @return AtomODataWriter |
||
| 224 | */ |
||
| 225 | protected function writeEntry(ODataEntry $entry, $isTopLevel = false) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Start writing a entry. |
||
| 244 | * |
||
| 245 | * @param ODataEntry $entry Entry to write |
||
| 246 | * @param bool $isTopLevel |
||
| 247 | * |
||
| 248 | * @return AtomODataWriter |
||
| 249 | */ |
||
| 250 | protected function writeBeginEntry(ODataEntry $entry, $isTopLevel) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param ODataLink $link Link to write |
||
| 370 | * |
||
| 371 | * @return AtomODataWriter |
||
| 372 | */ |
||
| 373 | protected function writeLink(ODataLink $link) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Write the given collection of properties. |
||
| 401 | * (properties of an entity or complex type). |
||
| 402 | * |
||
| 403 | * @param ODataPropertyContent $properties Collection of properties |
||
| 404 | * @param bool $topLevel is this property content is the top level response to be written? |
||
| 405 | * |
||
| 406 | * @return AtomODataWriter |
||
| 407 | */ |
||
| 408 | protected function writeProperties(ODataPropertyContent $properties, $topLevel = false) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * XML write a basic data type (string, number, boolean, null). |
||
| 432 | * |
||
| 433 | * @param string $value value to be written |
||
| 434 | * @param string $type|null data type of the value |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | protected function beforeWriteValue($value, $type = null) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Write the node which hold the entity properties as child. |
||
| 455 | * |
||
| 456 | * @param ODataEntry $entry ODataEntry object for pre writing properties |
||
| 457 | * |
||
| 458 | * @return AtomODataWriter |
||
| 459 | */ |
||
| 460 | public function preWriteProperties(ODataEntry $entry) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Write a property. |
||
| 505 | * |
||
| 506 | * @param ODataProperty $property Property to be written |
||
| 507 | * @param bool $isTopLevel is link top level or not |
||
| 508 | * |
||
| 509 | * @return AtomODataWriter |
||
| 510 | */ |
||
| 511 | protected function beginWriteProperty(ODataProperty $property, $isTopLevel) |
||
| 512 | { |
||
| 513 | $this->xmlWriter->startElementNS( |
||
| 514 | ODataConstants::ODATA_NAMESPACE_PREFIX, |
||
| 515 | $property->name, |
||
| 516 | null |
||
| 517 | ); |
||
| 518 | if ($property->typeName != null) { |
||
| 519 | $this->xmlWriter->startAttributeNs( |
||
| 520 | ODataConstants::ODATA_METADATA_NAMESPACE_PREFIX, |
||
| 521 | ODataConstants::ATOM_TYPE_ATTRIBUTE_NAME, |
||
| 522 | null |
||
| 523 | ); |
||
| 524 | $this->xmlWriter->text($property->typeName); |
||
| 525 | } |
||
| 526 | if ($isTopLevel) { |
||
| 527 | $this->xmlWriter->startAttribute(ODataConstants::XMLNS_NAMESPACE_PREFIX); |
||
| 528 | $this->xmlWriter->text(ODataConstants::ODATA_METADATA_NAMESPACE); |
||
| 529 | $this->xmlWriter->startAttributeNs( |
||
| 530 | ODataConstants::XMLNS_NAMESPACE_PREFIX, |
||
| 531 | ODataConstants::ODATA_NAMESPACE_PREFIX, |
||
| 532 | null |
||
| 533 | ); |
||
| 534 | $this->xmlWriter->text(ODataConstants::ODATA_NAMESPACE); |
||
| 535 | $this->xmlWriter->startAttributeNs( |
||
| 536 | ODataConstants::XMLNS_NAMESPACE_PREFIX, |
||
| 537 | ODataConstants::ODATA_METADATA_NAMESPACE_PREFIX, |
||
| 538 | null |
||
| 539 | ); |
||
| 540 | $this->xmlWriter->text(ODataConstants::ODATA_METADATA_NAMESPACE); |
||
| 541 | } |
||
| 542 | if ($property->typeName != null || $isTopLevel) { |
||
| 543 | $this->xmlWriter->endAttribute(); |
||
| 544 | } |
||
| 545 | |||
| 546 | return $this; |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Write after last property. |
||
| 551 | * |
||
| 552 | * @param ODataEntry $entry Entry object to post writing properties |
||
| 553 | * |
||
| 554 | * @return AtomODataWriter |
||
| 555 | */ |
||
| 556 | public function postWriteProperties(ODataEntry $entry) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Begin an item in a collection. |
||
| 568 | * |
||
| 569 | * @param ODataBagContent $bag Bag property object to begin write property |
||
| 570 | * |
||
| 571 | * @return AtomODataWriter |
||
| 572 | */ |
||
| 573 | protected function writeBagContent(ODataBagContent $bag) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Write null value. |
||
| 601 | * |
||
| 602 | * @param ODataProperty $property ODataProperty object to write null value |
||
| 603 | * according to property type |
||
| 604 | * |
||
| 605 | * @return AtomODataWriter |
||
| 606 | */ |
||
| 607 | protected function writeNullValue(ODataProperty $property) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Get the final result as string. |
||
| 623 | * |
||
| 624 | * @return string output of requested data in Atom format |
||
| 625 | */ |
||
| 626 | public function getOutput() |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Serialize the exception. |
||
| 635 | * |
||
| 636 | * @param ODataException $exception Exception to serialize |
||
| 637 | * @param bool $serializeInnerException if set to true, |
||
| 638 | * serialize the inner exception if $exception is an ODataException |
||
| 639 | * |
||
| 640 | * @return string |
||
| 641 | */ |
||
| 642 | public static function serializeException(ODataException $exception, $serializeInnerException) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Function to create element only contain value without argument. |
||
| 677 | * |
||
| 678 | * @param string $node Element name |
||
| 679 | * @param string $value Element value |
||
| 680 | * |
||
| 681 | * @return AtomODataWriter |
||
| 682 | */ |
||
| 683 | public function writeNodeValue($node, $value) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Function to create element with one attribute and value. |
||
| 694 | * |
||
| 695 | * @param string $node Element name |
||
| 696 | * @param string $attribute Attribute name |
||
| 697 | * @param string $attributeValue Attribute value |
||
| 698 | * @param string $nodeValue Element value |
||
| 699 | * |
||
| 700 | * @return AtomODataWriter |
||
| 701 | */ |
||
| 702 | public function writeNodeAttributeValue( |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Function to create link element with arguments. |
||
| 718 | * |
||
| 719 | * @param ODataLink $link Link object to make link element |
||
| 720 | * @param bool $isExpanded Is link expanded or not |
||
| 721 | * |
||
| 722 | * @return AtomODataWriter |
||
| 723 | */ |
||
| 724 | protected function writeLinkNode(ODataLink $link, $isExpanded) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Function to write base uri and default namespaces for top level elements. |
||
| 756 | * |
||
| 757 | * @return AtomODataWriter |
||
| 758 | */ |
||
| 759 | public function writeBaseUriAndDefaultNamespaces() |
||
| 784 | |||
| 785 | /** |
||
| 786 | * XML prefix for the Atom namespace. |
||
| 787 | * |
||
| 788 | * @var string |
||
| 789 | */ |
||
| 790 | const ATOM_NAMESPACE_PREFIX = 'atom'; |
||
| 791 | |||
| 792 | /** |
||
| 793 | * XML prefix for the Atom Publishing Protocol namespace. |
||
| 794 | * |
||
| 795 | * @var string |
||
| 796 | */ |
||
| 797 | const APP_NAMESPACE_PREFIX = 'app'; |
||
| 798 | |||
| 799 | /** |
||
| 800 | * @param ProvidersWrapper $providers |
||
| 801 | * |
||
| 802 | * @return IODataWriter |
||
| 803 | */ |
||
| 804 | public function writeServiceDocument(ProvidersWrapper $providers) |
||
| 805 | { |
||
| 806 | $writer = $this->xmlWriter; |
||
| 807 | $writer->startElementNs( |
||
| 808 | null, |
||
| 809 | ODataConstants::ATOM_PUBLISHING_SERVICE_ELEMENT_NAME, |
||
| 810 | ODataConstants::APP_NAMESPACE |
||
| 811 | ); |
||
| 812 | $writer->writeAttributeNs( |
||
| 813 | ODataConstants::XML_NAMESPACE_PREFIX, |
||
| 814 | ODataConstants::XML_BASE_ATTRIBUTE_NAME, |
||
| 815 | null, |
||
| 816 | $this->baseUri |
||
| 817 | ); |
||
| 818 | $writer->writeAttributeNs( |
||
| 819 | ODataConstants::XMLNS_NAMESPACE_PREFIX, |
||
| 820 | self::ATOM_NAMESPACE_PREFIX, |
||
| 821 | null, |
||
| 822 | ODataConstants::ATOM_NAMESPACE |
||
| 823 | ); |
||
| 824 | //$writer->writeAttributeNs( |
||
| 825 | //ODataConstants::XMLNS_NAMESPACE_PREFIX, |
||
| 826 | //self::APP_NAMESPACE_PREFIX, |
||
| 827 | //null, |
||
| 828 | //ODataConstants::APP_NAMESPACE |
||
| 829 | //); |
||
| 830 | |||
| 831 | $writer->startElement(ODataConstants::ATOM_PUBLISHING_WORKSPACE_ELEMNT_NAME); |
||
| 832 | $writer->startElementNs(self::ATOM_NAMESPACE_PREFIX, ODataConstants::ATOM_TITLE_ELELMET_NAME, null); |
||
| 833 | $writer->text(ODataConstants::ATOM_PUBLISHING_WORKSPACE_DEFAULT_VALUE); |
||
| 834 | $writer->endElement(); |
||
| 835 | foreach ($providers->getResourceSets() as $resourceSetWrapper) { |
||
| 836 | $name = $resourceSetWrapper->getName(); |
||
| 837 | $this->writeServiceDocumentNode($writer, $name); |
||
| 838 | } |
||
| 839 | foreach ($providers->getSingletons() as $single) { |
||
| 840 | $name = $single->getName(); |
||
| 841 | //start collection node |
||
| 842 | $this->writeServiceDocumentNode($writer, $name); |
||
| 843 | } |
||
| 844 | |||
| 845 | //End workspace and service nodes |
||
| 846 | $writer->endElement(); |
||
| 847 | $writer->endElement(); |
||
| 848 | |||
| 849 | return $this; |
||
| 850 | } |
||
| 851 | |||
| 852 | /** |
||
| 853 | * @param $writer |
||
| 854 | * @param $name |
||
| 855 | */ |
||
| 856 | private function writeServiceDocumentNode(&$writer, $name) |
||
| 869 | } |
||
| 870 |