| Total Complexity | 50 |
| Total Lines | 244 |
| Duplicated Lines | 2.46 % |
| Changes | 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() |
||
| 56 | { |
||
| 57 | return $this->relationName; |
||
| 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) |
||
| 80 | { |
||
| 81 | $this->multiplicity = $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) |
||
| 96 | { |
||
| 97 | $this->keyField = $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 | assert(2 == $count); |
||
|
|
|||
| 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 | View Code Duplication | if (null !== $targType && (!is_string($targType) || empty($targType))) { |
|
| 151 | return false; |
||
| 152 | } |
||
| 153 | $foreignField = $this->foreignField; |
||
| 154 | View Code Duplication | 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) |
||
| 220 | { |
||
| 221 | $this->throughField = $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) |
||
| 249 | } |
||
| 250 | } |
||
| 251 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.