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 | * True if we should be caching |
||
| 119 | * |
||
| 120 | * @var bool |
||
| 121 | */ |
||
| 122 | private $enableCache = false; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Cache directory, if set this enabled filesystem caching |
||
| 126 | * |
||
| 127 | * @var string |
||
| 128 | */ |
||
| 129 | private $cacheDir; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Add a custom type adapter |
||
| 133 | * |
||
| 134 | * @param TypeAdapterFactory $typeAdapterFactory |
||
| 135 | * @return GsonBuilder |
||
| 136 | */ |
||
| 137 | 2 | public function addTypeAdapterFactory(TypeAdapterFactory $typeAdapterFactory): GsonBuilder |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Add custom handling for a specific type |
||
| 146 | * |
||
| 147 | * Handler objects may be of types: TypeAdapter, JsonSerializer, or JsonDeserializer |
||
| 148 | * |
||
| 149 | * @param string $type |
||
| 150 | * @param $handler |
||
| 151 | * @return GsonBuilder |
||
| 152 | * @throws \BadMethodCallException If the handler is not supported |
||
| 153 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
| 154 | */ |
||
| 155 | 6 | public function registerType(string $type, $handler): GsonBuilder |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Add an [@see InstanceCreator] for a given type |
||
| 187 | * |
||
| 188 | * @param string $type |
||
| 189 | * @param InstanceCreator $instanceCreator |
||
| 190 | * @return GsonBuilder |
||
| 191 | * @throws \Tebru\Gson\Exception\MalformedTypeException If the type cannot be parsed |
||
| 192 | */ |
||
| 193 | 1 | public function addInstanceCreator(string $type, InstanceCreator $instanceCreator): GsonBuilder |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Set the version to be used with [@see Since] and [@see Until] annotations |
||
| 203 | * |
||
| 204 | * @param string $version |
||
| 205 | * @return GsonBuilder |
||
| 206 | */ |
||
| 207 | 4 | public function setVersion(string $version): GsonBuilder |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Set the property modifiers that should be excluded based on [@see \ReflectionProperty] |
||
| 216 | * |
||
| 217 | * This number is a bitmap, so ReflectionProperty::IS_STATIC will exclude all static properties. |
||
| 218 | * Likewise, passing (ReflectionProperty::IS_STATIC | ReflectionProperty::IS_PRIVATE) will exclude |
||
| 219 | * all static and private properties. |
||
| 220 | * |
||
| 221 | * @param int $modifiers |
||
| 222 | * @return GsonBuilder |
||
| 223 | */ |
||
| 224 | 2 | public function setExcludedModifier(int $modifiers): GsonBuilder |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Require the [@see Expose] annotation to serialize or deserialize property |
||
| 233 | * |
||
| 234 | * @return GsonBuilder |
||
| 235 | */ |
||
| 236 | 2 | public function requireExposeAnnotation(): GsonBuilder |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Add an exclusion strategy that should be used during serialization/deserialization |
||
| 245 | * |
||
| 246 | * @param ExclusionStrategy $strategy |
||
| 247 | * @param bool $serialization |
||
| 248 | * @param bool $deserialization |
||
| 249 | * @return GsonBuilder |
||
| 250 | */ |
||
| 251 | 2 | public function addExclusionStrategy(ExclusionStrategy $strategy, bool $serialization, bool $deserialization): GsonBuilder |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Set a custom property naming strategy |
||
| 260 | * |
||
| 261 | * @param PropertyNamingStrategy $propertyNamingStrategy |
||
| 262 | * @return GsonBuilder |
||
| 263 | */ |
||
| 264 | 1 | public function setPropertyNamingStrategy(PropertyNamingStrategy $propertyNamingStrategy): GsonBuilder |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Set a custom method naming strategy |
||
| 273 | * |
||
| 274 | * @param MethodNamingStrategy $methodNamingStrategy |
||
| 275 | * @return GsonBuilder |
||
| 276 | */ |
||
| 277 | 1 | public function setMethodNamingStrategy(MethodNamingStrategy $methodNamingStrategy): GsonBuilder |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Set whether we should serialize null |
||
| 286 | * |
||
| 287 | * @return GsonBuilder |
||
| 288 | */ |
||
| 289 | 1 | public function serializeNull(): GsonBuilder |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Set whether caching is enabled |
||
| 298 | * |
||
| 299 | * @param bool $enableCache |
||
| 300 | * @return GsonBuilder |
||
| 301 | */ |
||
| 302 | 1 | public function enableCache(bool $enableCache): GsonBuilder |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Setting a cache directory will turn on filesystem caching |
||
| 311 | * |
||
| 312 | * @param string $cacheDir |
||
| 313 | * @return GsonBuilder |
||
| 314 | */ |
||
| 315 | 1 | public function setCacheDir(string $cacheDir): GsonBuilder |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Builds a new [@see Gson] object based on configuration set |
||
| 324 | * |
||
| 325 | * @return Gson |
||
| 326 | * @throws \InvalidArgumentException If there was a problem creating the cache |
||
| 327 | * @throws \LogicException If trying to cache without a cache directory |
||
| 328 | */ |
||
| 329 | 26 | public function build(): Gson |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Merges default factories with user provided factories |
||
| 386 | * |
||
| 387 | * @param PropertyCollectionFactory $propertyCollectionFactory |
||
| 388 | * @param Excluder $excluder |
||
| 389 | * @param AnnotationCollectionFactory $annotationCollectionFactory |
||
| 390 | * @param MetadataFactory $metadataFactory |
||
| 391 | * @return array|TypeAdapterFactory[] |
||
| 392 | */ |
||
| 393 | 25 | private function getTypeAdapterFactories( |
|
| 424 | } |
||
| 425 |