Total Complexity | 54 |
Total Lines | 365 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Addr 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 Addr, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class Addr extends AbstractElement |
||
48 | { |
||
49 | |||
50 | /** @var AddressCodeType $address_code_use_attribute */ |
||
51 | protected $address_code_use_attribute; |
||
52 | /** @var HouseNumber */ |
||
53 | private $house_number; |
||
54 | /** @var StreetName */ |
||
55 | private $street_name; |
||
56 | /** @var StreetNameType */ |
||
57 | private $street_name_type; |
||
58 | /** @var StreetAddressLine */ |
||
59 | private $street_address_line; |
||
60 | /** @var City */ |
||
61 | private $city; |
||
62 | /** @var PostalCode */ |
||
63 | private $postal_code; |
||
64 | /** @var State */ |
||
65 | private $state; |
||
66 | /** @var Country */ |
||
67 | private $country; |
||
68 | /** @var AdditionalLocator */ |
||
69 | private $additional_locator; |
||
70 | |||
71 | /** |
||
72 | * Addr constructor. |
||
73 | * |
||
74 | * @param null $street_address_line |
||
|
|||
75 | * @param null $city |
||
76 | * @param null $state |
||
77 | * @param null $postal_code |
||
78 | * @param null $additional_locator |
||
79 | */ |
||
80 | public function __construct( |
||
81 | $street_address_line = null, |
||
82 | $city = null, |
||
83 | $state = null, |
||
84 | $postal_code = null, |
||
85 | $additional_locator = null |
||
86 | ) { |
||
87 | if ($street_address_line) { |
||
88 | if ($street_address_line instanceof StreetAddressLine) { |
||
89 | $this->setStreetAddressLine($street_address_line); |
||
90 | } elseif (\is_string($street_address_line)) { |
||
91 | $this->setStreetAddressLine(new StreetAddressLine($street_address_line)); |
||
92 | } |
||
93 | } |
||
94 | if ($city) { |
||
95 | if ($city instanceof City) { |
||
96 | $this->setCity($city); |
||
97 | } elseif (\is_string($city)) { |
||
98 | $this->setCity(new City($city)); |
||
99 | } |
||
100 | } |
||
101 | if ($state) { |
||
102 | if ($state instanceof State) { |
||
103 | $this->setState($state); |
||
104 | } elseif (\is_string($state)) { |
||
105 | $this->setState(new State($state)); |
||
106 | } |
||
107 | } |
||
108 | if ($postal_code) { |
||
109 | if ($postal_code instanceof PostalCode) { |
||
110 | $this->setPostalCode($postal_code); |
||
111 | } elseif (\is_string($postal_code)) { |
||
112 | $this->setPostalCode(new PostalCode($postal_code)); |
||
113 | } |
||
114 | } |
||
115 | if ($additional_locator) { |
||
116 | if ($additional_locator instanceof AdditionalLocator) { |
||
117 | $this->setAdditionalLocator($additional_locator); |
||
118 | } elseif (\is_string($additional_locator)) { |
||
119 | $this->setAdditionalLocator(new AdditionalLocator($additional_locator)); |
||
120 | } |
||
121 | } |
||
122 | $this->address_code_use_attribute = new AddressCodeType(''); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @return AddressCodeType |
||
127 | */ |
||
128 | public function getUseAttribute(): AddressCodeType |
||
129 | { |
||
130 | return $this->address_code_use_attribute; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param $address_code_type |
||
135 | * |
||
136 | * @return Addr |
||
137 | */ |
||
138 | public function setUseAttribute($address_code_type): Addr |
||
139 | { |
||
140 | $this->address_code_use_attribute = $address_code_type instanceof AddressCodeType |
||
141 | ? $address_code_type |
||
142 | : new AddressCodeType($address_code_type); |
||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param \DOMDocument $doc |
||
148 | * |
||
149 | * @return \DOMElement |
||
150 | */ |
||
151 | public function toDOMElement(\DOMDocument $doc): \DOMElement |
||
152 | { |
||
153 | $el = $this->createElement($doc, ['address_code_use_attribute']); |
||
154 | // if the street address line is present use it, otherwise see if the individual components were set. |
||
155 | if ($this->hasStreetAddressLine()) { |
||
156 | $el->appendChild($this->getStreetAddressLine()->toDOMElement($doc)); |
||
157 | } else { |
||
158 | if (null !== $this->house_number) { |
||
159 | $el->appendChild($this->getHouseNumber()->toDOMElement($doc)); |
||
160 | } |
||
161 | if (null !== $this->street_name) { |
||
162 | $el->appendChild($this->getStreetName()->toDOMElement($doc)); |
||
163 | } |
||
164 | if (null !== $this->street_name_type) { |
||
165 | $el->appendChild($this->getStreetNameType()->toDOMElement($doc)); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | if ($this->hasCity()) { |
||
170 | $el->appendChild($this->getCity()->toDOMElement($doc)); |
||
171 | } |
||
172 | if ($this->hasState()) { |
||
173 | $el->appendChild($this->getState()->toDOMElement($doc)); |
||
174 | } |
||
175 | if ($this->hasPostalCode()) { |
||
176 | $el->appendChild($this->getPostalCode()->toDOMElement($doc)); |
||
177 | } |
||
178 | if ($this->hasAdditionalLocator()) { |
||
179 | $el->appendChild($this->getAdditionalLocator()->toDOMElement($doc)); |
||
180 | } |
||
181 | if ($this->hasCountry()) { |
||
182 | $el->appendChild($this->getCountry()->toDOMElement($doc)); |
||
183 | } |
||
184 | return $el; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function hasStreetAddressLine(): bool |
||
191 | { |
||
192 | return $this->street_address_line !== null; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return StreetAddressLine |
||
197 | */ |
||
198 | public function getStreetAddressLine(): StreetAddressLine |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param StreetAddressLine $street_address_line |
||
205 | * |
||
206 | * @return Addr |
||
207 | */ |
||
208 | public function setStreetAddressLine(StreetAddressLine $street_address_line): Addr |
||
209 | { |
||
210 | $this->street_address_line = $street_address_line; |
||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @return HouseNumber |
||
216 | */ |
||
217 | public function getHouseNumber(): HouseNumber |
||
218 | { |
||
219 | return $this->house_number; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param HouseNumber $house_number |
||
224 | * |
||
225 | * @return Addr |
||
226 | */ |
||
227 | public function setHouseNumber(HouseNumber $house_number): Addr |
||
228 | { |
||
229 | $this->house_number = $house_number; |
||
230 | return $this; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @return StreetName |
||
235 | */ |
||
236 | public function getStreetName(): StreetName |
||
237 | { |
||
238 | return $this->street_name; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @param StreetName $street_name |
||
243 | * |
||
244 | * @return Addr |
||
245 | */ |
||
246 | public function setStreetName(StreetName $street_name): Addr |
||
247 | { |
||
248 | $this->street_name = $street_name; |
||
249 | return $this; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @return StreetNameType |
||
254 | */ |
||
255 | public function getStreetNameType(): StreetNameType |
||
256 | { |
||
257 | return $this->street_name_type; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param StreetNameType $street_name_type |
||
262 | * |
||
263 | * @return Addr |
||
264 | */ |
||
265 | public function setStreetNameType(StreetNameType $street_name_type): Addr |
||
266 | { |
||
267 | $this->street_name_type = $street_name_type; |
||
268 | return $this; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @return bool |
||
273 | */ |
||
274 | public function hasCity(): bool |
||
275 | { |
||
276 | return null !== $this->city; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @return City |
||
281 | */ |
||
282 | public function getCity(): City |
||
283 | { |
||
284 | return $this->city; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * @param City $city |
||
289 | * |
||
290 | * @return Addr |
||
291 | */ |
||
292 | public function setCity(City $city): Addr |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function hasState(): bool |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @return State |
||
308 | */ |
||
309 | public function getState(): State |
||
310 | { |
||
311 | return $this->state; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param State $state |
||
316 | * |
||
317 | * @return Addr |
||
318 | */ |
||
319 | public function setState(State $state): Addr |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @return bool |
||
327 | */ |
||
328 | public function hasPostalCode(): bool |
||
329 | { |
||
330 | return null !== $this->postal_code; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * @return PostalCode |
||
335 | */ |
||
336 | public function getPostalCode(): PostalCode |
||
337 | { |
||
338 | return $this->postal_code; |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @param PostalCode $postal_code |
||
343 | * |
||
344 | * @return Addr |
||
345 | */ |
||
346 | public function setPostalCode(PostalCode $postal_code): Addr |
||
347 | { |
||
348 | $this->postal_code = $postal_code; |
||
349 | return $this; |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @return bool |
||
354 | */ |
||
355 | public function hasAdditionalLocator(): bool |
||
356 | { |
||
357 | return null !== $this->additional_locator; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @return AdditionalLocator |
||
362 | */ |
||
363 | public function getAdditionalLocator(): AdditionalLocator |
||
364 | { |
||
365 | return $this->additional_locator; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @param AdditionalLocator $additional_locator |
||
370 | * |
||
371 | * @return Addr |
||
372 | */ |
||
373 | public function setAdditionalLocator(AdditionalLocator $additional_locator): Addr |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @return bool |
||
381 | */ |
||
382 | public function hasCountry(): bool |
||
383 | { |
||
384 | return null !== $this->country; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * @return Country |
||
389 | */ |
||
390 | public function getCountry(): Country |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * @param Country $country |
||
397 | * |
||
398 | * @return Addr |
||
399 | */ |
||
400 | public function setCountry(Country $country): Addr |
||
401 | { |
||
402 | $this->country = $country; |
||
403 | return $this; |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * @return string |
||
408 | */ |
||
409 | protected function getElementTag(): string |
||
412 | } |
||
413 | } |
||
414 |