Complex classes like SWLChangeSet 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 SWLChangeSet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class SWLChangeSet { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The subject the changes apply to. |
||
| 18 | * |
||
| 19 | * @var SMWDIWikiPage |
||
| 20 | */ |
||
| 21 | private $subject; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Object holding semantic data that got inserted. |
||
| 25 | * |
||
| 26 | * @var SMWSemanticData |
||
| 27 | */ |
||
| 28 | private $insertions; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Object holding semantic data that got deleted. |
||
| 32 | * |
||
| 33 | * @var SMWSemanticData |
||
| 34 | */ |
||
| 35 | private $deletions; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * List of all changes(, not including insertions and deletions). |
||
| 39 | * |
||
| 40 | * @var SWLPropertyChanges |
||
| 41 | */ |
||
| 42 | private $changes; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * DB ID of the change set (swl_sets.set_id). |
||
| 46 | * |
||
| 47 | * @var integer |
||
| 48 | */ |
||
| 49 | private $id; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The title of the page the changeset holds changes for. |
||
| 53 | * The title will be constructed from the subject of the SMWChangeSet |
||
| 54 | * the first time getTitle is called, so it should be accessed via this |
||
| 55 | * method. |
||
| 56 | * |
||
| 57 | * @var Title or false |
||
| 58 | */ |
||
| 59 | private $title = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The edit this set of changes belongs to. |
||
| 63 | * |
||
| 64 | * @var SWLEdit |
||
| 65 | */ |
||
| 66 | private $edit; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Creates and returns a new SWLChangeSet instance from a database result |
||
| 70 | * obtained by doing a select on swl_sets. |
||
| 71 | * |
||
| 72 | * @since 0.1 |
||
| 73 | * |
||
| 74 | * @param $set |
||
| 75 | * |
||
| 76 | * @return SWLChangeSet |
||
| 77 | */ |
||
| 78 | public static function newFromDBResult( $set ) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Creates and returns a new SWLChangeSet instance from a database result |
||
| 122 | * obtained by doing a select on swl_sets. |
||
| 123 | * |
||
| 124 | * @since 0.1 |
||
| 125 | * |
||
| 126 | * @param array $changeSetArray |
||
| 127 | * |
||
| 128 | * @return SWLChangeSet |
||
| 129 | */ |
||
| 130 | public static function newFromArray( array $changeSetArray ) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Creates and returns a new SMWChangeSet from 2 SMWSemanticData objects. |
||
| 165 | * |
||
| 166 | * @param SMWSemanticData $old |
||
| 167 | * @param SMWSemanticData $new |
||
| 168 | * @param array $filterProperties Optional list of properties (string serializations) to filter on. Null for no filtering. |
||
| 169 | * |
||
| 170 | * @return SMWChangeSet |
||
| 171 | */ |
||
| 172 | public static function newFromSemanticData( SMWSemanticData $old, SMWSemanticData $new, array $filterProperties = null ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Finds the inserts or deletions and adds them to the passed SMWSemanticData object. |
||
| 258 | * These values will also be removed from the first list of properties and their values, |
||
| 259 | * so it can be used for one-to-one change finding later on. |
||
| 260 | * |
||
| 261 | * @param SMWSemanticData $changeSet |
||
| 262 | * @param array $oldProperties |
||
| 263 | * @param SMWSemanticData $oldData |
||
| 264 | * @param array $newProperties |
||
| 265 | */ |
||
| 266 | private static function findSingleDirectionChanges( SMWSemanticData &$changeSet, |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Create a new instance of a change set. |
||
| 287 | * |
||
| 288 | * @param SMWDIWikiPage $subject |
||
| 289 | * @param SWLPropertyChanges $changes Can be null |
||
| 290 | * @param SMWSemanticData $insertions Can be null |
||
| 291 | * @param SMWSemanticData $deletions Can be null |
||
| 292 | * @param integer $id Can be null |
||
| 293 | * @param SWLEdit $edit Can be null |
||
| 294 | */ |
||
| 295 | public function __construct( SMWDIWikiPage $subject, /* SWLPropertyChanges */ $changes = null, |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Rteurns if the change set contains (changes for) user defined properties. |
||
| 310 | * |
||
| 311 | * @since 0.1 |
||
| 312 | * |
||
| 313 | * @return boolean |
||
| 314 | */ |
||
| 315 | public function hasUserDefinedProperties() { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns whether the set contains any changes. |
||
| 329 | * |
||
| 330 | * @since 0.1 |
||
| 331 | * |
||
| 332 | * @return boolean |
||
| 333 | */ |
||
| 334 | public function hasChanges() { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns a SMWSemanticData object holding all inserted SMWDataItem objects. |
||
| 342 | * |
||
| 343 | * @return SMWSemanticData |
||
| 344 | */ |
||
| 345 | public function getInsertions() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Returns a SMWSemanticData object holding all deleted SMWDataItem objects. |
||
| 351 | * |
||
| 352 | * @return SMWSemanticData |
||
| 353 | */ |
||
| 354 | public function getDeletions() { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Returns a SWLPropertyChanges object holding all SWLPropertyChange objects. |
||
| 360 | * |
||
| 361 | * @return SWLPropertyChanges |
||
| 362 | */ |
||
| 363 | public function getChanges() { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns the subject these changes apply to. |
||
| 369 | * |
||
| 370 | * @return SMWDIWikiPage |
||
| 371 | */ |
||
| 372 | public function getSubject() { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Adds a SWLPropertyChange to the set for the specified SMWDIProperty. |
||
| 378 | * |
||
| 379 | * @since 0.1 |
||
| 380 | * |
||
| 381 | * @param SMWDIProperty $property |
||
| 382 | * @param SWLPropertyChange $change |
||
| 383 | */ |
||
| 384 | public function addChange( SMWDIProperty $property, SWLPropertyChange $change ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Adds a SMWDataItem representing an insertion to the set for the specified SMWDIProperty. |
||
| 400 | * |
||
| 401 | * @since 0.1 |
||
| 402 | * |
||
| 403 | * @param SMWDIProperty $property |
||
| 404 | * @param SMWDataItem $dataItem |
||
| 405 | */ |
||
| 406 | public function addInsertion( SMWDIProperty $property, SMWDataItem $dataItem ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Adds a SMWDataItem representing a deletion to the set for the specified SMWDIProperty. |
||
| 412 | * |
||
| 413 | * @since 0.1 |
||
| 414 | * |
||
| 415 | * @param SMWDIProperty $property |
||
| 416 | * @param SMWDataItem $dataItem |
||
| 417 | */ |
||
| 418 | public function addDeletion( SMWDIProperty $property, SMWDataItem $dataItem ) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Returns a list of all properties. |
||
| 424 | * |
||
| 425 | * @return array of SMWDIProperty |
||
| 426 | */ |
||
| 427 | public function getAllProperties() { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Removes all changes for a certian property. |
||
| 437 | * |
||
| 438 | * @param SMWDIProperty $property |
||
| 439 | */ |
||
| 440 | public function removeChangesForProperty( SMWDIProperty $property ) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Returns a list of ALL changes, including isertions and deletions. |
||
| 448 | * |
||
| 449 | * @param SMWDIProperty $property |
||
| 450 | * |
||
| 451 | * @return array of SWLPropertyChange |
||
| 452 | */ |
||
| 453 | public function getAllPropertyChanges( SMWDIProperty $property ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Serializes the object as an associative array, which can be passed |
||
| 473 | * to newFromArray to create a new instance. |
||
| 474 | * |
||
| 475 | * @since 0.1 |
||
| 476 | * |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | public function toArray() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Save the change set to the database. |
||
| 514 | * |
||
| 515 | * @since 0.1 |
||
| 516 | * |
||
| 517 | * @param array of SWLGroup |
||
| 518 | * @param integer $editId |
||
| 519 | * |
||
| 520 | * @return integer ID of the inserted row (0 if nothing was inserted). |
||
| 521 | */ |
||
| 522 | public function writeToStore( array $groupsToAssociate, $editId ) { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Gets the title of the page these changes belong to. |
||
| 618 | * |
||
| 619 | * @since 0.1 |
||
| 620 | * |
||
| 621 | * @return Title |
||
| 622 | */ |
||
| 623 | public function getTitle() { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Gets the edit this set of changes belong to. |
||
| 633 | * |
||
| 634 | * @since 0.1 |
||
| 635 | * |
||
| 636 | * @return SWLEdit |
||
| 637 | */ |
||
| 638 | public function getEdit() { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Sets the edit this set of changes belong to. |
||
| 644 | * |
||
| 645 | * @since 0.1 |
||
| 646 | * |
||
| 647 | * @param SWLEdit $edit |
||
| 648 | */ |
||
| 649 | public function setEdit( SWLEdit $edit ) { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Returns if a certain insertion is present in the set of changes. |
||
| 655 | * |
||
| 656 | * @since 0.1 |
||
| 657 | * |
||
| 658 | * @param SMWDIProperty $property |
||
| 659 | * @param string $value |
||
| 660 | * |
||
| 661 | * @return boolean |
||
| 662 | */ |
||
| 663 | public function hasInsertion( SMWDIProperty $property, $value ) { |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Returns if a certain insertion is present in the set of changes. |
||
| 678 | * |
||
| 679 | * @since 0.1 |
||
| 680 | * |
||
| 681 | * @param SMWDIProperty $property |
||
| 682 | * @param string $value |
||
| 683 | * |
||
| 684 | * @return boolean |
||
| 685 | */ |
||
| 686 | public function hasDeletion( SMWDIProperty $property, $value ) { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Returns if a certain change is present in the set of changes. |
||
| 701 | * |
||
| 702 | * @since 0.1 |
||
| 703 | * |
||
| 704 | * @param SMWDIProperty $property |
||
| 705 | * @param SWLPropertyChange $change |
||
| 706 | * |
||
| 707 | * @return boolean |
||
| 708 | */ |
||
| 709 | public function hasChange( SMWDIProperty $property, SWLPropertyChange $change ) { |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Merges in the changes of another change set. |
||
| 724 | * Duplicate changes are detected and only kept as a single change. |
||
| 725 | * This is usefull for merging sets with (possibly overlapping) changes belonging to a single edit. |
||
| 726 | * |
||
| 727 | * @since 0.1 |
||
| 728 | * |
||
| 729 | * @param SWLChangeSet $set |
||
| 730 | */ |
||
| 731 | public function mergeInChangeSet( SWLChangeSet $set ) { |
||
| 752 | |||
| 753 | } |
||
| 754 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.