| Total Complexity | 46 | 
| Total Lines | 242 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like AssociationStubBase 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.
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 AssociationStubBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 7 | abstract class AssociationStubBase  | 
            ||
| 8 | { | 
            ||
| 9 | /**  | 
            ||
| 10 | * @var AssociationStubRelationType  | 
            ||
| 11 | */  | 
            ||
| 12 | protected $multiplicity;  | 
            ||
| 13 | |||
| 14 | /**  | 
            ||
| 15 | * Foreign key field of this end of relation.  | 
            ||
| 16 | *  | 
            ||
| 17 | * @var string  | 
            ||
| 18 | */  | 
            ||
| 19 | protected $keyField;  | 
            ||
| 20 | |||
| 21 | /**  | 
            ||
| 22 | * Foreign key field of imtermate relation.  | 
            ||
| 23 | *  | 
            ||
| 24 | * @var string  | 
            ||
| 25 | */  | 
            ||
| 26 | protected $throughField;  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * Foreign key field of other end of relation.  | 
            ||
| 30 | *  | 
            ||
| 31 | * @var string  | 
            ||
| 32 | */  | 
            ||
| 33 | protected $foreignField;  | 
            ||
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * @var string  | 
            ||
| 37 | */  | 
            ||
| 38 | protected $relationName;  | 
            ||
| 39 | |||
| 40 | /**  | 
            ||
| 41 | * Target type this relation points to, if known. Is null for known-side polymorphic relations.  | 
            ||
| 42 | *  | 
            ||
| 43 | * @var string|null  | 
            ||
| 44 | */  | 
            ||
| 45 | protected $targType;  | 
            ||
| 46 | |||
| 47 | /**  | 
            ||
| 48 | * Base type this relation is attached to.  | 
            ||
| 49 | *  | 
            ||
| 50 | * @var string  | 
            ||
| 51 | */  | 
            ||
| 52 | protected $baseType;  | 
            ||
| 53 | |||
| 54 | /**  | 
            ||
| 55 | * @return string  | 
            ||
| 56 | */  | 
            ||
| 57 | public function getRelationName()  | 
            ||
| 58 |     { | 
            ||
| 59 | return $this->relationName;  | 
            ||
| 60 | }  | 
            ||
| 61 | |||
| 62 | /**  | 
            ||
| 63 | * @param string $relationName  | 
            ||
| 64 | */  | 
            ||
| 65 | public function setRelationName($relationName)  | 
            ||
| 66 |     { | 
            ||
| 67 | $this->relationName = $relationName;  | 
            ||
| 68 | }  | 
            ||
| 69 | |||
| 70 | /**  | 
            ||
| 71 | * @return AssociationStubRelationType  | 
            ||
| 72 | */  | 
            ||
| 73 | public function getMultiplicity()  | 
            ||
| 74 |     { | 
            ||
| 75 | return $this->multiplicity;  | 
            ||
| 76 | }  | 
            ||
| 77 | |||
| 78 | /**  | 
            ||
| 79 | * @param AssociationStubRelationType $multiplicity  | 
            ||
| 80 | */  | 
            ||
| 81 | public function setMultiplicity(AssociationStubRelationType $multiplicity)  | 
            ||
| 82 |     { | 
            ||
| 83 | $this->multiplicity = $multiplicity;  | 
            ||
| 84 | }  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * @return string  | 
            ||
| 88 | */  | 
            ||
| 89 | public function getKeyField()  | 
            ||
| 90 |     { | 
            ||
| 91 | return $this->keyField;  | 
            ||
| 92 | }  | 
            ||
| 93 | |||
| 94 | /**  | 
            ||
| 95 | * @param string $keyField  | 
            ||
| 96 | */  | 
            ||
| 97 | public function setKeyField($keyField)  | 
            ||
| 98 |     { | 
            ||
| 99 | $this->keyField = $keyField;  | 
            ||
| 100 | }  | 
            ||
| 101 | |||
| 102 | public function isCompatible(AssociationStubBase $otherStub)  | 
            ||
| 103 |     { | 
            ||
| 104 | $thisPoly = $this instanceof AssociationStubPolymorphic;  | 
            ||
| 105 | $thatPoly = $otherStub instanceof AssociationStubPolymorphic;  | 
            ||
| 106 | $thisMono = $this instanceof AssociationStubMonomorphic;  | 
            ||
| 107 | $thatMono = $otherStub instanceof AssociationStubMonomorphic;  | 
            ||
| 108 | |||
| 109 |         if ($thisPoly && $thatMono) { | 
            ||
| 110 | return false;  | 
            ||
| 111 | }  | 
            ||
| 112 |         if ($thisMono && $thatPoly) { | 
            ||
| 113 | return false;  | 
            ||
| 114 | }  | 
            ||
| 115 |         if (!$this->isOk()) { | 
            ||
| 116 | return false;  | 
            ||
| 117 | }  | 
            ||
| 118 |         if (!$otherStub->isOk()) { | 
            ||
| 119 | return false;  | 
            ||
| 120 | }  | 
            ||
| 121 | $thisMult = $this->getMultiplicity();  | 
            ||
| 122 | $thatMult = $otherStub->getMultiplicity();  | 
            ||
| 123 | return (AssociationStubRelationType::MANY() == $thisMult || $thisMult != $thatMult);  | 
            ||
| 124 | }  | 
            ||
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * Is this AssociationStub sane?  | 
            ||
| 128 | */  | 
            ||
| 129 | public function isOk()  | 
            ||
| 130 |     { | 
            ||
| 131 |         if (null === $this->multiplicity) { | 
            ||
| 132 | return false;  | 
            ||
| 133 | }  | 
            ||
| 134 | $relName = $this->relationName;  | 
            ||
| 135 |         if (null === $relName || !is_string($relName) || empty($relName)) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 136 | return false;  | 
            ||
| 137 | }  | 
            ||
| 138 | $keyField = $this->keyField;  | 
            ||
| 139 |         if (null === $keyField || !is_string($keyField) || empty($keyField)) { | 
            ||
| 140 | return false;  | 
            ||
| 141 | }  | 
            ||
| 142 | $baseType = $this->baseType;  | 
            ||
| 143 |         if (null === $baseType || !is_string($baseType) || empty($baseType)) { | 
            ||
| 144 | return false;  | 
            ||
| 145 | }  | 
            ||
| 146 | $targType = $this->targType;  | 
            ||
| 147 |         if ($this instanceof AssociationStubMonomorphic && null === $targType) { | 
            ||
| 148 | return false;  | 
            ||
| 149 | }  | 
            ||
| 150 |         if (null !== $targType && (!is_string($targType) || empty($targType))) { | 
            ||
| 151 | return false;  | 
            ||
| 152 | }  | 
            ||
| 153 | $foreignField = $this->foreignField;  | 
            ||
| 154 |         if (null !== $targType && (null === $foreignField || !is_string($foreignField) || empty($foreignField))) { | 
            ||
| 155 | return false;  | 
            ||
| 156 | }  | 
            ||
| 157 | return (null === $targType) === (null === $foreignField);  | 
            ||
| 158 | }  | 
            ||
