Complex classes like GenericDeserializationVisitor 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 GenericDeserializationVisitor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | abstract class GenericDeserializationVisitor extends AbstractVisitor |
||
33 | { |
||
34 | private $navigator; |
||
35 | private $result; |
||
36 | private $objectStack; |
||
37 | private $currentObject; |
||
38 | |||
39 | public function setNavigator(GraphNavigator $navigator) |
||
40 | { |
||
41 | $this->navigator = $navigator; |
||
42 | $this->result = null; |
||
43 | $this->objectStack = new \SplStack; |
||
44 | } |
||
45 | |||
46 | public function getNavigator() |
||
50 | |||
51 | public function prepare($data) |
||
55 | |||
56 | public function visitNull($data, array $type, Context $context) |
||
60 | |||
61 | public function visitString($data, array $type, Context $context) |
||
77 | |||
78 | public function visitBoolean($data, array $type, Context $context) |
||
94 | |||
95 | public function visitInteger($data, array $type, Context $context) |
||
111 | |||
112 | public function visitDouble($data, array $type, Context $context) |
||
128 | |||
129 | public function visitArray($data, array $type, Context $context) |
||
130 | { |
||
131 | if ( ! is_array($data) && ! ($data instanceof \ArrayObject)) { |
||
132 | throw new DeserializeException($type, $data, $context); |
||
133 | } |
||
134 | |||
135 | // If no further parameters were given, keys/values are just passed as is. |
||
136 | if ( ! $type['params']) { |
||
137 | if (null === $this->result) { |
||
138 | $this->result = $data; |
||
139 | } |
||
140 | |||
141 | return $data; |
||
142 | } |
||
143 | |||
144 | switch (count($type['params'])) { |
||
145 | case 1: // Array is a list. |
||
146 | $listType = $type['params'][0]; |
||
147 | |||
148 | $result = array(); |
||
149 | if (null === $this->result) { |
||
150 | $this->result = &$result; |
||
151 | } |
||
152 | |||
153 | foreach ($data as $k => $v) { |
||
154 | $context->pushIndexMetadata(new IndexMetadata($k)); |
||
155 | $result[] = $this->navigator->accept($v, $listType, $context); |
||
156 | $context->popIndexMetadata(); |
||
157 | } |
||
158 | |||
159 | return $result; |
||
160 | |||
161 | case 2: // Array is a map. |
||
162 | list($keyType, $entryType) = $type['params']; |
||
163 | |||
164 | $result = array(); |
||
165 | if (null === $this->result) { |
||
166 | $this->result = &$result; |
||
167 | } |
||
168 | |||
169 | foreach ($data as $k => $v) { |
||
170 | $context->pushIndexMetadata(new IndexMetadata($k)); |
||
171 | $result[$this->navigator->accept($k, $keyType, $context)] = $this->navigator->accept($v, $entryType, $context); |
||
172 | $context->popIndexMetadata(); |
||
173 | } |
||
174 | |||
175 | return $result; |
||
176 | |||
177 | default: |
||
178 | throw new RuntimeException(sprintf('Array type cannot have more than 2 parameters, but got %s.', json_encode($type['params']))); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | public function startVisitingObject(ClassMetadata $metadata, $object, array $type, Context $context) |
||
183 | { |
||
184 | $this->setCurrentObject($object); |
||
185 | |||
186 | if (null === $this->result) { |
||
187 | $this->result = $this->currentObject; |
||
188 | } |
||
189 | } |
||
190 | |||
191 | public function visitProperty(PropertyMetadata $metadata, $data, Context $context) |
||
192 | { |
||
193 | $name = $this->namingStrategy->translateName($metadata); |
||
194 | |||
195 | if (null === $data) { |
||
196 | return; |
||
197 | } |
||
198 | if ( ! is_array($data)) { |
||
199 | $context->popPropertyMetadata(); |
||
200 | throw new DeserializeException(array('name' => 'object', 'params' => array()), $data, $context); |
||
201 | } |
||
202 | if ( ! array_key_exists($name, $data)) { |
||
203 | return; |
||
204 | } |
||
205 | |||
206 | if ( ! $metadata->type) { |
||
207 | throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->reflection->class, $metadata->name)); |
||
208 | } |
||
209 | |||
210 | $v = $data[$name] !== null ? $this->navigator->accept($data[$name], $metadata->type, $context) : null; |
||
211 | |||
212 | if (null === $metadata->setter) { |
||
213 | $metadata->reflection->setValue($this->currentObject, $v); |
||
214 | |||
215 | return; |
||
216 | } |
||
217 | |||
218 | $this->currentObject->{$metadata->setter}($v); |
||
219 | } |
||
220 | |||
221 | public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
||
222 | { |
||
223 | $obj = $this->currentObject; |
||
224 | $this->revertCurrentObject(); |
||
225 | |||
226 | return $obj; |
||
227 | } |
||
228 | |||
229 | public function getResult() |
||
233 | |||
234 | public function setCurrentObject($object) |
||
235 | { |
||
236 | $this->objectStack->push($this->currentObject); |
||
237 | $this->currentObject = $object; |
||
238 | } |
||
239 | |||
240 | public function getCurrentObject() |
||
244 | |||
245 | public function revertCurrentObject() |
||
249 | |||
250 | abstract protected function decode($str); |
||
251 | } |
||
252 |