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. 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 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 other end of relation |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $foreignField; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $relationName; |
||
30 | |||
31 | /** |
||
32 | * Target type this relation points to, if known. Is null for known-side polymorphic relations. |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $targType; |
||
36 | |||
37 | /** |
||
38 | * Base type this relation is attached to |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $baseType; |
||
42 | |||
43 | /** |
||
44 | * @return string |
||
45 | */ |
||
46 | public function getRelationName() |
||
50 | |||
51 | /** |
||
52 | * @param string $relationName |
||
53 | */ |
||
54 | public function setRelationName($relationName) |
||
58 | |||
59 | /** |
||
60 | * @return AssociationStubRelationType |
||
61 | */ |
||
62 | public function getMultiplicity() |
||
66 | |||
67 | /** |
||
68 | * @param AssociationStubRelationType $multiplicity |
||
69 | */ |
||
70 | public function setMultiplicity(AssociationStubRelationType $multiplicity) |
||
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | public function getKeyField() |
||
82 | |||
83 | /** |
||
84 | * @param string $keyField |
||
85 | */ |
||
86 | public function setKeyField($keyField) |
||
90 | |||
91 | public function isCompatible(AssociationStubBase $otherStub) |
||
117 | |||
118 | /** |
||
119 | * Is this AssociationStub sane? |
||
120 | */ |
||
121 | public function isOk() |
||
151 | |||
152 | /** |
||
153 | * @return string |
||
154 | */ |
||
155 | public function getTargType() |
||
159 | |||
160 | /** |
||
161 | * @param string $targType |
||
162 | */ |
||
163 | public function setTargType($targType) |
||
167 | |||
168 | /** |
||
169 | * @return string |
||
170 | */ |
||
171 | public function getBaseType() |
||
175 | |||
176 | /** |
||
177 | * @param string $baseType |
||
178 | */ |
||
179 | public function setBaseType($baseType) |
||
183 | |||
184 | /** |
||
185 | * @return string |
||
186 | */ |
||
187 | public function getForeignField() |
||
191 | |||
192 | /** |
||
193 | * @param string $foreignField |
||
194 | */ |
||
195 | public function setForeignField($foreignField) |
||
199 | |||
200 | /** |
||
201 | * Supply a canonical sort ordering to determine order in associations |
||
202 | * |
||
203 | * @param AssociationStubBase $other |
||
204 | * |
||
205 | * @return integer |
||
206 | */ |
||
207 | public function compare(AssociationStubBase $other) |
||
226 | } |
||
227 |