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 CynicDeserialiser 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 CynicDeserialiser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class CynicDeserialiser |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var IMetadataProvider |
||
| 17 | */ |
||
| 18 | private $metaProvider; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var ProvidersWrapper |
||
| 22 | */ |
||
| 23 | private $wrapper; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var ModelDeserialiser |
||
| 27 | */ |
||
| 28 | private $cereal; |
||
| 29 | |||
| 30 | public function __construct(IMetadataProvider $meta, ProvidersWrapper $wrapper) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param ODataEntry $payload |
||
| 39 | */ |
||
| 40 | public function processPayload(ODataEntry &$payload) |
||
| 41 | { |
||
| 42 | assert($this->isEntryOK($payload)); |
||
| 43 | list($sourceSet, $source) = $this->processEntryContent($payload); |
||
| 44 | assert($sourceSet instanceof ResourceSet); |
||
| 45 | $numLinks = count($payload->links); |
||
| 46 | for ($i = 0; $i < $numLinks; $i++) { |
||
| 47 | $this->processLink($payload->links[$i], $sourceSet, $source); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | protected function isEntryOK(ODataEntry $payload) |
||
| 52 | { |
||
| 53 | // check links |
||
| 54 | foreach ($payload->links as $link) { |
||
| 55 | $hasUrl = isset($link->url); |
||
| 56 | $hasExpanded = isset($link->expandedResult); |
||
| 57 | if ($hasUrl) { |
||
| 58 | if (!is_string($link->url)) { |
||
| 59 | $msg = 'Url must be either string or null'; |
||
| 60 | throw new \InvalidArgumentException($msg); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | if ($hasExpanded) { |
||
| 64 | $isGood = $link->expandedResult instanceof ODataEntry || $link->expandedResult instanceof ODataFeed; |
||
| 65 | if (!$isGood) { |
||
| 66 | $msg = 'Expanded result must null, or be instance of ODataEntry or ODataFeed'; |
||
| 67 | throw new \InvalidArgumentException($msg); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | $isEntry = $link->expandedResult instanceof ODataEntry; |
||
| 71 | |||
| 72 | if ($hasExpanded) { |
||
| 73 | if ($isEntry) { |
||
| 74 | $this->isEntryOK($link->expandedResult); |
||
| 75 | } else { |
||
| 76 | foreach ($link->expandedResult->entries as $expanded) { |
||
| 77 | $this->isEntryOK($expanded); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | $set = $this->getMetaProvider()->resolveResourceSet($payload->resourceSetName); |
||
| 84 | if (null === $set) { |
||
| 85 | $msg = 'Specified resource set could not be resolved'; |
||
| 86 | throw new \InvalidArgumentException($msg); |
||
| 87 | } |
||
| 88 | return true; |
||
| 89 | } |
||
| 90 | |||
| 91 | protected function processEntryContent(ODataEntry &$content) |
||
| 92 | { |
||
| 93 | assert(null === $content->id || is_string($content->id), 'Entry id must be null or string'); |
||
| 94 | |||
| 95 | $isCreate = null === $content->id; |
||
| 96 | $set = $this->getMetaProvider()->resolveResourceSet($content->resourceSetName); |
||
| 97 | assert($set instanceof ResourceSet, get_class($set)); |
||
| 98 | $type = $set->getResourceType(); |
||
| 99 | $properties = $this->getDeserialiser()->bulkDeserialise($type, $content); |
||
| 100 | $properties = (object) $properties; |
||
| 101 | |||
| 102 | if ($isCreate) { |
||
| 103 | $result = $this->getWrapper()->createResourceforResourceSet($set, null, $properties); |
||
| 104 | assert(isset($result), get_class($result)); |
||
| 105 | $key = $this->generateKeyDescriptor($type, $result); |
||
| 106 | } else { |
||
| 107 | $key = $this->generateKeyDescriptor($type, $content->propertyContent); |
||
| 108 | assert($key instanceof KeyDescriptor, get_class($key)); |
||
| 109 | $source = $this->getWrapper()->getResourceFromResourceSet($set, $key); |
||
| 110 | assert(isset($source), get_class($source)); |
||
| 111 | $result = $this->getWrapper()->updateResource($set, $source, $key, $properties); |
||
| 112 | } |
||
| 113 | |||
| 114 | $content->id = $key; |
||
| 115 | return [$set, $result]; |
||
| 116 | } |
||
| 117 | |||
| 118 | protected function getMetaProvider() |
||
| 122 | |||
| 123 | protected function getWrapper() |
||
| 127 | |||
| 128 | protected function getDeserialiser() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param ResourceEntityType $type |
||
| 135 | * @param ODataPropertyContent|object $result |
||
| 136 | * @return null|KeyDescriptor |
||
| 137 | */ |
||
| 138 | protected function generateKeyDescriptor(ResourceEntityType $type, $result) |
||
| 161 | |||
| 162 | protected function processLink(ODataLink &$link, ResourceSet $sourceSet, $source) |
||
| 163 | { |
||
| 164 | $hasUrl = isset($link->url); |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param ODataLink $link |
||
| 189 | * @param ResourceSet $sourceSet |
||
| 190 | * @param $source |
||
| 191 | * @param $hasUrl |
||
| 192 | * @param $hasPayload |
||
| 193 | */ |
||
| 194 | protected function processLinkSingleton(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload) |
||
| 249 | |||
| 250 | protected function processLinkFeed(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload) |
||
| 325 | } |
||
| 326 |