Total Complexity | 57 |
Total Lines | 420 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Value 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 Value, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class Value extends AbstractElement implements XSITypeInterface, MediaTypeInterface |
||
48 | { |
||
49 | use ValueTypeTrait; |
||
50 | use XSITypeTrait; |
||
51 | |||
52 | // attributes |
||
53 | |||
54 | /** @var ValueType */ |
||
55 | protected $code; |
||
56 | /** @var ValueType */ |
||
57 | protected $code_system; |
||
58 | /** @var ValueType */ |
||
59 | protected $code_system_name; |
||
60 | /** @var ValueType */ |
||
61 | protected $display_name; |
||
62 | /** @var ValueType */ |
||
63 | protected $units; |
||
64 | /** @var ReferenceElement */ |
||
65 | protected $reference_element; |
||
66 | /** @var string */ |
||
67 | protected $content; |
||
68 | /** @var string */ |
||
69 | protected $media_type; |
||
70 | // child elements |
||
71 | /** @var Low */ |
||
72 | protected $low; |
||
73 | /** @var High */ |
||
74 | protected $high; |
||
75 | /** @var array */ |
||
76 | protected $tags; |
||
77 | |||
78 | /** |
||
79 | * Value constructor. |
||
80 | * XSI types are required for the value tag |
||
81 | * |
||
82 | * @link http://www.cdapro.com/know/25047 |
||
83 | * |
||
84 | * @param string $xsi_type |
||
85 | * @param string $value |
||
86 | */ |
||
87 | public function __construct(string $value = '', string $xsi_type = '') |
||
88 | { |
||
89 | $this->media_type = ''; |
||
90 | if ($xsi_type === XSITypeInterface::BOOLEAN |
||
91 | && \in_array($value, array('true', 'false'), true) === false) { |
||
92 | throw new \InvalidArgumentException("The value for booleans must be true or false, {$value} supplied!"); |
||
93 | } |
||
94 | if ($value && \is_string($value)) { |
||
95 | $this->setValueTypeString($value); |
||
96 | } |
||
97 | $this->setXSIType($xsi_type); |
||
98 | $this->tags = array(); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param string $value_type |
||
103 | * |
||
104 | * @return Value |
||
105 | */ |
||
106 | public function setValueTypeString(string $value_type): self |
||
107 | { |
||
108 | $this->setValueType(new ValueType($value_type, 'value')); |
||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param string $code |
||
114 | * @param string $displayName |
||
115 | * |
||
116 | * @param bool $xsi_attribute |
||
117 | * |
||
118 | * @return Value |
||
119 | */ |
||
120 | public static function SNOMED(string $code, string $displayName, bool $xsi_attribute = true): Value |
||
121 | { |
||
122 | $value = (new Value()) |
||
123 | ->setCode($code) |
||
124 | ->setDisplayName($displayName) |
||
125 | ->setCodeSystem('2.16.840.1.113883.6.96') |
||
126 | ->setCodeSystemName('SNOMED CT'); |
||
127 | if ($xsi_attribute) { |
||
128 | $value->setXSIType(XSITypeInterface::CONCEPT_DESCRIPTOR); |
||
129 | } |
||
130 | return $value; |
||
131 | } |
||
132 | |||
133 | public static function NCTISValue(string $code, string $displayName, bool $xsi_attribute = true): Value |
||
134 | { |
||
135 | $value = (new Value()) |
||
136 | ->setCode($code) |
||
137 | ->setDisplayName($displayName) |
||
138 | ->setCodeSystem('2.16.840.1.113883.6.96') |
||
139 | ->setCodeSystemName('SNOMED CT'); |
||
140 | if ($xsi_attribute) { |
||
141 | $value->setXSIType(XSITypeInterface::CONCEPT_DESCRIPTOR); |
||
142 | } |
||
143 | return $value; |
||
144 | } |
||
145 | |||
146 | public static function HL7ResultStatus(string $code, string $displayName, bool $xsi_attribute = true): Value |
||
147 | { |
||
148 | $value = (new Value()) |
||
149 | ->setCode($code) |
||
150 | ->setDisplayName($displayName) |
||
151 | ->setCodeSystem('2.16.840.1.113883.12.123') |
||
152 | ->setCodeSystemName('HL7 Result Status'); |
||
153 | if ($xsi_attribute) { |
||
154 | $value->setXSIType(XSITypeInterface::CONCEPT_DESCRIPTOR); |
||
155 | } |
||
156 | return $value; |
||
157 | } |
||
158 | |||
159 | |||
160 | /** |
||
161 | * @return ValueType |
||
162 | */ |
||
163 | public function getCode(): ValueType |
||
164 | { |
||
165 | return $this->code; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param string $code |
||
170 | * |
||
171 | * @return Value |
||
172 | */ |
||
173 | public function setCode(string $code): self |
||
174 | { |
||
175 | $this->code = new ValueType ($code, 'code'); |
||
176 | return $this; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return ValueType |
||
181 | */ |
||
182 | public function getCodeSystem(): ValueType |
||
183 | { |
||
184 | return $this->code_system; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @param string $code_system |
||
189 | * |
||
190 | * @return Value |
||
191 | */ |
||
192 | public function setCodeSystem(string $code_system): self |
||
193 | { |
||
194 | $this->code_system = new ValueType ($code_system, 'codeSystem'); |
||
195 | return $this; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @return ValueType |
||
200 | */ |
||
201 | public function getCodeSystemName(): ValueType |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param string $code_system_name |
||
208 | * |
||
209 | * @return Value |
||
210 | */ |
||
211 | public function setCodeSystemName(string $code_system_name): self |
||
212 | { |
||
213 | $this->code_system_name = new ValueType ($code_system_name, 'codeSystemName'); |
||
214 | return $this; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @return ValueType |
||
219 | */ |
||
220 | public function getDisplayName(): ValueType |
||
221 | { |
||
222 | return $this->display_name; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param string $display_name |
||
227 | * |
||
228 | * @return Value |
||
229 | */ |
||
230 | public function setDisplayName(string $display_name): self |
||
231 | { |
||
232 | $this->display_name = new ValueType ($display_name, 'displayName'); |
||
233 | return $this; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @return ValueType |
||
238 | */ |
||
239 | public function getUnits(): ValueType |
||
240 | { |
||
241 | return $this->units; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param string $units |
||
246 | * |
||
247 | * @return Value |
||
248 | */ |
||
249 | public function setUnits(string $units): self |
||
250 | { |
||
251 | $this->units = new ValueType ($units, 'unit'); |
||
252 | return $this; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @return string |
||
257 | */ |
||
258 | public function getElementTag(): string |
||
259 | { |
||
260 | return 'value'; |
||
261 | } |
||
262 | |||
263 | |||
264 | /** |
||
265 | * @param \DOMDocument $doc |
||
266 | * |
||
267 | * @return \DOMElement |
||
268 | */ |
||
269 | public function toDOMElement(\DOMDocument $doc): \DOMElement |
||
270 | { |
||
271 | $attributes = array(); |
||
272 | if ($this->hasValueType()) { |
||
273 | $attributes[] = 'value_type'; |
||
274 | } |
||
275 | if ($this->hasCode()) { |
||
276 | $attributes[] = 'code'; |
||
277 | } |
||
278 | if ($this->hasCodeSystem()) { |
||
279 | $attributes[] = 'code_system'; |
||
280 | } |
||
281 | if ($this->hasCodeSystemName()) { |
||
282 | $attributes[] = 'code_system_name'; |
||
283 | } |
||
284 | if ($this->hasDisplayName()) { |
||
285 | $attributes[] = 'display_name'; |
||
286 | } |
||
287 | if ($this->hasUnits()) { |
||
288 | $attributes[] = 'units'; |
||
289 | } |
||
290 | |||
291 | $el = $this->createElement($doc, $attributes); |
||
292 | if ($this->content) { |
||
293 | $el->appendChild(new \DOMText($this->getContent())); |
||
294 | } elseif ($this->hasReferenceElement()) { |
||
295 | $el->appendChild($this->getReferenceElement()->toDOMElement($doc)); |
||
296 | } |
||
297 | |||
298 | if ($this->hasLow()) { |
||
299 | $el->appendChild($this->getLow()->toDOMElement($doc)); |
||
300 | } |
||
301 | if ($this->hasHigh()) { |
||
302 | $el->appendChild($this->getHigh()->toDOMElement($doc)); |
||
303 | } |
||
304 | |||
305 | if ($this->hasTags()) { |
||
306 | foreach ($this->getTags() as $tag => $value) { |
||
307 | $el->appendChild($doc->createElement(CDA::NS_CDA . $tag, $value)); |
||
308 | } |
||
309 | } |
||
310 | |||
311 | return $el; |
||
312 | } |
||
313 | |||
314 | public function hasCode(): bool |
||
315 | { |
||
316 | return null !== $this->code; |
||
317 | } |
||
318 | |||
319 | public function hasCodeSystem(): bool |
||
320 | { |
||
321 | return null !== $this->code_system; |
||
322 | } |
||
323 | |||
324 | public function hasCodeSystemName(): bool |
||
325 | { |
||
326 | return null !== $this->code_system_name; |
||
327 | } |
||
328 | |||
329 | public function hasDisplayName(): bool |
||
330 | { |
||
331 | return null !== $this->display_name; |
||
332 | } |
||
333 | |||
334 | public function hasUnits(): bool |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @return string |
||
341 | */ |
||
342 | public function getContent(): string |
||
343 | { |
||
344 | return $this->content; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @param string $content |
||
349 | * |
||
350 | * @return Value |
||
351 | */ |
||
352 | public function setContent(string $content): self |
||
353 | { |
||
354 | $this->content = $content; |
||
355 | return $this; |
||
356 | } |
||
357 | |||
358 | public function hasReferenceElement(): bool |
||
359 | { |
||
360 | return null !== $this->reference_element; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @return ReferenceElement |
||
365 | */ |
||
366 | public function getReferenceElement(): ReferenceElement |
||
367 | { |
||
368 | return $this->reference_element; |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @param ReferenceElement $reference_element |
||
373 | * |
||
374 | * @return Value |
||
375 | */ |
||
376 | public function setReferenceElement(ReferenceElement $reference_element): Value |
||
377 | { |
||
378 | $this->reference_element = $reference_element; |
||
379 | return $this; |
||
380 | } |
||
381 | |||
382 | public function hasLow(): bool |
||
383 | { |
||
384 | return null !== $this->low; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * @return Low |
||
389 | */ |
||
390 | public function getLow(): Low |
||
391 | { |
||
392 | return $this->low; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * @param Low $low |
||
397 | * |
||
398 | * @return self |
||
399 | */ |
||
400 | public function setLow(Low $low): self |
||
401 | { |
||
402 | $this->low = $low; |
||
403 | return $this; |
||
404 | } |
||
405 | |||
406 | public function hasHigh(): bool |
||
407 | { |
||
408 | return null !== $this->high; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * @return High |
||
413 | */ |
||
414 | public function getHigh(): High |
||
415 | { |
||
416 | return $this->high; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * @param High $high |
||
421 | * |
||
422 | * @return self |
||
423 | */ |
||
424 | public function setHigh(High $high): self |
||
425 | { |
||
426 | $this->high = $high; |
||
427 | return $this; |
||
428 | } |
||
429 | |||
430 | public function hasTags(): bool |
||
431 | { |
||
432 | return \count($this->tags) > 0; |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * @return array |
||
437 | */ |
||
438 | public function getTags(): array |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * @param $tag |
||
445 | * @param $value |
||
446 | * |
||
447 | * @return Value |
||
448 | */ |
||
449 | public function addTagValue($tag, $value): Value |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * @inheritDoc |
||
457 | */ |
||
458 | public function getMediaType(): string |
||
459 | { |
||
460 | return $this->media_type; |
||
461 | } |
||
462 | |||
463 | public function setMediaType(string $media_type): self |
||
464 | { |
||
465 | $this->media_type = $media_type; |
||
466 | return $this; |
||
467 | } |
||
468 | } |