Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
55 | class GsonBuilder |
||
56 | { |
||
57 | /** |
||
58 | * Array of type adapter factories |
||
59 | * |
||
60 | * @var TypeAdapterFactory[] |
||
61 | */ |
||
62 | private $typeAdapterFactories = []; |
||
63 | |||
64 | /** |
||
65 | * @var InstanceCreator[] |
||
66 | */ |
||
67 | private $instanceCreators = []; |
||
68 | |||
69 | /** |
||
70 | * Strategy for converting property names to serialized names |
||
71 | * |
||
72 | * @var PropertyNamingStrategy |
||
73 | */ |
||
74 | private $propertyNamingStrategy; |
||
75 | |||
76 | /** |
||
77 | * Property naming policy |
||
78 | * |
||
79 | * Defaults to converting camel case to snake case |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | private $propertyNamingPolicy = PropertyNamingPolicy::LOWER_CASE_WITH_UNDERSCORES; |
||
84 | |||
85 | /** |
||
86 | * Strategy for converting property names to method names |
||
87 | * |
||
88 | * @var MethodNamingStrategy |
||
89 | */ |
||
90 | private $methodNamingStrategy; |
||
91 | |||
92 | /** |
||
93 | * The version that should be used with Since/Until annotations |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | private $version; |
||
98 | |||
99 | /** |
||
100 | * Modifiers from [@see ReflectionProperty] that should be excluded |
||
101 | * |
||
102 | * @var int |
||
103 | */ |
||
104 | private $excludedModifiers = ReflectionProperty::IS_STATIC; |
||
105 | |||
106 | /** |
||
107 | * True if the [@see Expose] annotation is required for serialization/deserialization |
||
108 | * |
||
109 | * @var bool |
||
110 | */ |
||
111 | private $requireExpose = false; |
||
112 | |||
113 | /** |
||
114 | * An array of [@see ExclusionStrategy] objects |
||
115 | * |
||
116 | * @var ExclusionStrategy[] |
||
117 | */ |
||
118 | private $exclusionStrategies = []; |
||
119 | |||
120 | /** |
||
121 | * If we should serialize nulls, defaults to false |
||
122 | * |
||
123 | * @var bool |
||
124 | */ |
||
125 | private $serializeNull = false; |
||
126 | |||
127 | /** |
||
128 | * Default format for DateTimes |
||
129 | * |
||
130 | * @var string |
||
131 | */ |
||
132 | private $dateTimeFormat = DateTime::ATOM; |
||
133 | |||
134 | /** |
||
135 | * A cache interface to be used in place of defaults |
||
136 | * |
||
137 | * If this is set, [@see GsonBuilder::$enableCache] will be ignored |
||
138 | * |
||
139 | * @var CacheInterface |
||
140 | */ |
||
141 | private $cache; |
||
142 | |||
143 | /** |
||
144 | * True if we should be caching |
||
145 | * |
||
146 | * @var bool |
||
147 | */ |
||
148 | private $enableCache = false; |
||
149 | |||
150 | /** |
||
151 | * Cache directory, if set this enabled filesystem caching |
||
152 | * |
||
153 | * @var string |
||
154 | */ |
||
155 | private $cacheDir; |
||
156 | |||
157 | /** |
||
158 | * Add a custom type adapter |
||
159 | * |
||
160 | * @param TypeAdapterFactory $typeAdapterFactory |
||
161 | * @return GsonBuilder |
||
162 | */ |
||
163 | 16 | public function addTypeAdapterFactory(TypeAdapterFactory $typeAdapterFactory): GsonBuilder |
|
169 | |||
170 | /** |
||
171 | * Adds a [@see Discriminator] as a type adapter factory |
||
172 | * |
||
173 | * @param string $type |
||
174 | * @param Discriminator $discriminator |
||
175 | * @return GsonBuilder |
||
176 | */ |
||
177 | 3 | public function addDiscriminator(string $type, Discriminator $discriminator): GsonBuilder |
|
188 | |||
189 | /** |
||
190 | * Add custom handling for a specific type |
||
191 | * |
||
192 | * Handler objects may be of types: TypeAdapter, JsonSerializer, or JsonDeserializer. Passing |
||
193 | * $strict=true will match the specified type exactly, as opposed to checking anywhere in the |
||
194 | * inheritance chain. |
||
195 | * |
||
196 | * @param string $type |
||
197 | * @param $handler |
||
198 | * @param bool $strict |
||
199 | * @return GsonBuilder |
||
200 | */ |
||
201 | 7 | public function registerType(string $type, $handler, bool $strict = false): GsonBuilder |
|
229 | |||
230 | /** |
||
231 | * Add an [@see InstanceCreator] for a given type |
||
232 | * |
||
233 | * @param string $type |
||
234 | * @param InstanceCreator $instanceCreator |
||
235 | * @return GsonBuilder |
||
236 | */ |
||
237 | 2 | public function addInstanceCreator(string $type, InstanceCreator $instanceCreator): GsonBuilder |
|
244 | |||
245 | /** |
||
246 | * Set the version to be used with [@see Since] and [@see Until] annotations |
||
247 | * |
||
248 | * @param string $version |
||
249 | * @return GsonBuilder |
||
250 | */ |
||
251 | 4 | public function setVersion(string $version): GsonBuilder |
|
257 | |||
258 | /** |
||
259 | * Set the property modifiers that should be excluded based on [@see \ReflectionProperty] |
||
260 | * |
||
261 | * This number is a bitmap, so ReflectionProperty::IS_STATIC will exclude all static properties. |
||
262 | * Likewise, passing (ReflectionProperty::IS_STATIC | ReflectionProperty::IS_PRIVATE) will exclude |
||
263 | * all static and private properties. |
||
264 | * |
||
265 | * @param int $modifiers |
||
266 | * @return GsonBuilder |
||
267 | */ |
||
268 | 2 | public function setExcludedModifier(int $modifiers): GsonBuilder |
|
274 | |||
275 | /** |
||
276 | * Require the [@see Expose] annotation to serialize or deserialize property |
||
277 | * |
||
278 | * @return GsonBuilder |
||
279 | */ |
||
280 | 2 | public function requireExposeAnnotation(): GsonBuilder |
|
286 | |||
287 | /** |
||
288 | * Add an exclusion strategy that should be used during serialization/deserialization |
||
289 | * |
||
290 | * @param ExclusionStrategy $strategy |
||
291 | * @param bool $serialization |
||
292 | * @param bool $deserialization |
||
293 | * @return GsonBuilder |
||
294 | */ |
||
295 | 3 | public function addExclusionStrategy(ExclusionStrategy $strategy, bool $serialization, bool $deserialization): GsonBuilder |
|
301 | |||
302 | /** |
||
303 | * Set a custom property naming strategy |
||
304 | * |
||
305 | * @param PropertyNamingStrategy $propertyNamingStrategy |
||
306 | * @return GsonBuilder |
||
307 | */ |
||
308 | 1 | public function setPropertyNamingStrategy(PropertyNamingStrategy $propertyNamingStrategy): GsonBuilder |
|
314 | |||
315 | /** |
||
316 | * Set one of [@see PropertyNamingPolicy] |
||
317 | * |
||
318 | * @param string $policy |
||
319 | * @return GsonBuilder |
||
320 | */ |
||
321 | 1 | public function setPropertyNamingPolicy(string $policy): GsonBuilder |
|
327 | |||
328 | /** |
||
329 | * Set a custom method naming strategy |
||
330 | * |
||
331 | * @param MethodNamingStrategy $methodNamingStrategy |
||
332 | * @return GsonBuilder |
||
333 | */ |
||
334 | 1 | public function setMethodNamingStrategy(MethodNamingStrategy $methodNamingStrategy): GsonBuilder |
|
340 | |||
341 | /** |
||
342 | * Set whether we should serialize null |
||
343 | * |
||
344 | * @return GsonBuilder |
||
345 | */ |
||
346 | 1 | public function serializeNull(): GsonBuilder |
|
352 | |||
353 | /** |
||
354 | * Set the default datetime format |
||
355 | * |
||
356 | * @param string $format |
||
357 | * @return GsonBuilder |
||
358 | */ |
||
359 | 1 | public function setDateTimeFormat(string $format): GsonBuilder |
|
365 | |||
366 | /** |
||
367 | * Override default cache adapters |
||
368 | * |
||
369 | * @param CacheInterface $cache |
||
370 | * @return GsonBuilder |
||
371 | */ |
||
372 | 1 | public function setCache(CacheInterface $cache): GsonBuilder |
|
378 | |||
379 | |||
380 | /** |
||
381 | * Set whether caching is enabled |
||
382 | * |
||
383 | * @param bool $enableCache |
||
384 | * @return GsonBuilder |
||
385 | */ |
||
386 | 2 | public function enableCache(bool $enableCache): GsonBuilder |
|
392 | |||
393 | /** |
||
394 | * Setting a cache directory will turn on filesystem caching |
||
395 | * |
||
396 | * @param string $cacheDir |
||
397 | * @return GsonBuilder |
||
398 | */ |
||
399 | 2 | public function setCacheDir(string $cacheDir): GsonBuilder |
|
405 | |||
406 | /** |
||
407 | * Builds a new [@see Gson] object based on configuration set |
||
408 | * |
||
409 | * @return Gson |
||
410 | * @throws \LogicException |
||
411 | */ |
||
412 | 42 | public function build(): Gson |
|
456 | |||
457 | /** |
||
458 | * Merges default factories with user provided factories |
||
459 | * |
||
460 | * @param PropertyCollectionFactory $propertyCollectionFactory |
||
461 | * @param Excluder $excluder |
||
462 | * @param AnnotationReaderAdapter $annotationReader |
||
463 | * @param MetadataFactory $metadataFactory |
||
464 | * @param ConstructorConstructor $constructorConstructor |
||
465 | * @return array|TypeAdapterFactory[] |
||
466 | */ |
||
467 | 41 | private function getTypeAdapterFactories( |
|
499 | } |
||
500 |