| 159 | |||
| 160 | /**  | 
            ||
| 161 | * @return string  | 
            ||
| 162 | */  | 
            ||
| 163 | public function getTargType()  | 
            ||
| 164 |     { | 
            ||
| 165 | return $this->targType;  | 
            ||
| 166 | }  | 
            ||
| 167 | |||
| 168 | /**  | 
            ||
| 169 | * @param string $targType  | 
            ||
| 170 | */  | 
            ||
| 171 | public function setTargType($targType)  | 
            ||
| 172 |     { | 
            ||
| 173 | $this->targType = $targType;  | 
            ||
| 174 | }  | 
            ||
| 175 | |||
| 176 | /**  | 
            ||
| 177 | * @return string  | 
            ||
| 178 | */  | 
            ||
| 179 | public function getBaseType()  | 
            ||
| 180 |     { | 
            ||
| 181 | return $this->baseType;  | 
            ||
| 182 | }  | 
            ||
| 183 | |||
| 184 | /**  | 
            ||
| 185 | * @param string $baseType  | 
            ||
| 186 | */  | 
            ||
| 187 | public function setBaseType($baseType)  | 
            ||
| 188 |     { | 
            ||
| 189 | $this->baseType = $baseType;  | 
            ||
| 190 | }  | 
            ||
| 191 | |||
| 192 | /**  | 
            ||
| 193 | * @return string  | 
            ||
| 194 | */  | 
            ||
| 195 | public function getForeignField()  | 
            ||
| 198 | }  | 
            ||
| 199 | |||
| 200 | /**  | 
            ||
| 201 | * @param string $foreignField  | 
            ||
| 202 | */  | 
            ||
| 203 | public function setForeignField($foreignField)  | 
            ||
| 204 |     { | 
            ||
| 205 | $this->foreignField = $foreignField;  | 
            ||
| 206 | }  | 
            ||
| 207 | |||
| 208 | /**  | 
            ||
| 209 | * @return string  | 
            ||
| 210 | */  | 
            ||
| 211 | public function getThroughField()  | 
            ||
| 212 |     { | 
            ||
| 213 | return $this->throughField;  | 
            ||
| 214 | }  | 
            ||
| 215 | |||
| 216 | /**  | 
            ||
| 217 | * @param string $foreignField  | 
            ||
| 218 | */  | 
            ||
| 219 | public function setThroughField($foreignField)  | 
            ||
| 222 | }  | 
            ||
| 223 | |||
| 224 | /**  | 
            ||
| 225 | * Supply a canonical sort ordering to determine order in associations.  | 
            ||
| 226 | *  | 
            ||
| 227 | * @param AssociationStubBase $other  | 
            ||
| 228 | *  | 
            ||
| 229 | * @return int  | 
            ||
| 230 | */  | 
            ||
| 231 | public function compare(AssociationStubBase $other)  | 
            ||
| 232 |     { | 
            ||
| 233 | $thisClass = get_class($this);  | 
            ||
| 249 | }  | 
            ||
| 250 | }  | 
            ||
| 251 |