Complex classes like Model 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 Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Model { |
||
| 6 | private $members; |
||
| 7 | private $name; |
||
| 8 | private $namespace; |
||
| 9 | |||
| 10 | public function __construct($name, $namespace="models") { |
||
| 15 | |||
| 16 | public function addMember(Member $member) { |
||
| 20 | |||
| 21 | public function addManyToOne($member, $name, $className, $nullable=false) { |
||
| 28 | |||
| 29 | public function removeMember($memberName) { |
||
| 33 | |||
| 34 | public function addOneToMany($member, $mappedBy, $className) { |
||
| 40 | |||
| 41 | public function addManyToMany($member, $targetEntity, $inversedBy, $joinTable, $joinColumns=[], $inverseJoinColumns=[]) { |
||
| 47 | |||
| 48 | public function __toString() { |
||
| 67 | |||
| 68 | public function getName() { |
||
| 74 | |||
| 75 | public function getSimpleName() { |
||
| 78 | |||
| 79 | public function isAssociation() { |
||
| 88 | |||
| 89 | public function getPrimaryKey() { |
||
| 97 | |||
| 98 | public function getPkName(){ |
||
| 104 | |||
| 105 | public function getDefaultFk() { |
||
| 108 | |||
| 109 | public function getManyToOneMembers() { |
||
| 118 | |||
| 119 | private function getToStringField(){ |
||
| 128 | |||
| 129 | public function getToString(){ |
||
| 144 | } |
||
| 145 |