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 |
||
65 | class SerializerBuilder |
||
66 | { |
||
67 | private $metadataDirs = array(); |
||
68 | private $handlerRegistry; |
||
69 | private $handlersConfigured = false; |
||
70 | private $eventDispatcher; |
||
71 | private $listenersConfigured = false; |
||
72 | private $objectConstructor; |
||
73 | private $serializationVisitors; |
||
74 | private $deserializationVisitors; |
||
75 | private $visitorsAdded = false; |
||
76 | private $propertyNamingStrategy; |
||
77 | private $debug = false; |
||
78 | private $cacheDir; |
||
79 | private $annotationReader; |
||
80 | private $includeInterfaceMetadata = false; |
||
81 | private $driverFactory; |
||
82 | private $serializationContextFactory; |
||
83 | private $deserializationContextFactory; |
||
84 | |||
85 | /** |
||
86 | * @var ExpressionEvaluatorInterface |
||
87 | */ |
||
88 | private $expressionEvaluator; |
||
89 | |||
90 | /** |
||
91 | * @var AccessorStrategyInterface |
||
92 | */ |
||
93 | private $accessorStrategy; |
||
94 | |||
95 | /** |
||
96 | * @var ClassMetadataUpdaterInterface |
||
97 | */ |
||
98 | private $propertyUpdater; |
||
99 | |||
100 | 30 | public static function create() |
|
104 | |||
105 | 30 | public function __construct() |
|
114 | |||
115 | public function setAccessorStrategy(AccessorStrategyInterface $accessorStrategy) |
||
119 | |||
120 | 29 | protected function getAccessorStrategy() |
|
131 | |||
132 | 3 | public function setExpressionEvaluator(ExpressionEvaluatorInterface $expressionEvaluator) |
|
138 | |||
139 | public function setAnnotationReader(Reader $reader) |
||
145 | |||
146 | /** |
||
147 | * You must preserve default behavior of `ClassAccessorUpdater`. |
||
148 | * |
||
149 | * @param ClassMetadataUpdaterInterface $propertyUpdater |
||
150 | * |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function setPropertyUpdater(ClassMetadataUpdaterInterface $propertyUpdater) |
||
159 | |||
160 | public function setDebug($bool) |
||
166 | |||
167 | 1 | public function setCacheDir($dir) |
|
180 | |||
181 | 29 | public function addDefaultHandlers() |
|
192 | |||
193 | 1 | public function configureHandlers(\Closure $closure) |
|
200 | |||
201 | 29 | public function addDefaultListeners() |
|
208 | |||
209 | 1 | public function configureListeners(\Closure $closure) |
|
216 | |||
217 | public function setObjectConstructor(ObjectConstructorInterface $constructor) |
||
223 | |||
224 | public function setPropertyNamingStrategy(PropertyNamingStrategyInterface $propertyNamingStrategy) |
||
230 | |||
231 | 1 | public function setAdvancedNamingStrategy(AdvancedNamingStrategyInterface $advancedNamingStrategy) |
|
237 | |||
238 | 1 | public function setSerializationVisitor($format, VisitorInterface $visitor) |
|
245 | |||
246 | public function setDeserializationVisitor($format, VisitorInterface $visitor) |
||
253 | |||
254 | 29 | public function addDefaultSerializationVisitors() |
|
267 | |||
268 | 29 | public function addDefaultDeserializationVisitors() |
|
280 | |||
281 | /** |
||
282 | * @param Boolean $include Whether to include the metadata from the interfaces |
||
283 | * |
||
284 | * @return SerializerBuilder |
||
285 | */ |
||
286 | 1 | public function includeInterfaceMetadata($include) |
|
292 | |||
293 | /** |
||
294 | * Sets a map of namespace prefixes to directories. |
||
295 | * |
||
296 | * This method overrides any previously defined directories. |
||
297 | * |
||
298 | * @param array <string,string> $namespacePrefixToDirMap |
||
299 | * |
||
300 | * @return SerializerBuilder |
||
301 | * |
||
302 | * @throws InvalidArgumentException When a directory does not exist |
||
303 | */ |
||
304 | public function setMetadataDirs(array $namespacePrefixToDirMap) |
||
316 | |||
317 | /** |
||
318 | * Adds a directory where the serializer will look for class metadata. |
||
319 | * |
||
320 | * The namespace prefix will make the names of the actual metadata files a bit shorter. For example, let's assume |
||
321 | * that you have a directory where you only store metadata files for the ``MyApplication\Entity`` namespace. |
||
322 | * |
||
323 | * If you use an empty prefix, your metadata files would need to look like: |
||
324 | * |
||
325 | * ``my-dir/MyApplication.Entity.SomeObject.yml`` |
||
326 | * ``my-dir/MyApplication.Entity.OtherObject.xml`` |
||
327 | * |
||
328 | * If you use ``MyApplication\Entity`` as prefix, your metadata files would need to look like: |
||
329 | * |
||
330 | * ``my-dir/SomeObject.yml`` |
||
331 | * ``my-dir/OtherObject.yml`` |
||
332 | * |
||
333 | * Please keep in mind that you currently may only have one directory per namespace prefix. |
||
334 | * |
||
335 | * @param string $dir The directory where metadata files are located. |
||
336 | * @param string $namespacePrefix An optional prefix if you only store metadata for specific namespaces in this directory. |
||
337 | * |
||
338 | * @return SerializerBuilder |
||
339 | * |
||
340 | * @throws InvalidArgumentException When a directory does not exist |
||
341 | * @throws InvalidArgumentException When a directory has already been registered |
||
342 | */ |
||
343 | public function addMetadataDir($dir, $namespacePrefix = '') |
||
357 | |||
358 | /** |
||
359 | * Adds a map of namespace prefixes to directories. |
||
360 | * |
||
361 | * @param array <string,string> $namespacePrefixToDirMap |
||
362 | * |
||
363 | * @return SerializerBuilder |
||
364 | */ |
||
365 | public function addMetadataDirs(array $namespacePrefixToDirMap) |
||
373 | |||
374 | /** |
||
375 | * Similar to addMetadataDir(), but overrides an existing entry. |
||
376 | * |
||
377 | * @param string $dir |
||
378 | * @param string $namespacePrefix |
||
379 | * |
||
380 | * @return SerializerBuilder |
||
381 | * |
||
382 | * @throws InvalidArgumentException When a directory does not exist |
||
383 | * @throws InvalidArgumentException When no directory is configured for the ns prefix |
||
384 | */ |
||
385 | public function replaceMetadataDir($dir, $namespacePrefix = '') |
||
399 | |||
400 | 12 | public function setMetadataDriverFactory(DriverFactoryInterface $driverFactory) |
|
406 | |||
407 | /** |
||
408 | * @param SerializationContextFactoryInterface|callable $serializationContextFactory |
||
409 | * |
||
410 | * @return self |
||
411 | */ |
||
412 | 3 | public function setSerializationContextFactory($serializationContextFactory) |
|
426 | |||
427 | /** |
||
428 | * @param DeserializationContextFactoryInterface|callable $deserializationContextFactory |
||
429 | * |
||
430 | * @return self |
||
431 | */ |
||
432 | 1 | public function setDeserializationContextFactory($deserializationContextFactory) |
|
446 | |||
447 | 30 | public function build() |
|
508 | |||
509 | 29 | private function initializePropertyNamingStrategy() |
|
517 | |||
518 | 1 | private function createDir($dir) |
|
528 | } |
||
529 |