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 | return $source; |
||
50 | } |
||
51 | |||
52 | protected function isEntryOK(ODataEntry $payload) |
||
53 | { |
||
54 | // check links |
||
55 | foreach ($payload->links as $link) { |
||
56 | $hasUrl = isset($link->url); |
||
57 | $hasExpanded = isset($link->expandedResult); |
||
58 | if ($hasUrl) { |
||
59 | if (!is_string($link->url)) { |
||
60 | $msg = 'Url must be either string or null'; |
||
61 | throw new \InvalidArgumentException($msg); |
||
62 | } |
||
63 | } |
||
64 | if ($hasExpanded) { |
||
65 | $isGood = $link->expandedResult instanceof ODataEntry || $link->expandedResult instanceof ODataFeed; |
||
66 | if (!$isGood) { |
||
67 | $msg = 'Expanded result must null, or be instance of ODataEntry or ODataFeed'; |
||
68 | throw new \InvalidArgumentException($msg); |
||
69 | } |
||
70 | } |
||
71 | $isEntry = $link->expandedResult instanceof ODataEntry; |
||
72 | |||
73 | if ($hasExpanded) { |
||
74 | if ($isEntry) { |
||
75 | $this->isEntryOK($link->expandedResult); |
||
76 | } else { |
||
77 | foreach ($link->expandedResult->entries as $expanded) { |
||
78 | $this->isEntryOK($expanded); |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | $set = $this->getMetaProvider()->resolveResourceSet($payload->resourceSetName); |
||
85 | if (null === $set) { |
||
86 | $msg = 'Specified resource set could not be resolved'; |
||
87 | throw new \InvalidArgumentException($msg); |
||
88 | } |
||
89 | return true; |
||
90 | } |
||
91 | |||
92 | protected function processEntryContent(ODataEntry &$content) |
||
93 | { |
||
94 | assert(null === $content->id || is_string($content->id), 'Entry id must be null or string'); |
||
95 | |||
96 | $isCreate = null === $content->id || empty($content->id); |
||
97 | $set = $this->getMetaProvider()->resolveResourceSet($content->resourceSetName); |
||
98 | assert($set instanceof ResourceSet, get_class($set)); |
||
99 | $type = $set->getResourceType(); |
||
100 | $properties = $this->getDeserialiser()->bulkDeserialise($type, $content); |
||
101 | $properties = (object) $properties; |
||
102 | |||
103 | if ($isCreate) { |
||
104 | $result = $this->getWrapper()->createResourceforResourceSet($set, null, $properties); |
||
105 | assert(isset($result), get_class($result)); |
||
106 | $key = $this->generateKeyDescriptor($type, $result); |
||
107 | } else { |
||
108 | $key = $this->generateKeyDescriptor($type, $content->propertyContent, $content->id); |
||
109 | assert($key instanceof KeyDescriptor, get_class($key)); |
||
110 | $source = $this->getWrapper()->getResourceFromResourceSet($set, $key); |
||
111 | assert(isset($source), get_class($source)); |
||
112 | $result = $this->getWrapper()->updateResource($set, $source, $key, $properties); |
||
113 | } |
||
114 | |||
115 | $content->id = $key; |
||
116 | return [$set, $result]; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return IMetadataProvider |
||
121 | */ |
||
122 | protected function getMetaProvider() |
||
126 | |||
127 | /** |
||
128 | * @return ProvidersWrapper |
||
129 | */ |
||
130 | protected function getWrapper() |
||
134 | |||
135 | /** |
||
136 | * @return ModelDeserialiser |
||
137 | */ |
||
138 | protected function getDeserialiser() |
||
142 | |||
143 | /** |
||
144 | * @param ResourceEntityType $type |
||
145 | * @param ODataPropertyContent|object $result |
||
146 | * @param string|null $id |
||
147 | * @return null|KeyDescriptor |
||
148 | */ |
||
149 | protected function generateKeyDescriptor(ResourceEntityType $type, $result, $id = null) |
||
181 | |||
182 | protected function processLink(ODataLink &$link, ResourceSet $sourceSet, $source) |
||
206 | |||
207 | /** |
||
208 | * @param ODataLink $link |
||
209 | * @param ResourceSet $sourceSet |
||
210 | * @param $source |
||
211 | * @param $hasUrl |
||
212 | * @param $hasPayload |
||
213 | */ |
||
214 | protected function processLinkSingleton(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload) |
||
271 | |||
272 | protected function processLinkFeed(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload) |
||
347 | } |
||
348 |