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) |
|
| 66 | { |
||
| 67 | 6 | switch (strtolower($dirStr)) { |
|
| 68 | 6 | case 'serialization': |
|
| 69 | 6 | return self::DIRECTION_SERIALIZATION; |
|
| 70 | |||
| 71 | 3 | case 'deserialization': |
|
| 72 | 3 | return self::DIRECTION_DESERIALIZATION; |
|
| 73 | |||
| 74 | default: |
||
| 75 | throw new InvalidArgumentException(sprintf('The direction "%s" does not exist.', $dirStr)); |
||
| 76 | 2 | } |
|
| 77 | } |
||
| 78 | |||
| 79 | 402 | 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 | 375 | public function accept($data, array $type = null, Context $context) |
|
| 105 | { |
||
| 106 | 375 | $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 | 375 | if (null === $type) { |
|
| 111 | 325 | if ($context instanceof DeserializationContext) { |
|
| 112 | throw new RuntimeException('The type must be given for all properties when deserializing.'); |
||
| 113 | } |
||
| 114 | |||
| 115 | 325 | $typeName = gettype($data); |
|
| 116 | 325 | if ('object' === $typeName) { |
|
| 117 | 236 | $typeName = get_class($data); |
|
| 118 | 236 | } |
|
| 119 | |||
| 120 | 325 | $type = array('name' => $typeName, 'params' => array()); |
|
| 121 | 325 | } |
|
| 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 | 268 | else if ($context instanceof SerializationContext && null === $data) { |
|
| 125 | 21 | $type = array('name' => 'NULL', 'params' => array()); |
|
| 126 | 21 | } |
|
| 127 | // Sometimes data can convey null but is not of a null type. |
||
| 128 | // Visitors can have the power to add this custom null evaluation |
||
| 129 | 375 | if ($visitor instanceof NullAwareVisitorInterface && $visitor->isNull($data) === true) { |
|
| 130 | 7 | $type = array('name' => 'NULL', 'params' => array()); |
|
| 131 | 7 | } |
|
| 132 | |||
| 133 | 375 | switch ($type['name']) { |
|
| 134 | 375 | case 'NULL': |
|
| 135 | 49 | return $visitor->visitNull($data, $type, $context); |
|
| 136 | |||
| 137 | 362 | case 'string': |
|
| 138 | 214 | return $visitor->visitString($data, $type, $context); |
|
| 139 | |||
| 140 | 356 | case 'int': |
|
| 141 | 356 | case 'integer': |
|
| 142 | 65 | return $visitor->visitInteger($data, $type, $context); |
|
| 143 | |||
| 144 | 351 | case 'bool': |
|
| 145 | 351 | case 'boolean': |
|
| 146 | 22 | return $visitor->visitBoolean($data, $type, $context); |
|
| 147 | |||
| 148 | 340 | case 'double': |
|
| 149 | 340 | case 'float': |
|
| 150 | 34 | return $visitor->visitDouble($data, $type, $context); |
|
| 151 | |||
| 152 | 325 | case 'array': |
|
| 153 | 140 | return $visitor->visitArray($data, $type, $context); |
|
| 154 | |||
| 155 | 259 | case 'resource': |
|
| 156 | 1 | $msg = 'Resources are not supported in serialized data.'; |
|
| 157 | 1 | if ($context instanceof SerializationContext && null !== $path = $context->getPath()) { |
|
| 158 | $msg .= ' Path: ' . $path; |
||
| 159 | } |
||
| 160 | |||
| 161 | 1 | throw new RuntimeException($msg); |
|
| 162 | |||
| 163 | 258 | default: |
|
| 164 | // TODO: The rest of this method needs some refactoring. |
||
| 165 | 258 | if ($context instanceof SerializationContext) { |
|
| 166 | 240 | if (null !== $data) { |
|
| 167 | 240 | if ($context->isVisiting($data)) { |
|
| 168 | 3 | return null; |
|
| 169 | } |
||
| 170 | 240 | $context->startVisiting($data); |
|
| 171 | 240 | } |
|
| 172 | |||
| 173 | // If we're serializing a polymorphic type, then we'll be interested in the |
||
| 174 | // metadata for the actual type of the object, not the base class. |
||
| 175 | 240 | if (class_exists($type['name'], false) || interface_exists($type['name'], false)) { |
|
| 176 | 240 | if (is_subclass_of($data, $type['name'], false)) { |
|
|
|
|||
| 177 | 6 | $type = array('name' => get_class($data), 'params' => array()); |
|
| 178 | 6 | } |
|
| 179 | 240 | } |
|
| 180 | 258 | } elseif ($context instanceof DeserializationContext) { |
|
| 181 | 74 | $context->increaseDepth(); |
|
| 182 | 74 | } |
|
| 183 | |||
| 184 | // Trigger pre-serialization callbacks, and listeners if they exist. |
||
| 185 | // Dispatch pre-serialization event before handling data to have ability change type in listener |
||
| 186 | 258 | if ($context instanceof SerializationContext) { |
|
| 187 | 240 | if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_serialize', $type['name'], $context->getFormat())) { |
|
| 188 | 235 | $this->dispatcher->dispatch('serializer.pre_serialize', $type['name'], $context->getFormat(), $event = new PreSerializeEvent($context, $data, $type)); |
|
| 189 | 235 | $type = $event->getType(); |
|
| 190 | 235 | } |
|
| 191 | 258 | } elseif ($context instanceof DeserializationContext) { |
|
| 192 | 74 | if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_deserialize', $type['name'], $context->getFormat())) { |
|
| 193 | $this->dispatcher->dispatch('serializer.pre_deserialize', $type['name'], $context->getFormat(), $event = new PreDeserializeEvent($context, $data, $type)); |
||
| 194 | $type = $event->getType(); |
||
| 195 | $data = $event->getData(); |
||
| 196 | } |
||
| 197 | 74 | } |
|
| 198 | |||
| 199 | // First, try whether a custom handler exists for the given type. This is done |
||
| 200 | // before loading metadata because the type name might not be a class, but |
||
| 201 | // could also simply be an artifical type. |
||
| 202 | 258 | if (null !== $handler = $this->handlerRegistry->getHandler($context->getDirection(), $type['name'], $context->getFormat())) { |
|
| 203 | 62 | $rs = call_user_func($handler, $visitor, $data, $type, $context); |
|
| 204 | 62 | $this->leaveScope($context, $data); |
|
| 205 | |||
| 206 | 62 | return $rs; |
|
| 207 | } |
||
| 208 | |||
| 209 | 230 | $exclusionStrategy = $context->getExclusionStrategy(); |
|
| 210 | |||
| 211 | /** @var $metadata ClassMetadata */ |
||
| 212 | 230 | $metadata = $this->metadataFactory->getMetadataForClass($type['name']); |
|
| 213 | |||
| 214 | 227 | if ($metadata->usingExpression && !$this->expressionExclusionStrategy) { |
|
| 215 | 3 | throw new ExpressionLanguageRequiredException("To use conditional exclude/expose in {$metadata->name} you must configure the expression language."); |
|
| 216 | } |
||
| 217 | |||
| 218 | 224 | if ($context instanceof DeserializationContext && !empty($metadata->discriminatorMap) && $type['name'] === $metadata->discriminatorBaseClass) { |
|
| 219 | 13 | $metadata = $this->resolveMetadata($data, $metadata); |
|
| 220 | 11 | } |
|
| 221 | |||
| 222 | 222 | if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipClass($metadata, $context)) { |
|
| 223 | 6 | $this->leaveScope($context, $data); |
|
| 224 | |||
| 225 | 6 | return null; |
|
| 226 | } |
||
| 227 | |||
| 228 | 222 | $context->pushClassMetadata($metadata); |
|
| 229 | |||
| 230 | 222 | if ($context instanceof SerializationContext) { |
|
| 231 | 207 | foreach ($metadata->preSerializeMethods as $method) { |
|
| 232 | 3 | $method->invoke($data); |
|
| 233 | 207 | } |
|
| 234 | 207 | } |
|
| 235 | |||
| 236 | 222 | $object = $data; |
|
| 237 | 222 | if ($context instanceof DeserializationContext) { |
|
| 238 | 63 | $object = $this->objectConstructor->construct($visitor, $metadata, $data, $type, $context); |
|
| 239 | 63 | } |
|
| 240 | |||
| 241 | 222 | if (isset($metadata->handlerCallbacks[$context->getDirection()][$context->getFormat()])) { |
|
| 242 | 3 | $rs = $object->{$metadata->handlerCallbacks[$context->getDirection()][$context->getFormat()]}( |
|
| 243 | 3 | $visitor, |
|
| 244 | 3 | $context instanceof SerializationContext ? null : $data, |
|
| 245 | $context |
||
| 246 | 3 | ); |
|
| 247 | 3 | $this->afterVisitingObject($metadata, $object, $type, $context); |
|
| 248 | |||
| 249 | 3 | return $context instanceof SerializationContext ? $rs : $object; |
|
| 250 | } |
||
| 251 | |||
| 252 | 219 | $visitor->startVisitingObject($metadata, $object, $type, $context); |
|
| 253 | 219 | foreach ($metadata->propertyMetadata as $propertyMetadata) { |
|
| 254 | 217 | if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipProperty($propertyMetadata, $context)) { |
|
| 255 | 17 | continue; |
|
| 256 | } |
||
| 257 | |||
| 258 | 217 | if (null !== $this->expressionExclusionStrategy && $this->expressionExclusionStrategy->shouldSkipProperty($propertyMetadata, $context)) { |
|
| 259 | 23 | continue; |
|
| 260 | } |
||
| 261 | |||
| 262 | 217 | if ($context instanceof DeserializationContext && $propertyMetadata->readOnly) { |
|
| 263 | 17 | continue; |
|
| 264 | } |
||
| 265 | |||
| 266 | 217 | $context->pushPropertyMetadata($propertyMetadata); |
|
| 267 | 217 | $visitor->visitProperty($propertyMetadata, $data, $context); |
|
| 268 | 213 | $context->popPropertyMetadata(); |
|
| 269 | 215 | } |
|
| 270 | |||
| 271 | 213 | if ($context instanceof SerializationContext) { |
|
| 272 | 199 | $this->afterVisitingObject($metadata, $data, $type, $context); |
|
| 273 | |||
| 274 | 199 | return $visitor->endVisitingObject($metadata, $data, $type, $context); |
|
| 275 | } |
||
| 276 | |||
| 277 | 63 | $rs = $visitor->endVisitingObject($metadata, $data, $type, $context); |
|
| 278 | 63 | $this->afterVisitingObject($metadata, $rs, $type, $context); |
|
| 279 | |||
| 280 | 63 | return $rs; |
|
| 281 | 258 | } |
|
| 282 | } |
||
| 283 | |||
| 284 | 13 | private function resolveMetadata($data, ClassMetadata $metadata) |
|
| 325 | |||
| 326 | 244 | private function leaveScope(Context $context, $data) |
|
| 334 | |||
| 335 | 216 | private function afterVisitingObject(ClassMetadata $metadata, $object, array $type, Context $context) |
|
| 360 | } |
||
| 361 |