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 |
||
49 | class GsonBuilder |
||
50 | { |
||
51 | /** |
||
52 | * Array of type adapter factories |
||
53 | * |
||
54 | * @var TypeAdapterFactory[] |
||
55 | */ |
||
56 | private $typeAdapterFactories = []; |
||
57 | |||
58 | /** |
||
59 | * @var TypeAdapter[] |
||
60 | */ |
||
61 | private $typeAdapters = []; |
||
62 | |||
63 | /** |
||
64 | * @var InstanceCreator[] |
||
65 | */ |
||
66 | private $instanceCreators = []; |
||
67 | |||
68 | /** |
||
69 | * Strategy for converting property names to serialized names |
||
70 | * |
||
71 | * @var PropertyNamingStrategy |
||
72 | */ |
||
73 | private $propertyNamingStrategy; |
||
74 | |||
75 | /** |
||
76 | * Strategy for converting property names to method names |
||
77 | * |
||
78 | * @var MethodNamingStrategy |
||
79 | */ |
||
80 | private $methodNamingStrategy; |
||
81 | |||
82 | /** |
||
83 | * The version that should be used with Since/Until annotations |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | private $version; |
||
88 | |||
89 | /** |
||
90 | * Modifiers from [@see ReflectionProperty] that should be excluded |
||
91 | * |
||
92 | * @var int |
||
93 | */ |
||
94 | private $excludedModifiers = ReflectionProperty::IS_STATIC; |
||
95 | |||
96 | /** |
||
97 | * True if the [@see Expose] annotation is required for serialization/deserialization |
||
98 | * |
||
99 | * @var bool |
||
100 | */ |
||
101 | private $requireExpose = false; |
||
102 | |||
103 | /** |
||
104 | * An array of [@see ExclusionStrategy] objects |
||
105 | * |
||
106 | * @var ExclusionStrategy[] |
||
107 | */ |
||
108 | private $exclusionStrategies = []; |
||
109 | |||
110 | /** |
||
111 | * If we should serialize nulls, defaults to false |
||
112 | * |
||
113 | * @var bool |
||
114 | */ |
||
115 | private $serializeNull = false; |
||
116 | |||
117 | /** |
||
118 | * Cache directory, if set this enabled filesystem caching |
||
119 | * |
||
120 | * @var string |
||
121 | */ |
||
122 | private $cacheDir; |
||
123 | |||
124 | /** |
||
125 | * Add a custom type adapter |
||
126 | * |
||
127 | * @param TypeAdapterFactory $typeAdapterFactory |
||
128 | * @return GsonBuilder |
||
129 | */ |
||
130 | 2 | public function addTypeAdapterFactory(TypeAdapterFactory $typeAdapterFactory): GsonBuilder |
|
136 | |||
137 | /** |
||
138 | * Add custom handling for a specific type |
||
139 | * |
||
140 | * Handler objects may be of types: TypeAdapter, JsonSerializer, or JsonDeserializer |
||
141 | * |
||
142 | * @param string $type |
||
143 | * @param $handler |
||
144 | * @return GsonBuilder |
||
145 | * @throws \BadMethodCallException If the handler is not supported |
||
146 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
147 | */ |
||
148 | 6 | public function registerType(string $type, $handler): GsonBuilder |
|
149 | { |
||
150 | 6 | if ($handler instanceof TypeAdapter) { |
|
151 | 2 | $this->typeAdapters[(string) new PhpType($type)] = $handler; |
|
152 | |||
153 | 2 | return $this; |
|
154 | } |
||
155 | |||
156 | 4 | if ($handler instanceof JsonSerializer && $handler instanceof JsonDeserializer) { |
|
157 | 1 | $this->typeAdapterFactories[] = new CustomWrappedTypeAdapterFactory(new PhpType($type), $handler, $handler); |
|
158 | |||
159 | 1 | return $this; |
|
160 | } |
||
161 | |||
162 | 3 | View Code Duplication | if ($handler instanceof JsonSerializer) { |
163 | 1 | $this->typeAdapterFactories[] = new CustomWrappedTypeAdapterFactory(new PhpType($type), $handler); |
|
164 | |||
165 | 1 | return $this; |
|
166 | } |
||
167 | |||
168 | 2 | View Code Duplication | if ($handler instanceof JsonDeserializer) { |
169 | 1 | $this->typeAdapterFactories[] = new CustomWrappedTypeAdapterFactory(new PhpType($type), null, $handler); |
|
170 | |||
171 | 1 | return $this; |
|
172 | } |
||
173 | |||
174 | 1 | throw new BadMethodCallException(sprintf('Handler of type "%s" is not supported', get_class($handler))); |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * Add an [@see InstanceCreator] for a given type |
||
179 | * |
||
180 | * @param string $type |
||
181 | * @param InstanceCreator $instanceCreator |
||
182 | * @return GsonBuilder |
||
183 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
184 | */ |
||
185 | 1 | public function addInstanceCreator(string $type, InstanceCreator $instanceCreator): GsonBuilder |
|
191 | |||
192 | /** |
||
193 | * Set the version to be used with [@see Since] and [@see Until] annotations |
||
194 | * |
||
195 | * @param string $version |
||
196 | * @return GsonBuilder |
||
197 | */ |
||
198 | 4 | public function setVersion(string $version): GsonBuilder |
|
204 | |||
205 | /** |
||
206 | * Set the property modifiers that should be excluded based on [@see \ReflectionProperty] |
||
207 | * |
||
208 | * This number is a bitmap, so ReflectionProperty::IS_STATIC will exclude all static properties. |
||
209 | * Likewise, passing (ReflectionProperty::IS_STATIC | ReflectionProperty::IS_PRIVATE) will exclude |
||
210 | * all static and private properties. |
||
211 | * |
||
212 | * @param int $modifiers |
||
213 | * @return GsonBuilder |
||
214 | */ |
||
215 | 2 | public function setExcludedModifier(int $modifiers): GsonBuilder |
|
221 | |||
222 | /** |
||
223 | * Require the [@see Expose] annotation to serialize or deserialize property |
||
224 | * |
||
225 | * @return GsonBuilder |
||
226 | */ |
||
227 | 2 | public function requireExposeAnnotation(): GsonBuilder |
|
233 | |||
234 | /** |
||
235 | * Add an exclusion strategy that should be used during serialization/deserialization |
||
236 | * |
||
237 | * @param ExclusionStrategy $strategy |
||
238 | * @param bool $serialization |
||
239 | * @param bool $deserialization |
||
240 | * @return GsonBuilder |
||
241 | */ |
||
242 | 2 | public function addExclusionStrategy(ExclusionStrategy $strategy, bool $serialization, bool $deserialization): GsonBuilder |
|
248 | |||
249 | /** |
||
250 | * Set a custom property naming strategy |
||
251 | * |
||
252 | * @param PropertyNamingStrategy $propertyNamingStrategy |
||
253 | * @return GsonBuilder |
||
254 | */ |
||
255 | 1 | public function setPropertyNamingStrategy(PropertyNamingStrategy $propertyNamingStrategy): GsonBuilder |
|
261 | |||
262 | /** |
||
263 | * Set a custom method naming strategy |
||
264 | * |
||
265 | * @param MethodNamingStrategy $methodNamingStrategy |
||
266 | * @return GsonBuilder |
||
267 | */ |
||
268 | 1 | public function setMethodNamingStrategy(MethodNamingStrategy $methodNamingStrategy): GsonBuilder |
|
274 | |||
275 | /** |
||
276 | * Set whether we should serialize null |
||
277 | * |
||
278 | * @return GsonBuilder |
||
279 | */ |
||
280 | 1 | public function serializeNull(): GsonBuilder |
|
286 | |||
287 | /** |
||
288 | * Setting a cache directory will turn on filesystem caching |
||
289 | * |
||
290 | * @codeCoverageIgnore |
||
291 | * @param string $cacheDir |
||
292 | * @return GsonBuilder |
||
293 | */ |
||
294 | public function setCacheDir(string $cacheDir): GsonBuilder |
||
300 | |||
301 | /** |
||
302 | * Builds a new [@see Gson] object based on configuration set |
||
303 | * |
||
304 | * @return Gson |
||
305 | * @throws \InvalidArgumentException If there was a problem creating the cache |
||
306 | */ |
||
307 | 24 | public function build(): Gson |
|
355 | |||
356 | /** |
||
357 | * Merges default factories with user provided factories |
||
358 | * |
||
359 | * @param PropertyCollectionFactory $propertyCollectionFactory |
||
360 | * @param Excluder $excluder |
||
361 | * @param AnnotationCollectionFactory $annotationCollectionFactory |
||
362 | * @return array|TypeAdapterFactory[] |
||
363 | */ |
||
364 | 24 | private function getTypeAdapterFactories( |
|
391 | } |
||
392 |