Complex classes like ManyToManyParser 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 ManyToManyParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class ManyToManyParser { |
||
| 13 | private $member; |
||
| 14 | private $joinTable; |
||
| 15 | private $myFkField; |
||
| 16 | private $fkField; |
||
| 17 | private $targetEntity; |
||
| 18 | private $targetEntityClass; |
||
| 19 | private $targetEntityTable; |
||
| 20 | private $myPk; |
||
| 21 | private $inversedBy; |
||
| 22 | private $pk; |
||
| 23 | private $instance; |
||
| 24 | private $whereValues; |
||
| 25 | |||
| 26 | public function __construct($instance, $member) { |
||
| 31 | |||
| 32 | public function init($annot=false) { |
||
| 47 | |||
| 48 | private function _init($class,$annot){ |
||
| 78 | |||
| 79 | public function getMember() { |
||
| 82 | |||
| 83 | public function setMember($member) { |
||
| 87 | |||
| 88 | public function getJoinTable() { |
||
| 91 | |||
| 92 | public function setJoinTable($joinTable) { |
||
| 96 | |||
| 97 | public function getMyFkField() { |
||
| 100 | |||
| 101 | public function setMyFkField($myFkField) { |
||
| 105 | |||
| 106 | public function getFkField() { |
||
| 109 | |||
| 110 | public function setFkField($fkField) { |
||
| 114 | |||
| 115 | public function getTargetEntity() { |
||
| 118 | |||
| 119 | public function setTargetEntity($targetEntity) { |
||
| 123 | |||
| 124 | public function getTargetEntityClass() { |
||
| 127 | |||
| 128 | public function setTargetEntityClass($targetEntityClass) { |
||
| 132 | |||
| 133 | public function getTargetEntityTable() { |
||
| 136 | |||
| 137 | public function setTargetEntityTable($targetEntityTable) { |
||
| 141 | |||
| 142 | public function getMyPk() { |
||
| 145 | |||
| 146 | public function setMyPk($myPk) { |
||
| 150 | |||
| 151 | public function getPk() { |
||
| 154 | |||
| 155 | public function setPk($pk) { |
||
| 159 | |||
| 160 | public function getInversedBy() { |
||
| 163 | |||
| 164 | public function setInversedBy($inversedBy) { |
||
| 168 | |||
| 169 | public function getInstance() { |
||
| 172 | |||
| 173 | public function setInstance($instance) { |
||
| 177 | |||
| 178 | private function getSQL(){ |
||
| 181 | |||
| 182 | public function getJoinSQL($value=" ?"){ |
||
| 185 | |||
| 186 | private function getWhereMask($mask="'{value}'"){ |
||
| 189 | |||
| 190 | private function getParserWhereMask($mask="'{value}'"){ |
||
| 193 | |||
| 194 | private function generateWhereValues(){ |
||
| 203 | |||
| 204 | private function generateParserWhereValues(ConditionParser $cParser){ |
||
| 212 | |||
| 213 | public function generate(){ |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return \Ubiquity\orm\parser\ConditionParser |
||
| 222 | */ |
||
| 223 | public function generateConditionParser(){ |
||
| 231 | |||
| 232 | public function addValue($value){ |
||
| 235 | |||
| 236 | public function addValues($values){ |
||
| 241 | } |
||
| 242 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.