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