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 |
||
| 25 | class JsonSerializationVisitor extends GenericSerializationVisitor |
||
|
|
|||
| 26 | { |
||
| 27 | private $options = 0; |
||
| 28 | |||
| 29 | private $navigator; |
||
| 30 | private $root; |
||
| 31 | private $dataStack; |
||
| 32 | private $data; |
||
| 33 | |||
| 34 | 144 | public function setNavigator(GraphNavigator $navigator) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @return GraphNavigator |
||
| 43 | */ |
||
| 44 | public function getNavigator() |
||
| 48 | |||
| 49 | 28 | public function visitNull($data, array $type, Context $context) |
|
| 53 | |||
| 54 | 88 | public function visitString($data, array $type, Context $context) |
|
| 62 | |||
| 63 | 9 | public function visitBoolean($data, array $type, Context $context) |
|
| 71 | |||
| 72 | 26 | public function visitInteger($data, array $type, Context $context) |
|
| 80 | |||
| 81 | 13 | public function visitDouble($data, array $type, Context $context) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @param array $data |
||
| 92 | * @param array $type |
||
| 93 | * @param Context $context |
||
| 94 | * @return mixed |
||
| 95 | */ |
||
| 96 | 70 | public function visitArray($data, array $type, Context $context) |
|
| 97 | { |
||
| 98 | 70 | $this->dataStack->push($data); |
|
| 99 | |||
| 100 | 70 | $isHash = isset($type['params'][1]); |
|
| 101 | |||
| 102 | 70 | if (null === $this->root) { |
|
| 103 | 49 | $this->root = $isHash ? new \ArrayObject() : array(); |
|
| 104 | 49 | $rs = &$this->root; |
|
| 105 | 49 | } else { |
|
| 106 | 23 | $rs = $isHash ? new \ArrayObject() : array(); |
|
| 107 | } |
||
| 108 | |||
| 109 | 70 | $isList = isset($type['params'][0]) && ! isset($type['params'][1]); |
|
| 110 | |||
| 111 | 70 | foreach ($data as $k => $v) { |
|
| 112 | 64 | $v = $this->navigator->accept($v, $this->getElementType($type), $context); |
|
| 113 | |||
| 114 | 64 | if (null === $v && $context->shouldSerializeNull() !== true) { |
|
| 115 | 3 | continue; |
|
| 116 | } |
||
| 117 | |||
| 118 | 64 | if ($isList) { |
|
| 119 | 17 | $rs[] = $v; |
|
| 120 | 17 | } else { |
|
| 121 | 51 | $rs[$k] = $v; |
|
| 122 | } |
||
| 123 | 70 | } |
|
| 124 | |||
| 125 | 70 | $this->dataStack->pop(); |
|
| 126 | 70 | return $rs; |
|
| 127 | } |
||
| 128 | |||
| 129 | 81 | public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 130 | { |
||
| 131 | 81 | if (null === $this->root) { |
|
| 132 | 66 | $this->root = new \stdClass; |
|
| 133 | 66 | } |
|
| 134 | |||
| 135 | 81 | $this->dataStack->push($this->data); |
|
| 136 | 81 | $this->data = array(); |
|
| 137 | 81 | } |
|
| 138 | |||
| 139 | 80 | public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
| 140 | { |
||
| 141 | 80 | $rs = $this->data; |
|
| 142 | 80 | $this->data = $this->dataStack->pop(); |
|
| 143 | |||
| 144 | // Force JSON output to "{}" instead of "[]" if it contains either no properties or all properties are null. |
||
| 145 | 80 | if (empty($rs)) { |
|
| 146 | 11 | $rs = new \ArrayObject(); |
|
| 147 | 11 | } |
|
| 148 | |||
| 149 | 80 | if ($this->root instanceof \stdClass && 0 === $this->dataStack->count()) { |
|
| 150 | 65 | $this->root = $rs; |
|
| 151 | 65 | } |
|
| 152 | |||
| 153 | 80 | return $rs; |
|
| 154 | } |
||
| 155 | |||
| 156 | 77 | public function visitProperty(PropertyMetadata $metadata, $data, Context $context) |
|
| 157 | { |
||
| 158 | 77 | $v = $this->accessor->getValue($data, $metadata); |
|
| 159 | |||
| 160 | 76 | $v = $this->navigator->accept($v, $metadata->type, $context); |
|
| 161 | 76 | if ((null === $v && $context->shouldSerializeNull() !== true) |
|
| 162 | 74 | || (true === $metadata->skipWhenEmpty && ($v instanceof \ArrayObject || is_array($v)) && 0 === count($v)) |
|
| 163 | 76 | ) { |
|
| 164 | 16 | return; |
|
| 165 | } |
||
| 166 | |||
| 167 | 73 | $k = $this->namingStrategy->translateName($metadata); |
|
| 168 | |||
| 169 | 73 | if ($metadata->inline) { |
|
| 170 | 2 | if (is_array($v)) { |
|
| 171 | 1 | $this->data = array_merge($this->data, $v); |
|
| 172 | 1 | } |
|
| 173 | 2 | } else { |
|
| 174 | 73 | $this->data[$k] = $v; |
|
| 175 | } |
||
| 176 | 73 | } |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Allows you to add additional data to the current object/root element. |
||
| 180 | * @deprecated use setData instead |
||
| 181 | * @param string $key |
||
| 182 | * @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array. |
||
| 183 | * It must not contain any objects anymore. |
||
| 184 | */ |
||
| 185 | 1 | public function addData($key, $value) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Checks if some data key exists. |
||
| 196 | * |
||
| 197 | * @param string $key |
||
| 198 | * @return boolean |
||
| 199 | */ |
||
| 200 | 1 | public function hasData($key) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Allows you to replace existing data on the current object/root element. |
||
| 207 | * |
||
| 208 | * @param string $key |
||
| 209 | * @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array. |
||
| 210 | * It must not contain any objects anymore. |
||
| 211 | */ |
||
| 212 | 1 | public function setData($key, $value) |
|
| 216 | |||
| 217 | 144 | public function getRoot() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @param array|\ArrayObject $data the passed data must be understood by whatever encoding function is applied later. |
||
| 224 | */ |
||
| 225 | 7 | public function setRoot($data) |
|
| 229 | |||
| 230 | |||
| 231 | 133 | public function getResult() |
|
| 246 | |||
| 247 | public function getOptions() |
||
| 251 | |||
| 252 | public function setOptions($options) |
||
| 256 | } |
||
| 257 |
This class, trait or interface has been deprecated.