Complex classes like GraphNavigator 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 GraphNavigator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | final class GraphNavigator |
||
| 44 | { |
||
| 45 | const DIRECTION_SERIALIZATION = 1; |
||
| 46 | const DIRECTION_DESERIALIZATION = 2; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var ExpressionLanguageExclusionStrategy |
||
| 50 | */ |
||
| 51 | private $expressionExclusionStrategy; |
||
| 52 | |||
| 53 | private $dispatcher; |
||
| 54 | private $metadataFactory; |
||
| 55 | private $handlerRegistry; |
||
| 56 | private $objectConstructor; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Parses a direction string to one of the direction constants. |
||
| 60 | * |
||
| 61 | * @param string $dirStr |
||
| 62 | * |
||
| 63 | * @return integer |
||
| 64 | */ |
||
| 65 | 6 | public static function parseDirection($dirStr) |
|
| 78 | |||
| 79 | 324 | public function __construct( |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Called for each node of the graph that is being traversed. |
||
| 98 | * |
||
| 99 | * @param mixed $data the data depends on the direction, and type of visitor |
||
| 100 | * @param null|array $type array has the format ["name" => string, "params" => array] |
||
| 101 | * @param Context $context |
||
| 102 | * @return mixed the return value depends on the direction, and type of visitor |
||
| 103 | */ |
||
| 104 | 307 | public function accept($data, array $type = null, Context $context) |
|
| 105 | { |
||
| 106 | 307 | $visitor = $context->getVisitor(); |
|
| 107 | |||
| 108 | // If the type was not given, we infer the most specific type from the |
||
| 109 | // input data in serialization mode. |
||
| 110 | 307 | if (null === $type) { |
|
| 111 | 288 | if ($context instanceof DeserializationContext) { |
|
| 112 | throw new RuntimeException('The type must be given for all properties when deserializing.'); |
||
| 113 | 8 | } |
|
| 114 | |||
| 115 | 288 | $typeName = gettype($data); |
|
| 116 | 288 | if ('object' === $typeName) { |
|
| 117 | 213 | $typeName = get_class($data); |
|
| 118 | 213 | } |
|
| 119 | |||
| 120 | 288 | $type = array('name' => $typeName, 'params' => array()); |
|
| 121 | 288 | } |
|
| 122 | // If the data is null, we have to force the type to null regardless of the input in order to |
||
| 123 | // guarantee correct handling of null values, and not have any internal auto-casting behavior. |
||
| 124 | 219 | else if ($context instanceof SerializationContext && null === $data) { |
|
| 125 | 15 | $type = array('name' => 'NULL', 'params' => array()); |
|
| 126 | 15 | } |
|
| 127 | |||
| 128 | 307 | switch ($type['name']) { |
|
| 129 | 307 | case 'NULL': |
|
| 130 | 45 | return $visitor->visitNull($data, $type, $context); |
|
| 131 | |||
| 132 | 299 | case 'string': |
|
| 133 | 173 | return $visitor->visitString($data, $type, $context); |
|
| 134 | |||
| 135 | 292 | case 'int': |
|
| 136 | 292 | case 'integer': |
|
| 137 | 51 | return $visitor->visitInteger($data, $type, $context); |
|
| 138 | |||
| 139 | 286 | case 'boolean': |
|
| 140 | 22 | return $visitor->visitBoolean($data, $type, $context); |
|
| 141 | |||
| 142 | 275 | case 'double': |
|
| 143 | 275 | case 'float': |
|
| 144 | 36 | return $visitor->visitDouble($data, $type, $context); |
|
| 145 | |||
| 146 | 258 | case 'array': |
|
| 147 | 93 | return $visitor->visitArray($data, $type, $context); |
|
| 148 | |||
| 149 | 228 | case 'resource': |
|
| 150 | 1 | $msg = 'Resources are not supported in serialized data.'; |
|
| 151 | 1 | if ($context instanceof SerializationContext && null !== $path = $context->getPath()) { |
|
| 152 | $msg .= ' Path: '.$path; |
||
| 153 | } |
||
| 154 | |||
| 155 | 1 | throw new RuntimeException($msg); |
|
| 156 | |||
| 157 | 227 | default: |
|
| 158 | // TODO: The rest of this method needs some refactoring. |
||
| 159 | 227 | if ($context instanceof SerializationContext) { |
|
| 160 | 211 | if (null !== $data) { |
|
| 161 | 211 | if ($context->isVisiting($data)) { |
|
| 162 | 3 | return null; |
|
| 163 | } |
||
| 164 | 211 | $context->startVisiting($data); |
|
| 165 | 211 | } |
|
| 166 | |||
| 167 | // If we're serializing a polymorphic type, then we'll be interested in the |
||
| 168 | // metadata for the actual type of the object, not the base class. |
||
| 169 | 211 | if (class_exists($type['name'], false) || interface_exists($type['name'], false)) { |
|
| 170 | 211 | if (is_subclass_of($data, $type['name'], false)) { |
|
|
|
|||
| 171 | 6 | $type = array('name' => get_class($data), 'params' => array()); |
|
| 172 | 6 | } |
|
| 173 | 211 | } |
|
| 174 | 227 | } elseif ($context instanceof DeserializationContext) { |
|
| 175 | 69 | $context->increaseDepth(); |
|
| 176 | 69 | } |
|
| 177 | |||
| 178 | // Trigger pre-serialization callbacks, and listeners if they exist. |
||
| 179 | // Dispatch pre-serialization event before handling data to have ability change type in listener |
||
| 180 | 227 | if ($context instanceof SerializationContext) { |
|
| 181 | 211 | if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_serialize', $type['name'], $context->getFormat())) { |
|
| 182 | 206 | $this->dispatcher->dispatch('serializer.pre_serialize', $type['name'], $context->getFormat(), $event = new PreSerializeEvent($context, $data, $type)); |
|
| 183 | 206 | $type = $event->getType(); |
|
| 184 | 206 | } |
|
| 185 | 227 | } elseif ($context instanceof DeserializationContext) { |
|
| 186 | 69 | if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_deserialize', $type['name'], $context->getFormat())) { |
|
| 187 | $this->dispatcher->dispatch('serializer.pre_deserialize', $type['name'], $context->getFormat(), $event = new PreDeserializeEvent($context, $data, $type)); |
||
| 188 | $type = $event->getType(); |
||
| 189 | $data = $event->getData(); |
||
| 190 | } |
||
| 191 | 69 | } |
|
| 192 | |||
| 193 | // First, try whether a custom handler exists for the given type. This is done |
||
| 194 | // before loading metadata because the type name might not be a class, but |
||
| 195 | // could also simply be an artifical type. |
||
| 196 | 227 | if (null !== $handler = $this->handlerRegistry->getHandler($context->getDirection(), $type['name'], $context->getFormat())) { |
|
| 197 | 55 | $rs = call_user_func($handler, $visitor, $data, $type, $context); |
|
| 198 | 55 | $this->leaveScope($context, $data); |
|
| 199 | |||
| 200 | 55 | return $rs; |
|
| 201 | } |
||
| 202 | |||
| 203 | 203 | $exclusionStrategy = $context->getExclusionStrategy(); |
|
| 204 | |||
| 205 | /** @var $metadata ClassMetadata */ |
||
| 206 | 203 | $metadata = $this->metadataFactory->getMetadataForClass($type['name']); |
|
| 207 | |||
| 208 | 200 | if ($metadata->usingExpression && !$this->expressionExclusionStrategy) { |
|
| 209 | 3 | throw new ExpressionLanguageRequiredException("To use conditional exclude/expose in {$metadata->name} you must configure the expression language."); |
|
| 210 | } |
||
| 211 | |||
| 212 | 197 | if ($context instanceof DeserializationContext && ! empty($metadata->discriminatorMap) && $type['name'] === $metadata->discriminatorBaseClass) { |
|
| 213 | 12 | $metadata = $this->resolveMetadata($data, $metadata); |
|
| 214 | 10 | } |
|
| 215 | |||
| 216 | 195 | if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipClass($metadata, $context)) { |
|
| 217 | 3 | $this->leaveScope($context, $data); |
|
| 218 | |||
| 219 | 3 | return null; |
|
| 220 | } |
||
| 221 | |||
| 222 | 195 | $context->pushClassMetadata($metadata); |
|
| 223 | |||
| 224 | 195 | if ($context instanceof SerializationContext) { |
|
| 225 | 183 | foreach ($metadata->preSerializeMethods as $method) { |
|
| 226 | 3 | $method->invoke($data); |
|
| 227 | 183 | } |
|
| 228 | 183 | } |
|
| 229 | |||
| 230 | 195 | $object = $data; |
|
| 231 | 195 | if ($context instanceof DeserializationContext) { |
|
| 232 | 57 | $object = $this->objectConstructor->construct($visitor, $metadata, $data, $type, $context); |
|
| 233 | 57 | } |
|
| 234 | |||
| 235 | 195 | if (isset($metadata->handlerCallbacks[$context->getDirection()][$context->getFormat()])) { |
|
| 236 | 3 | $rs = $object->{$metadata->handlerCallbacks[$context->getDirection()][$context->getFormat()]}( |
|
| 237 | 3 | $visitor, |
|
| 238 | 3 | $context instanceof SerializationContext ? null : $data, |
|
| 239 | $context |
||
| 240 | 3 | ); |
|
| 241 | 3 | $this->afterVisitingObject($metadata, $object, $type, $context); |
|
| 242 | |||
| 243 | 3 | return $context instanceof SerializationContext ? $rs : $object; |
|
| 244 | } |
||
| 245 | |||
| 246 | 192 | $visitor->startVisitingObject($metadata, $object, $type, $context); |
|
| 247 | 192 | foreach ($metadata->propertyMetadata as $propertyMetadata) { |
|
| 248 | 189 | if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipProperty($propertyMetadata, $context)) { |
|
| 249 | 13 | continue; |
|
| 250 | } |
||
| 251 | |||
| 252 | 189 | if (null !== $this->expressionExclusionStrategy && $this->expressionExclusionStrategy->shouldSkipProperty($propertyMetadata, $context)) { |
|
| 253 | 23 | continue; |
|
| 254 | } |
||
| 255 | |||
| 256 | 189 | if ($context instanceof DeserializationContext && $propertyMetadata->readOnly) { |
|
| 257 | 16 | continue; |
|
| 258 | } |
||
| 259 | |||
| 260 | 189 | $context->pushPropertyMetadata($propertyMetadata); |
|
| 261 | 189 | $visitor->visitProperty($propertyMetadata, $data, $context); |
|
| 262 | 188 | $context->popPropertyMetadata(); |
|
| 263 | 191 | } |
|
| 264 | |||
| 265 | 189 | if ($context instanceof SerializationContext) { |
|
| 266 | 178 | $this->afterVisitingObject($metadata, $data, $type, $context); |
|
| 267 | |||
| 268 | 178 | return $visitor->endVisitingObject($metadata, $data, $type, $context); |
|
| 269 | } |
||
| 270 | |||
| 271 | 57 | $rs = $visitor->endVisitingObject($metadata, $data, $type, $context); |
|
| 272 | 57 | $this->afterVisitingObject($metadata, $rs, $type, $context); |
|
| 273 | |||
| 274 | 57 | return $rs; |
|
| 275 | 227 | } |
|
| 276 | } |
||
| 277 | |||
| 278 | 12 | private function resolveMetadata($data, ClassMetadata $metadata) |
|
| 313 | |||
| 314 | 216 | private function leaveScope(Context $context, $data) |
|
| 322 | |||
| 323 | 192 | private function afterVisitingObject(ClassMetadata $metadata, $object, array $type, Context $context) |
|
| 348 | } |
||
| 349 |