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 | * True if we should be caching |
||
| 134 | * |
||
| 135 | * @var bool |
||
| 136 | */ |
||
| 137 | private $enableCache = false; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Cache directory, if set this enabled filesystem caching |
||
| 141 | * |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | private $cacheDir; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Add a custom type adapter |
||
| 148 | * |
||
| 149 | * @param TypeAdapterFactory $typeAdapterFactory |
||
| 150 | * @return GsonBuilder |
||
| 151 | */ |
||
| 152 | 16 | public function addTypeAdapterFactory(TypeAdapterFactory $typeAdapterFactory): GsonBuilder |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Add custom handling for a specific type |
||
| 161 | * |
||
| 162 | * Handler objects may be of types: TypeAdapter, JsonSerializer, or JsonDeserializer |
||
| 163 | * |
||
| 164 | * @param string $type |
||
| 165 | * @param $handler |
||
| 166 | * @return GsonBuilder |
||
| 167 | * @throws \InvalidArgumentException |
||
| 168 | * @throws \Tebru\PhpType\Exception\MalformedTypeException If the type cannot be parsed |
||
| 169 | */ |
||
| 170 | 7 | public function registerType(string $type, $handler): GsonBuilder |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Add an [@see InstanceCreator] for a given type |
||
| 201 | * |
||
| 202 | * @param string $type |
||
| 203 | * @param InstanceCreator $instanceCreator |
||
| 204 | * @return GsonBuilder |
||
| 205 | * @throws \Tebru\PhpType\Exception\MalformedTypeException If the type cannot be parsed |
||
| 206 | */ |
||
| 207 | 2 | public function addInstanceCreator(string $type, InstanceCreator $instanceCreator): GsonBuilder |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Set the version to be used with [@see Since] and [@see Until] annotations |
||
| 217 | * |
||
| 218 | * @param string $version |
||
| 219 | * @return GsonBuilder |
||
| 220 | */ |
||
| 221 | 4 | public function setVersion(string $version): GsonBuilder |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Set the property modifiers that should be excluded based on [@see \ReflectionProperty] |
||
| 230 | * |
||
| 231 | * This number is a bitmap, so ReflectionProperty::IS_STATIC will exclude all static properties. |
||
| 232 | * Likewise, passing (ReflectionProperty::IS_STATIC | ReflectionProperty::IS_PRIVATE) will exclude |
||
| 233 | * all static and private properties. |
||
| 234 | * |
||
| 235 | * @param int $modifiers |
||
| 236 | * @return GsonBuilder |
||
| 237 | */ |
||
| 238 | 2 | public function setExcludedModifier(int $modifiers): GsonBuilder |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Require the [@see Expose] annotation to serialize or deserialize property |
||
| 247 | * |
||
| 248 | * @return GsonBuilder |
||
| 249 | */ |
||
| 250 | 2 | public function requireExposeAnnotation(): GsonBuilder |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Add an exclusion strategy that should be used during serialization/deserialization |
||
| 259 | * |
||
| 260 | * @param ExclusionStrategy $strategy |
||
| 261 | * @param bool $serialization |
||
| 262 | * @param bool $deserialization |
||
| 263 | * @return GsonBuilder |
||
| 264 | */ |
||
| 265 | 3 | public function addExclusionStrategy(ExclusionStrategy $strategy, bool $serialization, bool $deserialization): GsonBuilder |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Set a custom property naming strategy |
||
| 274 | * |
||
| 275 | * @param PropertyNamingStrategy $propertyNamingStrategy |
||
| 276 | * @return GsonBuilder |
||
| 277 | */ |
||
| 278 | 1 | public function setPropertyNamingStrategy(PropertyNamingStrategy $propertyNamingStrategy): GsonBuilder |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Set one of [@see PropertyNamingPolicy] |
||
| 287 | * |
||
| 288 | * @param string $policy |
||
| 289 | * @return GsonBuilder |
||
| 290 | */ |
||
| 291 | 1 | public function setPropertyNamingPolicy(string $policy): GsonBuilder |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Set a custom method naming strategy |
||
| 300 | * |
||
| 301 | * @param MethodNamingStrategy $methodNamingStrategy |
||
| 302 | * @return GsonBuilder |
||
| 303 | */ |
||
| 304 | 1 | public function setMethodNamingStrategy(MethodNamingStrategy $methodNamingStrategy): GsonBuilder |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Set whether we should serialize null |
||
| 313 | * |
||
| 314 | * @return GsonBuilder |
||
| 315 | */ |
||
| 316 | 1 | public function serializeNull(): GsonBuilder |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Set the default datetime format |
||
| 325 | * |
||
| 326 | * @param string $format |
||
| 327 | * @return GsonBuilder |
||
| 328 | */ |
||
| 329 | 1 | public function setDateTimeFormat(string $format): GsonBuilder |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Set whether caching is enabled |
||
| 338 | * |
||
| 339 | * @param bool $enableCache |
||
| 340 | * @return GsonBuilder |
||
| 341 | */ |
||
| 342 | 1 | public function enableCache(bool $enableCache): GsonBuilder |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Setting a cache directory will turn on filesystem caching |
||
| 351 | * |
||
| 352 | * @param string $cacheDir |
||
| 353 | * @return GsonBuilder |
||
| 354 | */ |
||
| 355 | 1 | public function setCacheDir(string $cacheDir): GsonBuilder |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Builds a new [@see Gson] object based on configuration set |
||
| 364 | * |
||
| 365 | * @return Gson |
||
| 366 | * @throws \Doctrine\Common\Annotations\AnnotationException |
||
| 367 | * @throws \InvalidArgumentException |
||
| 368 | * @throws \LogicException |
||
| 369 | */ |
||
| 370 | 37 | public function build(): Gson |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Merges default factories with user provided factories |
||
| 415 | * |
||
| 416 | * @param PropertyCollectionFactory $propertyCollectionFactory |
||
| 417 | * @param Excluder $excluder |
||
| 418 | * @param AnnotationReaderAdapter $annotationReader |
||
| 419 | * @param MetadataFactory $metadataFactory |
||
| 420 | * @param ConstructorConstructor $constructorConstructor |
||
| 421 | * @return array|TypeAdapterFactory[] |
||
| 422 | */ |
||
| 423 | 36 | private function getTypeAdapterFactories( |
|
| 456 | } |
||
| 457 |