Complex classes like PropertyMetadata 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 PropertyMetadata, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class PropertyMetadata implements PropertyMetadataInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $name; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $class; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $alias; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var TypeMetadataInterface|null |
||
| 36 | */ |
||
| 37 | private $type; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | private $readable = true; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private $writable = true; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string|null |
||
| 51 | */ |
||
| 52 | private $accessor; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string|null |
||
| 56 | */ |
||
| 57 | private $mutator; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string|null |
||
| 61 | */ |
||
| 62 | private $since; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string|null |
||
| 66 | */ |
||
| 67 | private $until; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var int|null |
||
| 71 | */ |
||
| 72 | private $maxDepth; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string[] |
||
| 76 | */ |
||
| 77 | private $groups = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $xmlAttribute = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | private $xmlValue = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | private $xmlInline = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string|null |
||
| 96 | */ |
||
| 97 | private $xmlEntry; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string|null |
||
| 101 | */ |
||
| 102 | private $xmlEntryAttribute; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var bool|null |
||
| 106 | */ |
||
| 107 | private $xmlKeyAsAttribute; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var bool|null |
||
| 111 | */ |
||
| 112 | private $xmlKeyAsNode; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $name |
||
| 116 | * @param string $class |
||
| 117 | */ |
||
| 118 | 1784 | public function __construct($name, $class) |
|
| 119 | { |
||
| 120 | 1784 | $this->setName($name); |
|
| 121 | 1784 | $this->setClass($class); |
|
| 122 | 1784 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | 1524 | public function getName() |
|
| 128 | { |
||
| 129 | 1524 | return $this->name; |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | 1784 | public function setName($name) |
|
| 136 | { |
||
| 137 | 1784 | $this->name = $name; |
|
| 138 | 1784 | } |
|
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | 8 | public function getClass() |
|
| 144 | { |
||
| 145 | 8 | return $this->class; |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | 1784 | public function setClass($class) |
|
| 152 | { |
||
| 153 | 1784 | $this->class = $class; |
|
| 154 | 1784 | } |
|
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | 1468 | public function hasAlias() |
|
| 160 | { |
||
| 161 | 1468 | return $this->alias !== null; |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | 508 | public function getAlias() |
|
| 168 | { |
||
| 169 | 508 | return $this->alias; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | 152 | public function setAlias($alias) |
|
| 176 | { |
||
| 177 | 152 | $this->alias = $alias; |
|
| 178 | 152 | } |
|
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | 380 | public function hasType() |
|
| 184 | { |
||
| 185 | 380 | return $this->type !== null; |
|
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | 1188 | public function getType() |
|
| 192 | { |
||
| 193 | 1188 | return $this->type; |
|
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | 1216 | public function setType(TypeMetadataInterface $type = null) |
|
| 200 | { |
||
| 201 | 1216 | $this->type = $type; |
|
| 202 | 1216 | } |
|
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | 1052 | public function isReadable() |
|
| 208 | { |
||
| 209 | 1052 | return $this->readable; |
|
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | 1708 | public function setReadable($readable) |
|
| 216 | { |
||
| 217 | 1708 | $this->readable = $readable; |
|
| 218 | 1708 | } |
|
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | 768 | public function isWritable() |
|
| 224 | { |
||
| 225 | 768 | return $this->writable; |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | 1704 | public function setWritable($writable) |
|
| 232 | { |
||
| 233 | 1704 | $this->writable = $writable; |
|
| 234 | 1704 | } |
|
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | 1052 | public function hasAccessor() |
|
| 240 | { |
||
| 241 | 1052 | return $this->accessor !== null; |
|
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | 396 | public function getAccessor() |
|
| 248 | { |
||
| 249 | 396 | return $this->accessor; |
|
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | 56 | public function setAccessor($accessor) |
|
| 256 | { |
||
| 257 | 56 | $this->accessor = $accessor; |
|
| 258 | 56 | } |
|
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | 708 | public function hasMutator() |
|
| 264 | { |
||
| 265 | 708 | return $this->mutator !== null; |
|
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | 396 | public function getMutator() |
|
| 272 | { |
||
| 273 | 396 | return $this->mutator; |
|
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | */ |
||
| 279 | 56 | public function setMutator($mutator) |
|
| 280 | { |
||
| 281 | 56 | $this->mutator = $mutator; |
|
| 282 | 56 | } |
|
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | 444 | public function hasSinceVersion() |
|
| 288 | { |
||
| 289 | 444 | return $this->since !== null; |
|
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | 444 | public function getSinceVersion() |
|
| 296 | { |
||
| 297 | 444 | return $this->since; |
|
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | 120 | public function setSinceVersion($since) |
|
| 304 | { |
||
| 305 | 120 | $this->since = $since; |
|
| 306 | 120 | } |
|
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | 444 | public function hasUntilVersion() |
|
| 312 | { |
||
| 313 | 444 | return $this->until !== null; |
|
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | 444 | public function getUntilVersion() |
|
| 320 | { |
||
| 321 | 444 | return $this->until; |
|
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * {@inheritdoc} |
||
| 326 | */ |
||
| 327 | 120 | public function setUntilVersion($until) |
|
| 328 | { |
||
| 329 | 120 | $this->until = $until; |
|
| 330 | 120 | } |
|
| 331 | |||
| 332 | /** |
||
| 333 | * {@inheritdoc} |
||
| 334 | */ |
||
| 335 | 412 | public function hasMaxDepth() |
|
| 336 | { |
||
| 337 | 412 | return $this->maxDepth !== null; |
|
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritdoc} |
||
| 342 | */ |
||
| 343 | 412 | public function getMaxDepth() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * {@inheritdoc} |
||
| 350 | */ |
||
| 351 | 88 | public function setMaxDepth($maxDepth) |
|
| 352 | { |
||
| 353 | 88 | $this->maxDepth = $maxDepth; |
|
| 354 | 88 | } |
|
| 355 | |||
| 356 | /** |
||
| 357 | * {@inheritdoc} |
||
| 358 | */ |
||
| 359 | 388 | public function hasGroups() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritdoc} |
||
| 366 | */ |
||
| 367 | 392 | public function getGroups() |
|
| 368 | { |
||
| 369 | 392 | return array_keys($this->groups); |
|
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * {@inheritdoc} |
||
| 374 | */ |
||
| 375 | 12 | public function setGroups(array $groups) |
|
| 376 | { |
||
| 377 | 12 | $this->groups = []; |
|
| 378 | 12 | $this->addGroups($groups); |
|
| 379 | 12 | } |
|
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritdoc} |
||
| 383 | */ |
||
| 384 | 12 | public function addGroups(array $groups) |
|
| 385 | { |
||
| 386 | 12 | foreach ($groups as $group) { |
|
| 387 | 12 | $this->addGroup($group); |
|
| 388 | 6 | } |
|
| 389 | 12 | } |
|
| 390 | |||
| 391 | /** |
||
| 392 | * {@inheritdoc} |
||
| 393 | */ |
||
| 394 | 148 | public function hasGroup($group) |
|
| 395 | { |
||
| 396 | 148 | return isset($this->groups[$group]); |
|
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * {@inheritdoc} |
||
| 401 | */ |
||
| 402 | 148 | public function addGroup($group) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * {@inheritdoc} |
||
| 411 | */ |
||
| 412 | 4 | public function removeGroup($group) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * {@inheritdoc} |
||
| 419 | */ |
||
| 420 | 640 | public function isXmlAttribute() |
|
| 424 | |||
| 425 | /** |
||
| 426 | * {@inheritdoc} |
||
| 427 | */ |
||
| 428 | 100 | public function setXmlAttribute($xmlAttribute) |
|
| 429 | { |
||
| 430 | 100 | $this->xmlAttribute = $xmlAttribute; |
|
| 431 | 100 | } |
|
| 432 | |||
| 433 | /** |
||
| 434 | * {@inheritdoc} |
||
| 435 | */ |
||
| 436 | 640 | public function isXmlValue() |
|
| 437 | { |
||
| 438 | 640 | return $this->xmlValue; |
|
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * {@inheritdoc} |
||
| 443 | */ |
||
| 444 | 52 | public function setXmlValue($xmlValue) |
|
| 445 | { |
||
| 446 | 52 | $this->xmlValue = $xmlValue; |
|
| 447 | 52 | } |
|
| 448 | |||
| 449 | /** |
||
| 450 | * {@inheritdoc} |
||
| 451 | */ |
||
| 452 | 636 | public function isXmlInline() |
|
| 453 | { |
||
| 454 | 636 | return $this->xmlInline; |
|
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * {@inheritdoc} |
||
| 459 | */ |
||
| 460 | 52 | public function setXmlInline($xmlInline) |
|
| 461 | { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * {@inheritdoc} |
||
| 467 | */ |
||
| 468 | 40 | public function hasXmlEntry() |
|
| 472 | |||
| 473 | /** |
||
| 474 | * {@inheritdoc} |
||
| 475 | */ |
||
| 476 | 376 | public function getXmlEntry() |
|
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | 48 | public function setXmlEntry($xmlEntry) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * {@inheritdoc} |
||
| 491 | */ |
||
| 492 | 40 | public function hasXmlEntryAttribute() |
|
| 496 | |||
| 497 | /** |
||
| 498 | * {@inheritdoc} |
||
| 499 | */ |
||
| 500 | 376 | public function getXmlEntryAttribute() |
|
| 504 | |||
| 505 | /** |
||
| 506 | * {@inheritdoc} |
||
| 507 | */ |
||
| 508 | 48 | public function setXmlEntryAttribute($xmlEntryAttribute) |
|
| 512 | |||
| 513 | /** |
||
| 514 | * {@inheritdoc} |
||
| 515 | */ |
||
| 516 | 40 | public function hasXmlKeyAsAttribute() |
|
| 520 | |||
| 521 | /** |
||
| 522 | * {@inheritdoc} |
||
| 523 | */ |
||
| 524 | 376 | public function useXmlKeyAsAttribute() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * {@inheritdoc} |
||
| 531 | */ |
||
| 532 | 48 | public function setXmlKeyAsAttribute($xmlKeyAsAttribute) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * {@inheritdoc} |
||
| 539 | */ |
||
| 540 | 32 | public function hasXmlKeyAsNode() |
|
| 544 | |||
| 545 | /** |
||
| 546 | * {@inheritdoc} |
||
| 547 | */ |
||
| 548 | 376 | public function useXmlKeyAsNode() |
|
| 552 | |||
| 553 | /** |
||
| 554 | * {@inheritdoc} |
||
| 555 | */ |
||
| 556 | 48 | public function setXmlKeyAsNode($xmlKeyAsNode) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * {@inheritdoc} |
||
| 563 | */ |
||
| 564 | 4 | public function merge(PropertyMetadataInterface $propertyMetadata) |
|
| 620 | |||
| 621 | /** |
||
| 622 | * {@inheritdoc} |
||
| 623 | */ |
||
| 624 | 4 | public function serialize() |
|
| 648 | |||
| 649 | /** |
||
| 650 | * {@inheritdoc} |
||
| 651 | */ |
||
| 652 | 4 | public function unserialize($serialized) |
|
| 676 | } |
||
| 677 |