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 $debug = false; |
||
| 76 | private $cacheDir; |
||
| 77 | private $annotationReader; |
||
| 78 | private $includeInterfaceMetadata = false; |
||
| 79 | private $driverFactory; |
||
| 80 | private $serializationContextFactory; |
||
| 81 | private $deserializationContextFactory; |
||
| 82 | |||
| 83 | 15 | public static function create() |
|
| 87 | |||
| 88 | 15 | public function __construct() |
|
| 89 | { |
||
| 90 | 15 | $this->handlerRegistry = new HandlerRegistry(); |
|
| 91 | 15 | $this->eventDispatcher = new EventDispatcher(); |
|
| 92 | 15 | $this->driverFactory = new DefaultDriverFactory(); |
|
| 93 | 15 | $this->serializationVisitors = new Map(); |
|
| 94 | 15 | $this->deserializationVisitors = new Map(); |
|
| 95 | 15 | } |
|
| 96 | |||
| 97 | public function setAnnotationReader(Reader $reader) |
||
| 103 | |||
| 104 | public function setDebug($bool) |
||
| 110 | |||
| 111 | 1 | public function setCacheDir($dir) |
|
| 124 | |||
| 125 | 14 | public function addDefaultHandlers() |
|
| 135 | |||
| 136 | 1 | public function configureHandlers(\Closure $closure) |
|
| 143 | |||
| 144 | 14 | public function addDefaultListeners() |
|
| 151 | |||
| 152 | 1 | public function configureListeners(\Closure $closure) |
|
| 159 | |||
| 160 | public function setObjectConstructor(ObjectConstructorInterface $constructor) |
||
| 166 | |||
| 167 | public function setPropertyNamingStrategy(PropertyNamingStrategyInterface $propertyNamingStrategy) |
||
| 173 | |||
| 174 | 1 | public function setSerializationVisitor($format, VisitorInterface $visitor) |
|
| 181 | |||
| 182 | public function setDeserializationVisitor($format, VisitorInterface $visitor) |
||
| 183 | { |
||
| 184 | $this->visitorsAdded = true; |
||
| 185 | $this->deserializationVisitors->set($format, $visitor); |
||
| 186 | |||
| 187 | return $this; |
||
| 188 | } |
||
| 189 | |||
| 190 | 14 | public function addDefaultSerializationVisitors() |
|
| 203 | |||
| 204 | 14 | public function addDefaultDeserializationVisitors() |
|
| 205 | 1 | { |
|
| 206 | 14 | $this->initializePropertyNamingStrategy(); |
|
| 207 | |||
| 208 | 14 | $this->visitorsAdded = true; |
|
| 209 | 14 | $this->deserializationVisitors->setAll(array( |
|
| 210 | 14 | 'xml' => new XmlDeserializationVisitor($this->propertyNamingStrategy), |
|
| 211 | 14 | 'json' => new JsonDeserializationVisitor($this->propertyNamingStrategy), |
|
| 212 | 14 | )); |
|
| 213 | |||
| 214 | 14 | return $this; |
|
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param Boolean $include Whether to include the metadata from the interfaces |
||
| 219 | * |
||
| 220 | * @return SerializerBuilder |
||
| 221 | */ |
||
| 222 | 1 | public function includeInterfaceMetadata($include) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Sets a map of namespace prefixes to directories. |
||
| 231 | * |
||
| 232 | * This method overrides any previously defined directories. |
||
| 233 | * |
||
| 234 | * @param array<string,string> $namespacePrefixToDirMap |
||
| 235 | * |
||
| 236 | * @return SerializerBuilder |
||
| 237 | * |
||
| 238 | * @throws InvalidArgumentException When a directory does not exist |
||
| 239 | */ |
||
| 240 | public function setMetadataDirs(array $namespacePrefixToDirMap) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Adds a directory where the serializer will look for class metadata. |
||
| 255 | * |
||
| 256 | * The namespace prefix will make the names of the actual metadata files a bit shorter. For example, let's assume |
||
| 257 | * that you have a directory where you only store metadata files for the ``MyApplication\Entity`` namespace. |
||
| 258 | * |
||
| 259 | * If you use an empty prefix, your metadata files would need to look like: |
||
| 260 | * |
||
| 261 | * ``my-dir/MyApplication.Entity.SomeObject.yml`` |
||
| 262 | * ``my-dir/MyApplication.Entity.OtherObject.xml`` |
||
| 263 | * |
||
| 264 | * If you use ``MyApplication\Entity`` as prefix, your metadata files would need to look like: |
||
| 265 | * |
||
| 266 | * ``my-dir/SomeObject.yml`` |
||
| 267 | * ``my-dir/OtherObject.yml`` |
||
| 268 | * |
||
| 269 | * Please keep in mind that you currently may only have one directory per namespace prefix. |
||
| 270 | * |
||
| 271 | * @param string $dir The directory where metadata files are located. |
||
| 272 | * @param string $namespacePrefix An optional prefix if you only store metadata for specific namespaces in this directory. |
||
| 273 | * |
||
| 274 | * @return SerializerBuilder |
||
| 275 | * |
||
| 276 | * @throws InvalidArgumentException When a directory does not exist |
||
| 277 | * @throws InvalidArgumentException When a directory has already been registered |
||
| 278 | */ |
||
| 279 | public function addMetadataDir($dir, $namespacePrefix = '') |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Adds a map of namespace prefixes to directories. |
||
| 296 | * |
||
| 297 | * @param array<string,string> $namespacePrefixToDirMap |
||
| 298 | * |
||
| 299 | * @return SerializerBuilder |
||
| 300 | */ |
||
| 301 | public function addMetadataDirs(array $namespacePrefixToDirMap) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Similar to addMetadataDir(), but overrides an existing entry. |
||
| 312 | * |
||
| 313 | * @param string $dir |
||
| 314 | * @param string $namespacePrefix |
||
| 315 | * |
||
| 316 | * @return SerializerBuilder |
||
| 317 | * |
||
| 318 | * @throws InvalidArgumentException When a directory does not exist |
||
| 319 | * @throws InvalidArgumentException When no directory is configured for the ns prefix |
||
| 320 | */ |
||
| 321 | public function replaceMetadataDir($dir, $namespacePrefix = '') |
||
| 335 | |||
| 336 | 1 | public function setMetadataDriverFactory(DriverFactoryInterface $driverFactory) |
|
| 337 | { |
||
| 338 | 1 | $this->driverFactory = $driverFactory; |
|
| 339 | |||
| 340 | 1 | return $this; |
|
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param SerializationContextFactoryInterface|callable $serializationContextFactory |
||
| 345 | * |
||
| 346 | * @return self |
||
| 347 | */ |
||
| 348 | 3 | public function setSerializationContextFactory($serializationContextFactory) |
|
| 349 | { |
||
| 350 | 3 | if ($serializationContextFactory instanceof SerializationContextFactoryInterface) { |
|
| 351 | 1 | $this->serializationContextFactory = $serializationContextFactory; |
|
| 352 | 3 | } elseif (is_callable($serializationContextFactory)) { |
|
| 353 | 2 | $this->serializationContextFactory = new CallableSerializationContextFactory( |
|
| 354 | $serializationContextFactory |
||
| 355 | 2 | ); |
|
| 356 | 2 | } else { |
|
| 357 | throw new InvalidArgumentException('expected SerializationContextFactoryInterface or callable.'); |
||
| 358 | } |
||
| 359 | |||
| 360 | 3 | return $this; |
|
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param DeserializationContextFactoryInterface|callable $deserializationContextFactory |
||
| 365 | * |
||
| 366 | * @return self |
||
| 367 | */ |
||
| 368 | 1 | public function setDeserializationContextFactory($deserializationContextFactory) |
|
| 369 | { |
||
| 370 | 1 | if ($deserializationContextFactory instanceof DeserializationContextFactoryInterface) { |
|
| 371 | 1 | $this->deserializationContextFactory = $deserializationContextFactory; |
|
| 372 | 1 | } elseif (is_callable($deserializationContextFactory)) { |
|
| 373 | $this->deserializationContextFactory = new CallableDeserializationContextFactory( |
||
| 374 | $deserializationContextFactory |
||
| 375 | ); |
||
| 376 | } else { |
||
| 377 | throw new InvalidArgumentException('expected DeserializationContextFactoryInterface or callable.'); |
||
| 378 | } |
||
| 379 | |||
| 380 | 1 | return $this; |
|
| 381 | } |
||
| 382 | |||
| 383 | 15 | public function build() |
|
| 384 | { |
||
| 385 | 15 | $annotationReader = $this->annotationReader; |
|
| 386 | 15 | if (null === $annotationReader) { |
|
| 387 | 15 | $annotationReader = new AnnotationReader(); |
|
| 388 | |||
| 389 | 15 | if (null !== $this->cacheDir) { |
|
| 390 | 1 | $this->createDir($this->cacheDir.'/annotations'); |
|
| 391 | 1 | $annotationsCache = new FilesystemCache($this->cacheDir.'/annotations'); |
|
| 392 | 1 | $annotationReader = new CachedReader($annotationReader, $annotationsCache, $this->debug); |
|
| 393 | 1 | } |
|
| 394 | 15 | } |
|
| 395 | |||
| 396 | 15 | $metadataDriver = $this->driverFactory->createDriver($this->metadataDirs, $annotationReader); |
|
| 397 | 15 | $metadataFactory = new MetadataFactory($metadataDriver, null, $this->debug); |
|
| 398 | |||
| 399 | 15 | $metadataFactory->setIncludeInterfaces($this->includeInterfaceMetadata); |
|
| 400 | |||
| 401 | 15 | if (null !== $this->cacheDir) { |
|
| 402 | 1 | $this->createDir($this->cacheDir.'/metadata'); |
|
| 403 | 1 | $metadataFactory->setCache(new FileCache($this->cacheDir.'/metadata')); |
|
| 404 | 1 | } |
|
| 405 | |||
| 406 | 15 | if ( ! $this->handlersConfigured) { |
|
| 407 | 13 | $this->addDefaultHandlers(); |
|
| 408 | 13 | } |
|
| 409 | |||
| 410 | 15 | if ( ! $this->listenersConfigured) { |
|
| 411 | 14 | $this->addDefaultListeners(); |
|
| 412 | 14 | } |
|
| 413 | |||
| 414 | 15 | if ( ! $this->visitorsAdded) { |
|
| 415 | 14 | $this->addDefaultSerializationVisitors(); |
|
| 416 | 14 | $this->addDefaultDeserializationVisitors(); |
|
| 417 | 14 | } |
|
| 418 | |||
| 419 | 15 | $serializer = new Serializer( |
|
| 420 | 15 | $metadataFactory, |
|
| 421 | 15 | $this->handlerRegistry, |
|
| 422 | 15 | $this->objectConstructor ?: new UnserializeObjectConstructor(), |
|
| 423 | 15 | $this->serializationVisitors, |
|
| 424 | 15 | $this->deserializationVisitors, |
|
| 425 | 15 | $this->eventDispatcher |
|
| 426 | 15 | ); |
|
| 427 | |||
| 428 | 15 | if (null !== $this->serializationContextFactory) { |
|
| 429 | 3 | $serializer->setSerializationContextFactory($this->serializationContextFactory); |
|
| 430 | 3 | } |
|
| 431 | |||
| 432 | 15 | if (null !== $this->deserializationContextFactory) { |
|
| 433 | 1 | $serializer->setDeserializationContextFactory($this->deserializationContextFactory); |
|
| 434 | 1 | } |
|
| 435 | |||
| 436 | 15 | return $serializer; |
|
| 437 | } |
||
| 438 | |||
| 439 | 14 | private function initializePropertyNamingStrategy() |
|
| 447 | |||
| 448 | 1 | private function createDir($dir) |
|
| 458 | } |
||
| 459 |