Complex classes like JsonSerializationVisitor 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 JsonSerializationVisitor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class JsonSerializationVisitor extends GenericSerializationVisitor |
||
|
|
|||
| 27 | { |
||
| 28 | private $options = 0; |
||
| 29 | |||
| 30 | private $navigator; |
||
| 31 | private $root; |
||
| 32 | private $dataStack; |
||
| 33 | private $data; |
||
| 34 | |||
| 35 | 148 | public function setNavigator(GraphNavigator $navigator) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @return GraphNavigator |
||
| 44 | */ |
||
| 45 | public function getNavigator() |
||
| 49 | |||
| 50 | 29 | public function visitNull($data, array $type, Context $context) |
|
| 54 | |||
| 55 | 92 | public function visitString($data, array $type, Context $context) |
|
| 63 | |||
| 64 | 9 | public function visitBoolean($data, array $type, Context $context) |
|
| 72 | |||
| 73 | 26 | public function visitInteger($data, array $type, Context $context) |
|
| 81 | |||
| 82 | 13 | public function visitDouble($data, array $type, Context $context) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @param array $data |
||
| 93 | * @param array $type |
||
| 94 | * @param Context $context |
||
| 95 | * @return mixed |
||
| 96 | */ |
||
| 97 | 71 | public function visitArray($data, array $type, Context $context) |
|
| 98 | { |
||
| 99 | 71 | $this->dataStack->push($data); |
|
| 100 | |||
| 101 | 71 | $isHash = isset($type['params'][1]); |
|
| 102 | |||
| 103 | 71 | if (null === $this->root) { |
|
| 104 | 49 | $this->root = $isHash ? new \ArrayObject() : array(); |
|
| 105 | 49 | $rs = &$this->root; |
|
| 106 | } else { |
||
| 107 | 24 | $rs = $isHash ? new \ArrayObject() : array(); |
|
| 108 | } |
||
| 109 | |||
| 110 | 71 | $isList = isset($type['params'][0]) && !isset($type['params'][1]); |
|
| 111 | |||
| 112 | 71 | foreach ($data as $k => $v) { |
|
| 113 | 65 | $v = $this->navigator->accept($v, $this->getElementType($type), $context); |
|
| 114 | |||
| 115 | 65 | if (null === $v && $context->shouldSerializeNull() !== true) { |
|
| 116 | 3 | continue; |
|
| 117 | } |
||
| 118 | |||
| 119 | 65 | if ($isList) { |
|
| 120 | 17 | $rs[] = $v; |
|
| 121 | } else { |
||
| 122 | 65 | $rs[$k] = $v; |
|
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | 71 | $this->dataStack->pop(); |
|
| 127 | 71 | return $rs; |
|
| 128 | } |
||
| 129 | |||
| 130 | 85 | public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 139 | |||
| 140 | 84 | public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 156 | |||
| 157 | 81 | public function visitProperty(PropertyMetadata $metadata, $data, Context $context) |
|
| 158 | { |
||
| 159 | 81 | $v = $this->accessor->getValue($data, $metadata); |
|
| 160 | |||
| 161 | 80 | $v = $this->navigator->accept($v, $metadata->type, $context); |
|
| 162 | 80 | if (null === $v && $context->shouldSerializeNull() !== true){ |
|
| 163 | 16 | return; |
|
| 164 | } |
||
| 165 | |||
| 166 | 78 | if (true === $metadata->skipWhenEmpty) { |
|
| 167 | 3 | if (($v instanceof \ArrayObject || \is_array($v)) && 0 === count($v)) { |
|
| 168 | 3 | return; |
|
| 169 | } |
||
| 170 | 1 | if ('' === $v) { |
|
| 171 | 1 | return; |
|
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | 76 | if ($this->namingStrategy instanceof AdvancedNamingStrategyInterface) { |
|
| 176 | 1 | $k = $this->namingStrategy->getPropertyName($metadata, $context); |
|
| 177 | } else { |
||
| 178 | 75 | $k = $this->namingStrategy->translateName($metadata); |
|
| 179 | } |
||
| 180 | |||
| 181 | 76 | if ($metadata->inline) { |
|
| 182 | 3 | if (\is_array($v) || ($v instanceof \ArrayObject)) { |
|
| 183 | 3 | $this->data = array_merge($this->data, (array) $v); |
|
| 184 | } |
||
| 185 | } else { |
||
| 186 | 75 | $this->data[$k] = $v; |
|
| 187 | } |
||
| 188 | 76 | } |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Allows you to add additional data to the current object/root element. |
||
| 192 | * @deprecated use setData instead |
||
| 193 | * @param string $key |
||
| 194 | * @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array. |
||
| 195 | * It must not contain any objects anymore. |
||
| 196 | */ |
||
| 197 | 1 | public function addData($key, $value) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Checks if some data key exists. |
||
| 208 | * |
||
| 209 | * @param string $key |
||
| 210 | * @return boolean |
||
| 211 | */ |
||
| 212 | 1 | public function hasData($key) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Allows you to replace existing data on the current object/root element. |
||
| 219 | * |
||
| 220 | * @param string $key |
||
| 221 | * @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array. |
||
| 222 | * It must not contain any objects anymore. |
||
| 223 | */ |
||
| 224 | 1 | public function setData($key, $value) |
|
| 228 | |||
| 229 | 149 | public function getRoot() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @param array|\ArrayObject $data the passed data must be understood by whatever encoding function is applied later. |
||
| 236 | */ |
||
| 237 | 8 | public function setRoot($data) |
|
| 241 | |||
| 242 | |||
| 243 | 137 | public function getResult() |
|
| 258 | |||
| 259 | public function getOptions() |
||
| 263 | |||
| 264 | public function setOptions($options) |
||
| 268 | } |
||
| 269 |
This class, trait or interface has been deprecated.