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 |
||
| 53 | class GsonBuilder |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * Array of type adapter factories |
||
| 57 | * |
||
| 58 | * @var TypeAdapterFactory[] |
||
| 59 | */ |
||
| 60 | private $typeAdapterFactories = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var InstanceCreator[] |
||
| 64 | */ |
||
| 65 | private $instanceCreators = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Strategy for converting property names to serialized names |
||
| 69 | * |
||
| 70 | * @var PropertyNamingStrategy |
||
| 71 | */ |
||
| 72 | private $propertyNamingStrategy; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Property naming policy |
||
| 76 | * |
||
| 77 | * Defaults to converting camel case to snake case |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | private $propertyNamingPolicy = PropertyNamingPolicy::LOWER_CASE_WITH_UNDERSCORES; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Strategy for converting property names to method names |
||
| 85 | * |
||
| 86 | * @var MethodNamingStrategy |
||
| 87 | */ |
||
| 88 | private $methodNamingStrategy; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The version that should be used with Since/Until annotations |
||
| 92 | * |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | private $version; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Modifiers from [@see ReflectionProperty] that should be excluded |
||
| 99 | * |
||
| 100 | * @var int |
||
| 101 | */ |
||
| 102 | private $excludedModifiers = ReflectionProperty::IS_STATIC; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * True if the [@see Expose] annotation is required for serialization/deserialization |
||
| 106 | * |
||
| 107 | * @var bool |
||
| 108 | */ |
||
| 109 | private $requireExpose = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * An array of [@see ExclusionStrategy] objects |
||
| 113 | * |
||
| 114 | * @var ExclusionStrategy[] |
||
| 115 | */ |
||
| 116 | private $exclusionStrategies = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * If we should serialize nulls, defaults to false |
||
| 120 | * |
||
| 121 | * @var bool |
||
| 122 | */ |
||
| 123 | private $serializeNull = false; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Default format for DateTimes |
||
| 127 | * |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | private $dateTimeFormat = DateTime::ATOM; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * A cache interface to be used in place of defaults |
||
| 134 | * |
||
| 135 | * If this is set, [@see GsonBuilder::$enableCache] will be ignored |
||
| 136 | * |
||
| 137 | * @var CacheInterface |
||
| 138 | */ |
||
| 139 | private $cache; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * True if we should be caching |
||
| 143 | * |
||
| 144 | * @var bool |
||
| 145 | */ |
||
| 146 | private $enableCache = false; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Cache directory, if set this enabled filesystem caching |
||
| 150 | * |
||
| 151 | * @var string |
||
| 152 | */ |
||
| 153 | private $cacheDir; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Add a custom type adapter |
||
| 157 | * |
||
| 158 | * @param TypeAdapterFactory $typeAdapterFactory |
||
| 159 | * @return GsonBuilder |
||
| 160 | */ |
||
| 161 | 16 | public function addTypeAdapterFactory(TypeAdapterFactory $typeAdapterFactory): GsonBuilder |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Add custom handling for a specific type |
||
| 170 | * |
||
| 171 | * Handler objects may be of types: TypeAdapter, JsonSerializer, or JsonDeserializer |
||
| 172 | * |
||
| 173 | * @param string $type |
||
| 174 | * @param $handler |
||
| 175 | * @return GsonBuilder |
||
| 176 | * @throws \InvalidArgumentException |
||
| 177 | */ |
||
| 178 | 7 | public function registerType(string $type, $handler): GsonBuilder |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Add an [@see InstanceCreator] for a given type |
||
| 209 | * |
||
| 210 | * @param string $type |
||
| 211 | * @param InstanceCreator $instanceCreator |
||
| 212 | * @return GsonBuilder |
||
| 213 | */ |
||
| 214 | 2 | public function addInstanceCreator(string $type, InstanceCreator $instanceCreator): GsonBuilder |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Set the version to be used with [@see Since] and [@see Until] annotations |
||
| 224 | * |
||
| 225 | * @param string $version |
||
| 226 | * @return GsonBuilder |
||
| 227 | */ |
||
| 228 | 4 | public function setVersion(string $version): GsonBuilder |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Set the property modifiers that should be excluded based on [@see \ReflectionProperty] |
||
| 237 | * |
||
| 238 | * This number is a bitmap, so ReflectionProperty::IS_STATIC will exclude all static properties. |
||
| 239 | * Likewise, passing (ReflectionProperty::IS_STATIC | ReflectionProperty::IS_PRIVATE) will exclude |
||
| 240 | * all static and private properties. |
||
| 241 | * |
||
| 242 | * @param int $modifiers |
||
| 243 | * @return GsonBuilder |
||
| 244 | */ |
||
| 245 | 2 | public function setExcludedModifier(int $modifiers): GsonBuilder |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Require the [@see Expose] annotation to serialize or deserialize property |
||
| 254 | * |
||
| 255 | * @return GsonBuilder |
||
| 256 | */ |
||
| 257 | 2 | public function requireExposeAnnotation(): GsonBuilder |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Add an exclusion strategy that should be used during serialization/deserialization |
||
| 266 | * |
||
| 267 | * @param ExclusionStrategy $strategy |
||
| 268 | * @param bool $serialization |
||
| 269 | * @param bool $deserialization |
||
| 270 | * @return GsonBuilder |
||
| 271 | */ |
||
| 272 | 3 | public function addExclusionStrategy(ExclusionStrategy $strategy, bool $serialization, bool $deserialization): GsonBuilder |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Set a custom property naming strategy |
||
| 281 | * |
||
| 282 | * @param PropertyNamingStrategy $propertyNamingStrategy |
||
| 283 | * @return GsonBuilder |
||
| 284 | */ |
||
| 285 | 1 | public function setPropertyNamingStrategy(PropertyNamingStrategy $propertyNamingStrategy): GsonBuilder |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Set one of [@see PropertyNamingPolicy] |
||
| 294 | * |
||
| 295 | * @param string $policy |
||
| 296 | * @return GsonBuilder |
||
| 297 | */ |
||
| 298 | 1 | public function setPropertyNamingPolicy(string $policy): GsonBuilder |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Set a custom method naming strategy |
||
| 307 | * |
||
| 308 | * @param MethodNamingStrategy $methodNamingStrategy |
||
| 309 | * @return GsonBuilder |
||
| 310 | */ |
||
| 311 | 1 | public function setMethodNamingStrategy(MethodNamingStrategy $methodNamingStrategy): GsonBuilder |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Set whether we should serialize null |
||
| 320 | * |
||
| 321 | * @return GsonBuilder |
||
| 322 | */ |
||
| 323 | 1 | public function serializeNull(): GsonBuilder |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Set the default datetime format |
||
| 332 | * |
||
| 333 | * @param string $format |
||
| 334 | * @return GsonBuilder |
||
| 335 | */ |
||
| 336 | 1 | public function setDateTimeFormat(string $format): GsonBuilder |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Override default cache adapters |
||
| 345 | * |
||
| 346 | * @param CacheInterface $cache |
||
| 347 | * @return GsonBuilder |
||
| 348 | */ |
||
| 349 | 1 | public function setCache(CacheInterface $cache): GsonBuilder |
|
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * Set whether caching is enabled |
||
| 359 | * |
||
| 360 | * @param bool $enableCache |
||
| 361 | * @return GsonBuilder |
||
| 362 | */ |
||
| 363 | 2 | public function enableCache(bool $enableCache): GsonBuilder |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Setting a cache directory will turn on filesystem caching |
||
| 372 | * |
||
| 373 | * @param string $cacheDir |
||
| 374 | * @return GsonBuilder |
||
| 375 | */ |
||
| 376 | 2 | public function setCacheDir(string $cacheDir): GsonBuilder |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Builds a new [@see Gson] object based on configuration set |
||
| 385 | * |
||
| 386 | * @return Gson |
||
| 387 | * @throws \LogicException |
||
| 388 | */ |
||
| 389 | 39 | public function build(): Gson |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Merges default factories with user provided factories |
||
| 436 | * |
||
| 437 | * @param PropertyCollectionFactory $propertyCollectionFactory |
||
| 438 | * @param Excluder $excluder |
||
| 439 | * @param AnnotationReaderAdapter $annotationReader |
||
| 440 | * @param MetadataFactory $metadataFactory |
||
| 441 | * @param ConstructorConstructor $constructorConstructor |
||
| 442 | * @return array|TypeAdapterFactory[] |
||
| 443 | */ |
||
| 444 | 38 | private function getTypeAdapterFactories( |
|
| 476 | } |
||
| 477 |