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:
Complex classes like AbstractPropertySet 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 AbstractPropertySet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class AbstractPropertySet implements PropertySetInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var PropertySetSchema |
||
| 21 | */ |
||
| 22 | protected $schema; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param string $property |
||
| 26 | * |
||
| 27 | * @return boolean |
||
| 28 | */ |
||
| 29 | public function isSettable($property) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param array $config |
||
| 36 | * @param array $args |
||
| 37 | * |
||
| 38 | * @return $this |
||
| 39 | */ |
||
| 40 | public function init(array $config = [], array $args = []) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param integer $type |
||
| 47 | * |
||
| 48 | * @return boolean |
||
| 49 | * |
||
| 50 | * @throws \OldTown\PropertySet\Exception\PropertyException |
||
| 51 | */ |
||
| 52 | public function supportsType($type) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * |
||
| 59 | */ |
||
| 60 | public function __toString() |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $key |
||
| 84 | * @param mixed $value |
||
| 85 | * |
||
| 86 | * @return $this |
||
| 87 | * |
||
| 88 | * @throws Exception\PropertyException |
||
| 89 | */ |
||
| 90 | public function setAsActualType($key, $value) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $key |
||
| 119 | * |
||
| 120 | * @return mixed |
||
| 121 | * |
||
| 122 | * @throws Exception\PropertyException |
||
| 123 | */ |
||
| 124 | public function getAsActualType($key) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $key |
||
| 172 | * @param object $value |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | * |
||
| 176 | * @throws Exception\PropertyException |
||
| 177 | */ |
||
| 178 | View Code Duplication | public function setObject($key, $value) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param string $key |
||
| 191 | * |
||
| 192 | * @return object |
||
| 193 | * |
||
| 194 | * @throws Exception\PropertyException |
||
| 195 | */ |
||
| 196 | View Code Duplication | public function getObject($key) |
|
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $key |
||
| 212 | * @param array|Traversable $value |
||
| 213 | * |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | * |
||
| 217 | * @throws Exception\PropertyException |
||
| 218 | */ |
||
| 219 | public function setArray($key, $value) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $key |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | * |
||
| 245 | * @throws Exception\PropertyException |
||
| 246 | */ |
||
| 247 | View Code Duplication | public function getArray($key) |
|
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $key |
||
| 263 | * @param string $value |
||
| 264 | * |
||
| 265 | * @return $this |
||
| 266 | * |
||
| 267 | * @throws Exception\PropertyException |
||
| 268 | */ |
||
| 269 | View Code Duplication | public function setData($key, $value) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $key |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | * |
||
| 285 | * @throws Exception\PropertyException |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function getData($key) |
|
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $key |
||
| 303 | * @param DOMDocument $value |
||
| 304 | * |
||
| 305 | * @return $this |
||
| 306 | * @throws Exception\PropertyException |
||
| 307 | */ |
||
| 308 | public function setXML($key, DOMDocument $value) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param string $key |
||
| 317 | * |
||
| 318 | * @return DOMDocument |
||
| 319 | * |
||
| 320 | * @throws Exception\PropertyException |
||
| 321 | */ |
||
| 322 | View Code Duplication | public function getXML($key) |
|
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * @param String $key |
||
| 338 | * @param DateTime $value |
||
| 339 | * |
||
| 340 | * @return $this |
||
| 341 | * |
||
| 342 | * @throws Exception\PropertyException |
||
| 343 | */ |
||
| 344 | public function setDate($key, DateTime $value) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param string $key |
||
| 353 | * |
||
| 354 | * @return DateTime |
||
| 355 | * |
||
| 356 | * @throws Exception\PropertyException |
||
| 357 | */ |
||
| 358 | View Code Duplication | public function getDate($key) |
|
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $key |
||
| 374 | * @param string $value |
||
| 375 | * |
||
| 376 | * @return $this |
||
| 377 | * |
||
| 378 | * @throws Exception\PropertyException |
||
| 379 | */ |
||
| 380 | public function setString($key, $value) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param string $key |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | * |
||
| 400 | * @throws Exception\PropertyException |
||
| 401 | */ |
||
| 402 | View Code Duplication | public function getString($key) |
|
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $key |
||
| 418 | * @param boolean $value |
||
| 419 | * |
||
| 420 | * @return $this |
||
| 421 | * |
||
| 422 | * @throws Exception\PropertyException |
||
| 423 | */ |
||
| 424 | View Code Duplication | public function setBoolean($key, $value) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @param String $key |
||
| 437 | * |
||
| 438 | * @return boolean |
||
| 439 | * |
||
| 440 | * @throws Exception\PropertyException |
||
| 441 | */ |
||
| 442 | View Code Duplication | public function getBoolean($key) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * @param string $key |
||
| 457 | * @param integer $value |
||
| 458 | * |
||
| 459 | * @return $this |
||
| 460 | * |
||
| 461 | * @throws Exception\PropertyException |
||
| 462 | */ |
||
| 463 | View Code Duplication | public function setInt($key, $value) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * @param string $key |
||
| 476 | * |
||
| 477 | * @return integer |
||
| 478 | * |
||
| 479 | * @throws Exception\PropertyException |
||
| 480 | */ |
||
| 481 | View Code Duplication | public function getInt($key) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * @param string $key |
||
| 496 | * @param float $value |
||
| 497 | * |
||
| 498 | * @return $this |
||
| 499 | * |
||
| 500 | * @throws Exception\PropertyException |
||
| 501 | */ |
||
| 502 | View Code Duplication | public function setFloat($key, $value) |
|
| 512 | |||
| 513 | /** |
||
| 514 | * @param string $key |
||
| 515 | * |
||
| 516 | * @return float |
||
| 517 | */ |
||
| 518 | View Code Duplication | public function getFloat($key) |
|
| 530 | |||
| 531 | |||
| 532 | /** |
||
| 533 | * @return PropertySetSchema |
||
| 534 | */ |
||
| 535 | public function getSchema() |
||
| 539 | |||
| 540 | |||
| 541 | /** |
||
| 542 | * @param PropertySetSchema $schema |
||
| 543 | * |
||
| 544 | * @return $this |
||
| 545 | */ |
||
| 546 | public function setSchema(PropertySetSchema $schema) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @return boolean |
||
| 555 | */ |
||
| 556 | public function supportsTypes() |
||
| 560 | |||
| 561 | |||
| 562 | /** |
||
| 563 | * @param integer $type |
||
| 564 | * @param string $key |
||
| 565 | * @param mixed $value |
||
| 566 | * |
||
| 567 | * @return $this |
||
| 568 | * |
||
| 569 | * @throws Exception\IllegalPropertyException |
||
| 570 | * @throws Exception\InvalidPropertyTypeException |
||
| 571 | * @throws \OldTown\PropertySet\Exception\PropertyException |
||
| 572 | */ |
||
| 573 | protected function set($type, $key, $value) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @param integer $type |
||
| 605 | * @param string $key |
||
| 606 | * @param mixed $value |
||
| 607 | * |
||
| 608 | * @return $this |
||
| 609 | * |
||
| 610 | * @throws Exception\PropertyException |
||
| 611 | */ |
||
| 612 | protected function setImpl($type, $key, $value) |
||
| 615 | |||
| 616 | |||
| 617 | /** |
||
| 618 | * @param integer $type |
||
| 619 | * @param string $key |
||
| 620 | * |
||
| 621 | * @return mixed |
||
| 622 | * |
||
| 623 | * @throws Exception\InvalidPropertyTypeException |
||
| 624 | * @throws \OldTown\PropertySet\Exception\PropertyException |
||
| 625 | */ |
||
| 626 | protected function get(/** @noinspection PhpUnusedParameterInspection */ $type, $key) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param integer|string $type |
||
| 637 | * |
||
| 638 | * @return integer|string |
||
| 639 | * |
||
| 640 | * @throws \OldTown\PropertySet\Exception\RuntimeException |
||
| 641 | */ |
||
| 642 | protected function type($type) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @param integer $type |
||
| 658 | * |
||
| 659 | * @return string |
||
| 660 | */ |
||
| 661 | protected function getTypeByCode($type) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * @param string|null $type |
||
| 690 | * |
||
| 691 | * @return integer |
||
| 692 | * |
||
| 693 | * @throws \OldTown\PropertySet\Exception\RuntimeException |
||
| 694 | */ |
||
| 695 | protected function getTypeByName($type = null) |
||
| 738 | } |
||
| 739 |