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 |
||
| 63 | class SerializerBuilder |
||
| 64 | { |
||
| 65 | private $metadataDirs = array(); |
||
| 66 | private $handlerRegistry; |
||
| 67 | private $handlersConfigured = false; |
||
| 68 | private $eventDispatcher; |
||
| 69 | private $listenersConfigured = false; |
||
| 70 | private $objectConstructor; |
||
| 71 | private $serializationVisitors; |
||
| 72 | private $deserializationVisitors; |
||
| 73 | private $visitorsAdded = false; |
||
| 74 | private $propertyNamingStrategy; |
||
| 75 | private $advancedNamingStrategy; |
||
| 76 | private $debug = false; |
||
| 77 | private $cacheDir; |
||
| 78 | private $annotationReader; |
||
| 79 | private $includeInterfaceMetadata = false; |
||
| 80 | private $driverFactory; |
||
| 81 | private $serializationContextFactory; |
||
| 82 | private $deserializationContextFactory; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var ExpressionEvaluatorInterface |
||
| 86 | */ |
||
| 87 | private $expressionEvaluator; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var AccessorStrategyInterface |
||
| 91 | */ |
||
| 92 | private $accessorStrategy; |
||
| 93 | |||
| 94 | 29 | public static function create() |
|
| 95 | { |
||
| 96 | 29 | return new static(); |
|
| 97 | } |
||
| 98 | |||
| 99 | 29 | public function __construct() |
|
| 100 | { |
||
| 101 | 29 | $this->handlerRegistry = new HandlerRegistry(); |
|
| 102 | 29 | $this->eventDispatcher = new EventDispatcher(); |
|
| 103 | 29 | $this->driverFactory = new DefaultDriverFactory(); |
|
| 104 | 29 | $this->serializationVisitors = new Map(); |
|
| 105 | 29 | $this->deserializationVisitors = new Map(); |
|
| 106 | 29 | } |
|
| 107 | |||
| 108 | public function setAccessorStrategy(AccessorStrategyInterface $accessorStrategy) |
||
| 109 | { |
||
| 110 | $this->accessorStrategy = $accessorStrategy; |
||
| 111 | } |
||
| 112 | |||
| 113 | 28 | protected function getAccessorStrategy() |
|
| 114 | { |
||
| 115 | 28 | if (!$this->accessorStrategy) { |
|
| 116 | 28 | $this->accessorStrategy = new DefaultAccessorStrategy(); |
|
| 117 | |||
| 118 | 28 | if ($this->expressionEvaluator) { |
|
| 119 | 3 | $this->accessorStrategy = new ExpressionAccessorStrategy($this->expressionEvaluator, $this->accessorStrategy); |
|
| 120 | 3 | } |
|
| 121 | 28 | } |
|
| 122 | 28 | return $this->accessorStrategy; |
|
| 123 | } |
||
| 124 | |||
| 125 | 3 | public function setExpressionEvaluator(ExpressionEvaluatorInterface $expressionEvaluator) |
|
| 126 | { |
||
| 127 | 3 | $this->expressionEvaluator = $expressionEvaluator; |
|
| 128 | |||
| 129 | 3 | return $this; |
|
| 130 | } |
||
| 131 | |||
| 132 | public function setAnnotationReader(Reader $reader) |
||
| 133 | { |
||
| 134 | $this->annotationReader = $reader; |
||
| 135 | |||
| 136 | return $this; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function setDebug($bool) |
||
| 140 | { |
||
| 141 | $this->debug = (boolean)$bool; |
||
| 142 | |||
| 143 | return $this; |
||
| 144 | } |
||
| 145 | |||
| 146 | 1 | public function setCacheDir($dir) |
|
| 147 | { |
||
| 148 | 1 | if (!is_dir($dir)) { |
|
| 149 | 1 | $this->createDir($dir); |
|
| 150 | 1 | } |
|
| 151 | 1 | if (!is_writable($dir)) { |
|
| 152 | throw new InvalidArgumentException(sprintf('The cache directory "%s" is not writable.', $dir)); |
||
| 153 | } |
||
| 154 | |||
| 155 | 1 | $this->cacheDir = $dir; |
|
| 156 | |||
| 157 | 1 | return $this; |
|
| 158 | } |
||
| 159 | |||
| 160 | 28 | public function addDefaultHandlers() |
|
| 161 | { |
||
| 162 | 28 | $this->handlersConfigured = true; |
|
| 163 | 28 | $this->handlerRegistry->registerSubscribingHandler(new DateHandler()); |
|
| 164 | 28 | $this->handlerRegistry->registerSubscribingHandler(new StdClassHandler()); |
|
| 165 | 28 | $this->handlerRegistry->registerSubscribingHandler(new PhpCollectionHandler()); |
|
| 166 | 28 | $this->handlerRegistry->registerSubscribingHandler(new ArrayCollectionHandler()); |
|
| 167 | 28 | $this->handlerRegistry->registerSubscribingHandler(new PropelCollectionHandler()); |
|
| 168 | |||
| 169 | 28 | return $this; |
|
| 170 | } |
||
| 171 | |||
| 172 | 1 | public function configureHandlers(\Closure $closure) |
|
| 179 | |||
| 180 | 28 | public function addDefaultListeners() |
|
| 187 | |||
| 188 | 1 | public function configureListeners(\Closure $closure) |
|
| 195 | |||
| 196 | 1 | public function setObjectConstructor(ObjectConstructorInterface $constructor) |
|
| 202 | |||
| 203 | 1 | public function setPropertyNamingStrategy(PropertyNamingStrategyInterface $propertyNamingStrategy) |
|
| 209 | |||
| 210 | public function setAdvancedNamingStrategy(AdvancedNamingStrategyInterface $advancedNamingStrategy) |
||
| 211 | { |
||
| 212 | $this->advancedNamingStrategy = $advancedNamingStrategy; |
||
| 213 | |||
| 214 | return $this; |
||
| 215 | } |
||
| 216 | |||
| 217 | 1 | public function setSerializationVisitor($format, VisitorInterface $visitor) |
|
| 218 | { |
||
| 219 | 1 | $this->visitorsAdded = true; |
|
| 224 | |||
| 225 | public function setDeserializationVisitor($format, VisitorInterface $visitor) |
||
| 232 | |||
| 233 | 28 | public function addDefaultSerializationVisitors() |
|
| 258 | |||
| 259 | 28 | public function addDefaultDeserializationVisitors() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param Boolean $include Whether to include the metadata from the interfaces |
||
| 282 | * |
||
| 283 | * @return SerializerBuilder |
||
| 284 | */ |
||
| 285 | 1 | public function includeInterfaceMetadata($include) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Sets a map of namespace prefixes to directories. |
||
| 294 | * |
||
| 295 | * This method overrides any previously defined directories. |
||
| 296 | * |
||
| 297 | * @param array <string,string> $namespacePrefixToDirMap |
||
| 298 | * |
||
| 299 | * @return SerializerBuilder |
||
| 300 | * |
||
| 301 | * @throws InvalidArgumentException When a directory does not exist |
||
| 302 | */ |
||
| 303 | public function setMetadataDirs(array $namespacePrefixToDirMap) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Adds a directory where the serializer will look for class metadata. |
||
| 318 | * |
||
| 319 | * The namespace prefix will make the names of the actual metadata files a bit shorter. For example, let's assume |
||
| 320 | * that you have a directory where you only store metadata files for the ``MyApplication\Entity`` namespace. |
||
| 321 | * |
||
| 322 | * If you use an empty prefix, your metadata files would need to look like: |
||
| 323 | * |
||
| 324 | * ``my-dir/MyApplication.Entity.SomeObject.yml`` |
||
| 325 | * ``my-dir/MyApplication.Entity.OtherObject.xml`` |
||
| 326 | * |
||
| 327 | * If you use ``MyApplication\Entity`` as prefix, your metadata files would need to look like: |
||
| 328 | * |
||
| 329 | * ``my-dir/SomeObject.yml`` |
||
| 330 | * ``my-dir/OtherObject.yml`` |
||
| 331 | * |
||
| 332 | * Please keep in mind that you currently may only have one directory per namespace prefix. |
||
| 333 | * |
||
| 334 | * @param string $dir The directory where metadata files are located. |
||
| 335 | * @param string $namespacePrefix An optional prefix if you only store metadata for specific namespaces in this directory. |
||
| 336 | * |
||
| 337 | * @return SerializerBuilder |
||
| 338 | * |
||
| 339 | * @throws InvalidArgumentException When a directory does not exist |
||
| 340 | * @throws InvalidArgumentException When a directory has already been registered |
||
| 341 | */ |
||
| 342 | public function addMetadataDir($dir, $namespacePrefix = '') |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Adds a map of namespace prefixes to directories. |
||
| 359 | * |
||
| 360 | * @param array <string,string> $namespacePrefixToDirMap |
||
| 361 | * |
||
| 362 | * @return SerializerBuilder |
||
| 363 | */ |
||
| 364 | public function addMetadataDirs(array $namespacePrefixToDirMap) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Similar to addMetadataDir(), but overrides an existing entry. |
||
| 375 | * |
||
| 376 | * @param string $dir |
||
| 377 | * @param string $namespacePrefix |
||
| 378 | * |
||
| 379 | * @return SerializerBuilder |
||
| 380 | * |
||
| 381 | * @throws InvalidArgumentException When a directory does not exist |
||
| 382 | * @throws InvalidArgumentException When no directory is configured for the ns prefix |
||
| 383 | */ |
||
| 384 | public function replaceMetadataDir($dir, $namespacePrefix = '') |
||
| 398 | |||
| 399 | 12 | public function setMetadataDriverFactory(DriverFactoryInterface $driverFactory) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @param SerializationContextFactoryInterface|callable $serializationContextFactory |
||
| 408 | * |
||
| 409 | * @return self |
||
| 410 | */ |
||
| 411 | 3 | public function setSerializationContextFactory($serializationContextFactory) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * @param DeserializationContextFactoryInterface|callable $deserializationContextFactory |
||
| 428 | * |
||
| 429 | * @return self |
||
| 430 | */ |
||
| 431 | 1 | public function setDeserializationContextFactory($deserializationContextFactory) |
|
| 445 | |||
| 446 | 29 | public function build() |
|
| 503 | |||
| 504 | 28 | private function initializePropertyNamingStrategy() |
|
| 512 | |||
| 513 | 1 | private function createDir($dir) |
|
| 523 | } |
||
| 524 |