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 |
||
52 | class GsonBuilder |
||
53 | { |
||
54 | /** |
||
55 | * Array of type adapter factories |
||
56 | * |
||
57 | * @var TypeAdapterFactory[] |
||
58 | */ |
||
59 | private $typeAdapterFactories = []; |
||
60 | |||
61 | /** |
||
62 | * @var InstanceCreator[] |
||
63 | */ |
||
64 | private $instanceCreators = []; |
||
65 | |||
66 | /** |
||
67 | * Strategy for converting property names to serialized names |
||
68 | * |
||
69 | * @var PropertyNamingStrategy |
||
70 | */ |
||
71 | private $propertyNamingStrategy; |
||
72 | |||
73 | /** |
||
74 | * Strategy for converting property names to method names |
||
75 | * |
||
76 | * @var MethodNamingStrategy |
||
77 | */ |
||
78 | private $methodNamingStrategy; |
||
79 | |||
80 | /** |
||
81 | * The version that should be used with Since/Until annotations |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | private $version; |
||
86 | |||
87 | /** |
||
88 | * Modifiers from [@see ReflectionProperty] that should be excluded |
||
89 | * |
||
90 | * @var int |
||
91 | */ |
||
92 | private $excludedModifiers = ReflectionProperty::IS_STATIC; |
||
93 | |||
94 | /** |
||
95 | * True if the [@see Expose] annotation is required for serialization/deserialization |
||
96 | * |
||
97 | * @var bool |
||
98 | */ |
||
99 | private $requireExpose = false; |
||
100 | |||
101 | /** |
||
102 | * An array of [@see ExclusionStrategy] objects |
||
103 | * |
||
104 | * @var ExclusionStrategy[] |
||
105 | */ |
||
106 | private $exclusionStrategies = []; |
||
107 | |||
108 | /** |
||
109 | * If we should serialize nulls, defaults to false |
||
110 | * |
||
111 | * @var bool |
||
112 | */ |
||
113 | private $serializeNull = false; |
||
114 | |||
115 | /** |
||
116 | * Default format for DateTimes |
||
117 | * |
||
118 | * @var string |
||
119 | */ |
||
120 | private $dateTimeFormat = DateTime::ATOM; |
||
121 | |||
122 | /** |
||
123 | * True if we should be caching |
||
124 | * |
||
125 | * @var bool |
||
126 | */ |
||
127 | private $enableCache = false; |
||
128 | |||
129 | /** |
||
130 | * Cache directory, if set this enabled filesystem caching |
||
131 | * |
||
132 | * @var string |
||
133 | */ |
||
134 | private $cacheDir; |
||
135 | |||
136 | /** |
||
137 | * Add a custom type adapter |
||
138 | * |
||
139 | * @param TypeAdapterFactory $typeAdapterFactory |
||
140 | * @return GsonBuilder |
||
141 | */ |
||
142 | 12 | public function addTypeAdapterFactory(TypeAdapterFactory $typeAdapterFactory): GsonBuilder |
|
148 | |||
149 | /** |
||
150 | * Add custom handling for a specific type |
||
151 | * |
||
152 | * Handler objects may be of types: TypeAdapter, JsonSerializer, or JsonDeserializer |
||
153 | * |
||
154 | * @param string $type |
||
155 | * @param $handler |
||
156 | * @return GsonBuilder |
||
157 | * @throws \BadMethodCallException If the handler is not supported |
||
158 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
159 | */ |
||
160 | 7 | public function registerType(string $type, $handler): GsonBuilder |
|
188 | |||
189 | /** |
||
190 | * Add an [@see InstanceCreator] for a given type |
||
191 | * |
||
192 | * @param string $type |
||
193 | * @param InstanceCreator $instanceCreator |
||
194 | * @return GsonBuilder |
||
195 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
196 | */ |
||
197 | 2 | public function addInstanceCreator(string $type, InstanceCreator $instanceCreator): GsonBuilder |
|
204 | |||
205 | /** |
||
206 | * Set the version to be used with [@see Since] and [@see Until] annotations |
||
207 | * |
||
208 | * @param string $version |
||
209 | * @return GsonBuilder |
||
210 | */ |
||
211 | 4 | public function setVersion(string $version): GsonBuilder |
|
217 | |||
218 | /** |
||
219 | * Set the property modifiers that should be excluded based on [@see \ReflectionProperty] |
||
220 | * |
||
221 | * This number is a bitmap, so ReflectionProperty::IS_STATIC will exclude all static properties. |
||
222 | * Likewise, passing (ReflectionProperty::IS_STATIC | ReflectionProperty::IS_PRIVATE) will exclude |
||
223 | * all static and private properties. |
||
224 | * |
||
225 | * @param int $modifiers |
||
226 | * @return GsonBuilder |
||
227 | */ |
||
228 | 2 | public function setExcludedModifier(int $modifiers): GsonBuilder |
|
234 | |||
235 | /** |
||
236 | * Require the [@see Expose] annotation to serialize or deserialize property |
||
237 | * |
||
238 | * @return GsonBuilder |
||
239 | */ |
||
240 | 2 | public function requireExposeAnnotation(): GsonBuilder |
|
246 | |||
247 | /** |
||
248 | * Add an exclusion strategy that should be used during serialization/deserialization |
||
249 | * |
||
250 | * @param ExclusionStrategy $strategy |
||
251 | * @param bool $serialization |
||
252 | * @param bool $deserialization |
||
253 | * @return GsonBuilder |
||
254 | */ |
||
255 | 3 | public function addExclusionStrategy(ExclusionStrategy $strategy, bool $serialization, bool $deserialization): GsonBuilder |
|
261 | |||
262 | /** |
||
263 | * Set a custom property naming strategy |
||
264 | * |
||
265 | * @param PropertyNamingStrategy $propertyNamingStrategy |
||
266 | * @return GsonBuilder |
||
267 | */ |
||
268 | 1 | public function setPropertyNamingStrategy(PropertyNamingStrategy $propertyNamingStrategy): GsonBuilder |
|
274 | |||
275 | /** |
||
276 | * Set a custom method naming strategy |
||
277 | * |
||
278 | * @param MethodNamingStrategy $methodNamingStrategy |
||
279 | * @return GsonBuilder |
||
280 | */ |
||
281 | 1 | public function setMethodNamingStrategy(MethodNamingStrategy $methodNamingStrategy): GsonBuilder |
|
287 | |||
288 | /** |
||
289 | * Set whether we should serialize null |
||
290 | * |
||
291 | * @return GsonBuilder |
||
292 | */ |
||
293 | 1 | public function serializeNull(): GsonBuilder |
|
299 | |||
300 | /** |
||
301 | * Set the default datetime format |
||
302 | * |
||
303 | * @param string $format |
||
304 | * @return GsonBuilder |
||
305 | */ |
||
306 | 1 | public function setDateTimeFormat(string $format): GsonBuilder |
|
312 | |||
313 | /** |
||
314 | * Set whether caching is enabled |
||
315 | * |
||
316 | * @param bool $enableCache |
||
317 | * @return GsonBuilder |
||
318 | */ |
||
319 | 1 | public function enableCache(bool $enableCache): GsonBuilder |
|
325 | |||
326 | /** |
||
327 | * Setting a cache directory will turn on filesystem caching |
||
328 | * |
||
329 | * @param string $cacheDir |
||
330 | * @return GsonBuilder |
||
331 | */ |
||
332 | 1 | public function setCacheDir(string $cacheDir): GsonBuilder |
|
338 | |||
339 | /** |
||
340 | * Builds a new [@see Gson] object based on configuration set |
||
341 | * |
||
342 | * @return Gson |
||
343 | * @throws \InvalidArgumentException If there was a problem creating the cache |
||
344 | * @throws \LogicException If trying to cache without a cache directory |
||
345 | */ |
||
346 | 30 | public function build(): Gson |
|
398 | |||
399 | /** |
||
400 | * Merges default factories with user provided factories |
||
401 | * |
||
402 | * @param PropertyCollectionFactory $propertyCollectionFactory |
||
403 | * @param Excluder $excluder |
||
404 | * @param AnnotationCollectionFactory $annotationCollectionFactory |
||
405 | * @param MetadataFactory $metadataFactory |
||
406 | * @param ConstructorConstructor $constructorConstructor |
||
407 | * @return array|TypeAdapterFactory[] |
||
408 | */ |
||
409 | 29 | private function getTypeAdapterFactories( |
|
442 | } |
||
443 |