Total Complexity | 40 |
Total Lines | 253 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 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 |
||
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|null |
||
16 | */ |
||
17 | protected $keyField; |
||
18 | |||
19 | /** |
||
20 | * A list of fields to Traverse between Keyfield and foreignField. |
||
21 | * |
||
22 | * @var string[] |
||
23 | */ |
||
24 | protected $throughFieldChain; |
||
25 | |||
26 | /** |
||
27 | * Foreign key field of other end of relation. |
||
28 | * |
||
29 | * @var string|null |
||
30 | */ |
||
31 | protected $foreignField; |
||
32 | |||
33 | /** |
||
34 | * @var string|null |
||
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|null |
||
42 | */ |
||
43 | protected $targType; |
||
44 | |||
45 | /** |
||
46 | * Base type this relation is attached to. |
||
47 | * |
||
48 | * @var string|null |
||
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 = $this->checkStringInput($relationName) ? $relationName : $this->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() |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $keyField |
||
94 | */ |
||
95 | public function setKeyField($keyField) |
||
96 | { |
||
97 | $this->keyField = $this->checkStringInput($keyField) ? $keyField : $this->keyField; |
||
98 | } |
||
99 | |||
100 | public function isCompatible(AssociationStubBase $otherStub) |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Is this AssociationStub sane? |
||
120 | */ |
||
121 | public function isOk() |
||
122 | { |
||
123 | if (null === $this->multiplicity) { |
||
124 | return false; |
||
125 | } |
||
126 | if (null === $this->relationName) { |
||
127 | return false; |
||
128 | } |
||
129 | if (null === $this->keyField) { |
||
130 | return false; |
||
131 | } |
||
132 | if (null === $this->baseType) { |
||
133 | return false; |
||
134 | } |
||
135 | $targType = $this->targType; |
||
136 | if ($this instanceof AssociationStubMonomorphic && null === $targType) { |
||
137 | return false; |
||
138 | } |
||
139 | $foreignField = $this->foreignField; |
||
140 | if (null !== $targType) { |
||
141 | if (!$this->checkStringInput($targType)) { |
||
142 | return false; |
||
143 | } |
||
144 | if (!$this->checkStringInput($foreignField)) { |
||
145 | return false; |
||
146 | } |
||
147 | } |
||
148 | return (null === $targType) === (null === $foreignField); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | public function getTargType() |
||
155 | { |
||
156 | return $this->targType; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param string $targType |
||
161 | */ |
||
162 | public function setTargType($targType) |
||
163 | { |
||
164 | $this->targType = $targType; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getBaseType() |
||
171 | { |
||
172 | return $this->baseType; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param string $baseType |
||
177 | */ |
||
178 | public function setBaseType($baseType) |
||
179 | { |
||
180 | $this->baseType = $this->checkStringInput($baseType) ? $baseType : $this->baseType; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getForeignField() |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param string $foreignField |
||
193 | */ |
||
194 | public function setForeignField($foreignField) |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return string[]|null |
||
201 | */ |
||
202 | public function getThroughFieldChain(): array |
||
203 | { |
||
204 | return $this->throughFieldChain; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param string[]|null $keyChain |
||
209 | */ |
||
210 | public function setThroughFieldChain(?array $keyChain) |
||
211 | { |
||
212 | $this->throughFieldChain = $keyChain; |
||
213 | } |
||
214 | /** |
||
215 | * Supply a canonical sort ordering to determine order in associations. |
||
216 | * |
||
217 | * @param AssociationStubBase $other |
||
218 | * |
||
219 | * @return int |
||
220 | */ |
||
221 | public function compare(AssociationStubBase $other) |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Return what type of stub this is - polymorphic, monomorphic, or something else. |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | abstract public function morphicType(); |
||
247 | |||
248 | /** |
||
249 | * @param $input |
||
250 | * @return bool |
||
251 | */ |
||
252 | private function checkStringInput($input) |
||
260 |