Complex classes like Converter 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 Converter, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 23 | class Converter | ||
| 24 | { | ||
| 25 | /** | ||
| 26 | * @var array | ||
| 27 | */ | ||
| 28 | private $typesMapping; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @var array | ||
| 32 | */ | ||
| 33 | private $bundlesMapping; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @var PropertyAccessor | ||
| 37 | */ | ||
| 38 | private $propertyAccessor; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * Constructor. | ||
| 42 | * | ||
| 43 | * @param array $typesMapping | ||
| 44 | * @param array $bundlesMapping | ||
| 45 | */ | ||
| 46 | public function __construct($typesMapping, $bundlesMapping) | ||
| 47 |     { | ||
| 48 | $this->typesMapping = $typesMapping; | ||
| 49 | $this->bundlesMapping = $bundlesMapping; | ||
| 50 | } | ||
| 51 | |||
| 52 | /** | ||
| 53 | * Converts raw array to document. | ||
| 54 | * | ||
| 55 | * @param array $rawData | ||
| 56 | * | ||
| 57 | * @return DocumentInterface | ||
| 58 | * | ||
| 59 | * @throws \LogicException | ||
| 60 | */ | ||
| 61 | public function convertToDocument($rawData) | ||
| 62 |     { | ||
| 63 |         if (!isset($this->typesMapping[$rawData['_type']])) { | ||
| 64 |             throw new \LogicException("Got document of unknown type '{$rawData['_type']}'."); | ||
| 65 | } | ||
| 66 | |||
| 67 | /** @var ClassMetadata $metadata */ | ||
| 68 | $metadata = $this->bundlesMapping[$this->typesMapping[$rawData['_type']]]; | ||
| 69 |         $data = isset($rawData['_source']) ? $rawData['_source'] : array_map('reset', $rawData['fields']); | ||
| 70 | $proxy = $metadata->getProxyNamespace(); | ||
| 71 | |||
| 72 | /** @var DocumentInterface $object */ | ||
| 73 | $object = $this->assignArrayToObject($data, new $proxy(), $metadata->getAliases()); | ||
| 74 | |||
| 75 |         if ($object instanceof ProxyInterface) { | ||
| 76 | $object->__setInitialized(true); | ||
| 77 | } | ||
| 78 | |||
| 79 | $this->setObjectFields($object, $rawData, ['_id', '_score', 'highlight', 'fields _parent', 'fields _ttl']); | ||
| 80 | |||
| 81 | return $object; | ||
| 82 | } | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Assigns all properties to object. | ||
| 86 | * | ||
| 87 | * @param array $array | ||
| 88 | * @param object $object | ||
| 89 | * @param array $aliases | ||
| 90 | * | ||
| 91 | * @return object | ||
| 92 | */ | ||
| 93 | public function assignArrayToObject(array $array, $object, array $aliases) | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Converts object to an array. | ||
| 130 | * | ||
| 131 | * @param DocumentInterface $object | ||
| 132 | * @param array $aliases | ||
| 133 | * | ||
| 134 | * @return array | ||
| 135 | */ | ||
| 136 | public function convertToArray($object, $aliases = []) | ||
| 178 | |||
| 179 | /** | ||
| 180 | * Sets fields into object from raw response. | ||
| 181 | * | ||
| 182 | * @param object $object Object to set values to. | ||
| 183 | * @param array $rawResponse Array to take values from. | ||
| 184 | * @param array $fields Values to take. | ||
| 185 | */ | ||
| 186 | private function setObjectFields($object, $rawResponse, $fields = []) | ||
| 201 | |||
| 202 | /** | ||
| 203 | * Sets fields into array from object. | ||
| 204 | * | ||
| 205 | * @param array $array To set values to. | ||
| 206 | * @param object $object Take values from. | ||
| 207 | * @param array $fields Fields to set. | ||
| 208 | */ | ||
| 209 | private function setArrayFields(&$array, $object, $fields = []) | ||
| 221 | |||
| 222 | /** | ||
| 223 | * Returns property to access for object used by property accessor. | ||
| 224 | * | ||
| 225 | * @param string $field | ||
| 226 | * | ||
| 227 | * @return string | ||
| 228 | */ | ||
| 229 | private function getPropertyToAccess($field) | ||
| 238 | |||
| 239 | /** | ||
| 240 | * Returns property to access for array used by property accessor. | ||
| 241 | * | ||
| 242 | * @param string $field | ||
| 243 | * | ||
| 244 | * @return string | ||
| 245 | */ | ||
| 246 | private function getPropertyPathToAccess($field) | ||
| 250 | |||
| 251 | /** | ||
| 252 | * Check if class matches the expected one. | ||
| 253 | * | ||
| 254 | * @param object $object | ||
| 255 | * @param array $expectedClasses | ||
| 256 | * | ||
| 257 | * @throws \InvalidArgumentException | ||
| 258 | */ | ||
| 259 | private function checkVariableType($object, array $expectedClasses) | ||
| 271 | |||
| 272 | /** | ||
| 273 | * Check if object is traversable, throw exception otherwise. | ||
| 274 | * | ||
| 275 | * @param mixed $value | ||
| 276 | * | ||
| 277 | * @return bool | ||
| 278 | * | ||
| 279 | * @throws \InvalidArgumentException | ||
| 280 | */ | ||
| 281 | private function isTraversable($value) | ||
| 289 | |||
| 290 | /** | ||
| 291 | * Returns aliases for certain document. | ||
| 292 | * | ||
| 293 | * @param DocumentInterface $document | ||
| 294 | * | ||
| 295 | * @return array | ||
| 296 | * | ||
| 297 | * @throws \DomainException | ||
| 298 | */ | ||
| 299 | private function getAlias($document) | ||
| 311 | |||
| 312 | /** | ||
| 313 | * Returns property accessor instance. | ||
| 314 | * | ||
| 315 | * @return PropertyAccessor | ||
| 316 | */ | ||
| 317 | private function getPropertyAccessor() | ||
| 327 | } | ||
| 328 |