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 | 147 | 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 | 91 | 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) |
|
| 129 | |||
| 130 | 84 | public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 139 | |||
| 140 | 83 | public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 156 | |||
| 157 | 80 | public function visitProperty(PropertyMetadata $metadata, $data, Context $context) |
|
| 158 | { |
||
| 159 | 80 | $v = $this->accessor->getValue($data, $metadata); |
|
| 160 | |||
| 161 | 79 | $v = $this->navigator->accept($v, $metadata->type, $context); |
|
| 162 | 79 | if ((null === $v && $context->shouldSerializeNull() !== true) |
|
| 163 | 77 | || (true === $metadata->skipWhenEmpty && ($v instanceof \ArrayObject || is_array($v)) && 0 === count($v)) |
|
| 164 | 79 | ) { |
|
| 165 | 17 | return; |
|
| 166 | } |
||
| 167 | |||
| 168 | 76 | if ($this->namingStrategy instanceof AdvancedNamingStrategyInterface) { |
|
| 169 | 1 | $k = $this->namingStrategy->getPropertyName($metadata, $context); |
|
| 170 | 1 | } else { |
|
| 171 | 75 | $k = $this->namingStrategy->translateName($metadata); |
|
| 172 | } |
||
| 173 | |||
| 174 | 76 | if ($metadata->inline) { |
|
| 175 | 3 | if (is_array($v) || ($v instanceof \ArrayObject)) { |
|
| 176 | 3 | $this->data = array_merge($this->data, (array) $v); |
|
| 177 | 3 | } |
|
| 178 | 3 | } else { |
|
| 179 | 75 | $this->data[$k] = $v; |
|
| 180 | } |
||
| 181 | 76 | } |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Allows you to add additional data to the current object/root element. |
||
| 185 | * @deprecated use setData instead |
||
| 186 | * @param string $key |
||
| 187 | * @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array. |
||
| 188 | * It must not contain any objects anymore. |
||
| 189 | */ |
||
| 190 | 1 | public function addData($key, $value) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Checks if some data key exists. |
||
| 201 | * |
||
| 202 | * @param string $key |
||
| 203 | * @return boolean |
||
| 204 | */ |
||
| 205 | 1 | public function hasData($key) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Allows you to replace existing data on the current object/root element. |
||
| 212 | * |
||
| 213 | * @param string $key |
||
| 214 | * @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array. |
||
| 215 | * It must not contain any objects anymore. |
||
| 216 | */ |
||
| 217 | 1 | public function setData($key, $value) |
|
| 221 | |||
| 222 | 148 | public function getRoot() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @param array|\ArrayObject $data the passed data must be understood by whatever encoding function is applied later. |
||
| 229 | */ |
||
| 230 | 8 | public function setRoot($data) |
|
| 234 | |||
| 235 | |||
| 236 | 136 | public function getResult() |
|
| 251 | |||
| 252 | public function getOptions() |
||
| 256 | |||
| 257 | public function setOptions($options) |
||
| 261 | } |
||
| 262 |
This class, trait or interface has been deprecated.