Complex classes like SerializerBuilder 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 SerializerBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 62 | class SerializerBuilder |
||
| 63 | { |
||
| 64 | private $metadataDirs = array(); |
||
| 65 | private $handlerRegistry; |
||
| 66 | private $handlersConfigured = false; |
||
| 67 | private $eventDispatcher; |
||
| 68 | private $listenersConfigured = false; |
||
| 69 | private $objectConstructor; |
||
| 70 | private $serializationVisitors; |
||
| 71 | private $deserializationVisitors; |
||
| 72 | private $visitorsAdded = false; |
||
| 73 | private $propertyNamingStrategy; |
||
| 74 | private $debug = false; |
||
| 75 | private $cacheDir; |
||
| 76 | private $annotationReader; |
||
| 77 | private $includeInterfaceMetadata = false; |
||
| 78 | private $driverFactory; |
||
| 79 | private $serializationContextFactory; |
||
| 80 | private $deserializationContextFactory; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var ExpressionEvaluatorInterface |
||
| 84 | */ |
||
| 85 | private $expressionEvaluator; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var AccessorStrategyInterface |
||
| 89 | */ |
||
| 90 | private $accessorStrategy; |
||
| 91 | |||
| 92 | 28 | public static function create() |
|
| 96 | |||
| 97 | 28 | public function __construct() |
|
| 98 | 1 | { |
|
| 99 | 28 | $this->handlerRegistry = new HandlerRegistry(); |
|
| 100 | 28 | $this->eventDispatcher = new EventDispatcher(); |
|
| 101 | 28 | $this->driverFactory = new DefaultDriverFactory(); |
|
| 102 | 28 | $this->serializationVisitors = new Map(); |
|
| 103 | 28 | $this->deserializationVisitors = new Map(); |
|
| 104 | 28 | } |
|
| 105 | |||
| 106 | public function setAccessorStrategy(AccessorStrategyInterface $accessorStrategy) |
||
| 110 | |||
| 111 | 27 | protected function getAccessorStrategy() |
|
| 122 | |||
| 123 | 2 | public function setExpressionEvaluator(ExpressionEvaluatorInterface $expressionEvaluator) |
|
| 129 | |||
| 130 | public function setAnnotationReader(Reader $reader) |
||
| 136 | |||
| 137 | public function setDebug($bool) |
||
| 143 | |||
| 144 | 1 | public function setCacheDir($dir) |
|
| 157 | |||
| 158 | 27 | public function addDefaultHandlers() |
|
| 169 | |||
| 170 | 1 | public function configureHandlers(\Closure $closure) |
|
| 177 | |||
| 178 | 27 | public function addDefaultListeners() |
|
| 185 | |||
| 186 | 1 | public function configureListeners(\Closure $closure) |
|
| 193 | |||
| 194 | 1 | public function setObjectConstructor(ObjectConstructorInterface $constructor) |
|
| 200 | |||
| 201 | 1 | public function setPropertyNamingStrategy(PropertyNamingStrategyInterface $propertyNamingStrategy) |
|
| 207 | |||
| 208 | 1 | public function setSerializationVisitor($format, VisitorInterface $visitor) |
|
| 215 | |||
| 216 | public function setDeserializationVisitor($format, VisitorInterface $visitor) |
||
| 223 | |||
| 224 | 27 | public function addDefaultSerializationVisitors() |
|
| 237 | |||
| 238 | 27 | public function addDefaultDeserializationVisitors() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @param Boolean $include Whether to include the metadata from the interfaces |
||
| 253 | * |
||
| 254 | * @return SerializerBuilder |
||
| 255 | */ |
||
| 256 | 1 | public function includeInterfaceMetadata($include) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Sets a map of namespace prefixes to directories. |
||
| 265 | * |
||
| 266 | * This method overrides any previously defined directories. |
||
| 267 | * |
||
| 268 | * @param array <string,string> $namespacePrefixToDirMap |
||
| 269 | * |
||
| 270 | * @return SerializerBuilder |
||
| 271 | * |
||
| 272 | * @throws InvalidArgumentException When a directory does not exist |
||
| 273 | */ |
||
| 274 | public function setMetadataDirs(array $namespacePrefixToDirMap) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Adds a directory where the serializer will look for class metadata. |
||
| 289 | * |
||
| 290 | * The namespace prefix will make the names of the actual metadata files a bit shorter. For example, let's assume |
||
| 291 | * that you have a directory where you only store metadata files for the ``MyApplication\Entity`` namespace. |
||
| 292 | * |
||
| 293 | * If you use an empty prefix, your metadata files would need to look like: |
||
| 294 | * |
||
| 295 | * ``my-dir/MyApplication.Entity.SomeObject.yml`` |
||
| 296 | * ``my-dir/MyApplication.Entity.OtherObject.xml`` |
||
| 297 | * |
||
| 298 | * If you use ``MyApplication\Entity`` as prefix, your metadata files would need to look like: |
||
| 299 | * |
||
| 300 | * ``my-dir/SomeObject.yml`` |
||
| 301 | * ``my-dir/OtherObject.yml`` |
||
| 302 | * |
||
| 303 | * Please keep in mind that you currently may only have one directory per namespace prefix. |
||
| 304 | * |
||
| 305 | * @param string $dir The directory where metadata files are located. |
||
| 306 | * @param string $namespacePrefix An optional prefix if you only store metadata for specific namespaces in this directory. |
||
| 307 | * |
||
| 308 | * @return SerializerBuilder |
||
| 309 | * |
||
| 310 | * @throws InvalidArgumentException When a directory does not exist |
||
| 311 | * @throws InvalidArgumentException When a directory has already been registered |
||
| 312 | */ |
||
| 313 | public function addMetadataDir($dir, $namespacePrefix = '') |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Adds a map of namespace prefixes to directories. |
||
| 330 | * |
||
| 331 | * @param array <string,string> $namespacePrefixToDirMap |
||
| 332 | * |
||
| 333 | * @return SerializerBuilder |
||
| 334 | */ |
||
| 335 | public function addMetadataDirs(array $namespacePrefixToDirMap) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Similar to addMetadataDir(), but overrides an existing entry. |
||
| 346 | * |
||
| 347 | * @param string $dir |
||
| 348 | * @param string $namespacePrefix |
||
| 349 | * |
||
| 350 | * @return SerializerBuilder |
||
| 351 | * |
||
| 352 | * @throws InvalidArgumentException When a directory does not exist |
||
| 353 | * @throws InvalidArgumentException When no directory is configured for the ns prefix |
||
| 354 | */ |
||
| 355 | public function replaceMetadataDir($dir, $namespacePrefix = '') |
||
| 369 | |||
| 370 | 12 | public function setMetadataDriverFactory(DriverFactoryInterface $driverFactory) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @param SerializationContextFactoryInterface|callable $serializationContextFactory |
||
| 379 | * |
||
| 380 | * @return self |
||
| 381 | */ |
||
| 382 | 3 | public function setSerializationContextFactory($serializationContextFactory) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @param DeserializationContextFactoryInterface|callable $deserializationContextFactory |
||
| 399 | * |
||
| 400 | * @return self |
||
| 401 | */ |
||
| 402 | 1 | public function setDeserializationContextFactory($deserializationContextFactory) |
|
| 416 | |||
| 417 | 28 | public function build() |
|
| 474 | |||
| 475 | 27 | private function initializePropertyNamingStrategy() |
|
| 483 | |||
| 484 | 1 | private function createDir($dir) |
|
| 494 | } |
||
| 495 |