| Total Complexity | 70 |
| Total Lines | 462 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Excluder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Excluder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | final class Excluder |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Version, if set, will be used with [@see Since] and [@see Until] annotations |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $version; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Which modifiers are excluded |
||
| 45 | * |
||
| 46 | * By default only static properties are excluded |
||
| 47 | * |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | private $excludedModifiers = ReflectionProperty::IS_STATIC; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * If this is true, properties will need to explicitly have an [@see Expose] annotation |
||
| 54 | * to be serialized or deserialized |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | private $requireExpose = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Class Exclusion strategies during serialization |
||
| 62 | * |
||
| 63 | * @var ClassSerializationExclusionStrategy[] |
||
| 64 | */ |
||
| 65 | private $classSerializationStrategies = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Property Exclusion strategies during serialization |
||
| 69 | * |
||
| 70 | * @var PropertySerializationExclusionStrategy[] |
||
| 71 | */ |
||
| 72 | private $propertySerializationStrategies = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Class Exclusion strategies during deserialization |
||
| 76 | * |
||
| 77 | * @var ClassDeserializationExclusionStrategy[] |
||
| 78 | */ |
||
| 79 | private $classDeserializationStrategies = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Property Exclusion strategies during deserialization |
||
| 83 | * |
||
| 84 | * @var PropertyDeserializationExclusionStrategy[] |
||
| 85 | */ |
||
| 86 | private $propertyDeserializationStrategies = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Exclusion strategies that can be cached |
||
| 90 | * |
||
| 91 | * @var ExclusionStrategy[] |
||
| 92 | */ |
||
| 93 | private $cachedStrategies = []; |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * Set the version to test against |
||
| 98 | * |
||
| 99 | * @param string $version |
||
| 100 | * @return Excluder |
||
| 101 | */ |
||
| 102 | public function setVersion(?string $version): Excluder |
||
| 103 | { |
||
| 104 | $this->version = $version; |
||
| 105 | |||
| 106 | return $this; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Set an integer representing the property modifiers that should be excluded |
||
| 111 | * |
||
| 112 | * @param int $modifiers |
||
| 113 | * @return Excluder |
||
| 114 | */ |
||
| 115 | public function setExcludedModifiers(int $modifiers): Excluder |
||
| 116 | { |
||
| 117 | $this->excludedModifiers = $modifiers; |
||
| 118 | |||
| 119 | return $this; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Require the [@see Expose] annotation to serialize properties |
||
| 124 | * |
||
| 125 | * @param bool $requireExpose |
||
| 126 | * @return Excluder |
||
| 127 | */ |
||
| 128 | public function setRequireExpose(bool $requireExpose): Excluder |
||
| 129 | { |
||
| 130 | $this->requireExpose = $requireExpose; |
||
| 131 | |||
| 132 | return $this; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Add an exclusion strategy |
||
| 137 | * |
||
| 138 | * @param ExclusionStrategy $strategy |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | public function addExclusionStrategy(ExclusionStrategy $strategy): void |
||
| 142 | { |
||
| 143 | if ($strategy instanceof ClassSerializationExclusionStrategy) { |
||
| 144 | $this->classSerializationStrategies[] = $strategy; |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($strategy instanceof PropertySerializationExclusionStrategy) { |
||
| 148 | $this->propertySerializationStrategies[] = $strategy; |
||
| 149 | } |
||
| 150 | |||
| 151 | if ($strategy instanceof ClassDeserializationExclusionStrategy) { |
||
| 152 | $this->classDeserializationStrategies[] = $strategy; |
||
| 153 | } |
||
| 154 | |||
| 155 | if ($strategy instanceof PropertyDeserializationExclusionStrategy) { |
||
| 156 | $this->propertyDeserializationStrategies[] = $strategy; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Add an exclusion strategy that can be cached |
||
| 162 | * |
||
| 163 | * @param ExclusionStrategy $strategy |
||
| 164 | */ |
||
| 165 | public function addCachedExclusionStrategy(ExclusionStrategy $strategy): void |
||
| 166 | { |
||
| 167 | $this->cachedStrategies[] = $strategy; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Compile time exclusion checking of classes to determine if we should exclude during serialization |
||
| 172 | * |
||
| 173 | * @param ClassMetadata $classMetadata |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function excludeClassSerialize(ClassMetadata $classMetadata): bool |
||
| 177 | { |
||
| 178 | foreach ($this->cachedStrategies as $strategy) { |
||
| 179 | if ($strategy instanceof ClassSerializationExclusionStrategy && $strategy->skipSerializingClass($classMetadata)) { |
||
| 180 | return true; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | return $this->excludeByAnnotation($classMetadata->getAnnotations(), true); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Compile time exclusion checking of classes to determine if we should exclude during deserialization |
||
| 189 | * |
||
| 190 | * @param ClassMetadata $classMetadata |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function excludeClassDeserialize(ClassMetadata $classMetadata): bool |
||
| 194 | { |
||
| 195 | foreach ($this->cachedStrategies as $strategy) { |
||
| 196 | if ($strategy instanceof ClassDeserializationExclusionStrategy && $strategy->skipDeserializingClass($classMetadata)) { |
||
| 197 | return true; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | return $this->excludeByAnnotation($classMetadata->getAnnotations(), false); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Add [@see SerializationExclusionData] to class exclusion strategies |
||
| 206 | * |
||
| 207 | * @param SerializationExclusionData $exclusionData |
||
| 208 | */ |
||
| 209 | public function applyClassSerializationExclusionData(SerializationExclusionData $exclusionData): void |
||
| 210 | { |
||
| 211 | foreach ($this->classSerializationStrategies as $exclusionStrategy) { |
||
| 212 | if ($exclusionStrategy instanceof SerializationExclusionDataAware) { |
||
| 213 | $exclusionStrategy->setSerializationExclusionData($exclusionData); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Add [@see SerializationExclusionData] to property exclusion strategies |
||
| 220 | * |
||
| 221 | * @param SerializationExclusionData $exclusionData |
||
| 222 | */ |
||
| 223 | public function applyPropertySerializationExclusionData(SerializationExclusionData $exclusionData): void |
||
| 224 | { |
||
| 225 | foreach ($this->propertySerializationStrategies as $exclusionStrategy) { |
||
| 226 | if ($exclusionStrategy instanceof SerializationExclusionDataAware) { |
||
| 227 | $exclusionStrategy->setSerializationExclusionData($exclusionData); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Add [@see SerializationExclusionData] to class deserialization strategies |
||
| 234 | * |
||
| 235 | * @param DeserializationExclusionData $exclusionData |
||
| 236 | */ |
||
| 237 | public function applyClassDeserializationExclusionData(DeserializationExclusionData $exclusionData): void |
||
| 238 | { |
||
| 239 | foreach ($this->classDeserializationStrategies as $exclusionStrategy) { |
||
| 240 | if ($exclusionStrategy instanceof DeserializationExclusionDataAware) { |
||
| 241 | $exclusionStrategy->setDeserializationExclusionData($exclusionData); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Add [@see SerializationExclusionData] to property deserialization strategies |
||
| 248 | * |
||
| 249 | * @param DeserializationExclusionData $exclusionData |
||
| 250 | */ |
||
| 251 | public function applyPropertyDeserializationExclusionData(DeserializationExclusionData $exclusionData): void |
||
| 252 | { |
||
| 253 | foreach ($this->propertyDeserializationStrategies as $exclusionStrategy) { |
||
| 254 | if ($exclusionStrategy instanceof DeserializationExclusionDataAware) { |
||
| 255 | $exclusionStrategy->setDeserializationExclusionData($exclusionData); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Runtime exclusion checking of classes by strategy during serialization |
||
| 262 | * |
||
| 263 | * Uses user-defined strategies |
||
| 264 | * |
||
| 265 | * @param ClassMetadata $classMetadata |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | public function excludeClassBySerializationStrategy(ClassMetadata $classMetadata): bool |
||
| 269 | { |
||
| 270 | foreach ($this->classSerializationStrategies as $exclusionStrategy) { |
||
| 271 | if ($exclusionStrategy->skipSerializingClass($classMetadata)) { |
||
| 272 | return true; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | return false; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Runtime exclusion checking of classes by strategy during deserialization |
||
| 281 | * |
||
| 282 | * Uses user-defined strategies |
||
| 283 | * |
||
| 284 | * @param ClassMetadata $classMetadata |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public function excludeClassByDeserializationStrategy(ClassMetadata $classMetadata): bool |
||
| 288 | { |
||
| 289 | foreach ($this->classDeserializationStrategies as $exclusionStrategy) { |
||
| 290 | if ($exclusionStrategy->skipDeserializingClass($classMetadata)) { |
||
| 291 | return true; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | return false; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Compile time exclusion checking of properties |
||
| 300 | * |
||
| 301 | * @param PropertyMetadata $propertyMetadata |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | public function excludePropertySerialize(PropertyMetadata $propertyMetadata): bool |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Compile time exclusion checking of properties |
||
| 322 | * |
||
| 323 | * @param PropertyMetadata $propertyMetadata |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | public function excludePropertyDeserialize(PropertyMetadata $propertyMetadata): bool |
||
| 327 | { |
||
| 328 | // exclude the property if the property modifiers are found in the excluded modifiers |
||
| 329 | if (0 !== ($this->excludedModifiers & $propertyMetadata->getModifiers())) { |
||
| 330 | return true; |
||
| 331 | } |
||
| 332 | |||
| 333 | foreach ($this->cachedStrategies as $strategy) { |
||
| 334 | if ($strategy instanceof PropertyDeserializationExclusionStrategy && $strategy->skipDeserializingProperty($propertyMetadata)) { |
||
| 335 | return true; |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | return $this->excludeByAnnotation($propertyMetadata->getAnnotations(), false); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Runtime exclusion checking of properties by strategy during serialization |
||
| 344 | * |
||
| 345 | * Uses user-defined strategies |
||
| 346 | * |
||
| 347 | * @param PropertyMetadata $property |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | public function excludePropertyBySerializationStrategy(PropertyMetadata $property): bool |
||
| 351 | { |
||
| 352 | foreach ($this->propertySerializationStrategies as $exclusionStrategy) { |
||
| 353 | if ($exclusionStrategy->skipSerializingProperty($property)) { |
||
| 354 | return true; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | return false; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Runtime exclusion checking of properties by strategy during deserialization |
||
| 363 | * |
||
| 364 | * Uses user-defined strategies |
||
| 365 | * |
||
| 366 | * @param PropertyMetadata $property |
||
| 367 | * @return bool |
||
| 368 | */ |
||
| 369 | public function excludePropertyByDeserializationStrategy(PropertyMetadata $property): bool |
||
| 370 | { |
||
| 371 | foreach ($this->propertyDeserializationStrategies as $exclusionStrategy) { |
||
| 372 | if ($exclusionStrategy->skipDeserializingProperty($property)) { |
||
| 373 | return true; |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | return false; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Returns true if class serialization strategies exist |
||
| 382 | * |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | public function hasClassSerializationStrategies(): bool |
||
| 386 | { |
||
| 387 | return $this->classSerializationStrategies !== []; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Returns true if property serialization strategies exist |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | public function hasPropertySerializationStrategies(): bool |
||
| 396 | { |
||
| 397 | return $this->propertySerializationStrategies !== []; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns true if class deserialization strategies exist |
||
| 402 | * |
||
| 403 | * @return bool |
||
| 404 | */ |
||
| 405 | public function hasClassDeserializationStrategies(): bool |
||
| 406 | { |
||
| 407 | return $this->classDeserializationStrategies !== []; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Returns true if property deserialization strategies exist |
||
| 412 | * |
||
| 413 | * @return bool |
||
| 414 | */ |
||
| 415 | public function hasPropertyDeserializationStrategies(): bool |
||
| 416 | { |
||
| 417 | return $this->propertyDeserializationStrategies !== []; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Checks various annotations to see if the property should be excluded |
||
| 422 | * |
||
| 423 | * - [@see Since] / [@see Until] |
||
| 424 | * - [@see Exclude] |
||
| 425 | * - [@see Expose] (if requireExpose is set) |
||
| 426 | * |
||
| 427 | * @param AnnotationCollection $annotations |
||
| 428 | * @param bool $serialize |
||
| 429 | * @return bool |
||
| 430 | */ |
||
| 431 | private function excludeByAnnotation(AnnotationCollection $annotations, bool $serialize): bool |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Returns true if the set version is valid for [@see Since] and [@see Until] annotations |
||
| 457 | * |
||
| 458 | * @param AnnotationCollection $annotations |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | private function validVersion(AnnotationCollection $annotations): bool |
||
| 462 | { |
||
| 463 | return !$this->shouldSkipSince($annotations) && !$this->shouldSkipUntil($annotations); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Returns true if we should skip based on the [@see Since] annotation |
||
| 468 | * |
||
| 469 | * @param AnnotationCollection $annotations |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | private function shouldSkipSince(AnnotationCollection $annotations): bool |
||
| 473 | { |
||
| 474 | $sinceAnnotation = $annotations->get(Since::class); |
||
| 475 | |||
| 476 | return |
||
| 477 | null !== $sinceAnnotation |
||
| 478 | && null !== $this->version |
||
| 479 | && \version_compare($this->version, $sinceAnnotation->getValue(), '<'); |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Returns true if we should skip based on the [@see Until] annotation |
||
| 484 | * |
||
| 485 | * @param AnnotationCollection $annotations |
||
| 486 | * @return bool |
||
| 487 | */ |
||
| 488 | private function shouldSkipUntil(AnnotationCollection $annotations): bool |
||
| 496 | } |
||
| 497 | } |
||
| 498 |