| Total Complexity | 57 |
| Total Lines | 438 |
| 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 | { |
||
| 93 | throw new \InvalidArgumentException("The value for booleans must be true or false, {$value} supplied!"); |
||
| 94 | } |
||
| 95 | if ($value && \is_string($value)) |
||
| 96 | { |
||
| 97 | $this->setValueTypeString($value); |
||
| 98 | } |
||
| 99 | $this->setXSIType($xsi_type); |
||
| 100 | $this->tags = array(); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $value_type |
||
| 105 | * |
||
| 106 | * @return Value |
||
| 107 | */ |
||
| 108 | public function setValueTypeString (string $value_type): self |
||
| 109 | { |
||
| 110 | $this->setValueType(new ValueType($value_type, 'value')); |
||
| 111 | return $this; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $code |
||
| 116 | * @param string $displayName |
||
| 117 | * |
||
| 118 | * @param bool $xsi_attribute |
||
| 119 | * |
||
| 120 | * @return Value |
||
| 121 | */ |
||
| 122 | public static function SNOMED (string $code, string $displayName, bool $xsi_attribute = TRUE): Value |
||
| 123 | { |
||
| 124 | $value = (new Value()) |
||
| 125 | ->setCode($code) |
||
| 126 | ->setDisplayName($displayName) |
||
| 127 | ->setCodeSystem('2.16.840.1.113883.6.96') |
||
| 128 | ->setCodeSystemName('SNOMED CT'); |
||
| 129 | if ($xsi_attribute) |
||
| 130 | { |
||
| 131 | $value->setXSIType(XSITypeInterface::CONCEPT_DESCRIPTOR); |
||
| 132 | } |
||
| 133 | return $value; |
||
| 134 | } |
||
| 135 | |||
| 136 | public static function NCTISValue (string $code, string $displayName, bool $xsi_attribute = TRUE): Value |
||
| 137 | { |
||
| 138 | $value = (new Value()) |
||
| 139 | ->setCode($code) |
||
| 140 | ->setDisplayName($displayName) |
||
| 141 | ->setCodeSystem('2.16.840.1.113883.6.96') |
||
| 142 | ->setCodeSystemName('SNOMED CT'); |
||
| 143 | if ($xsi_attribute) |
||
| 144 | { |
||
| 145 | $value->setXSIType(XSITypeInterface::CONCEPT_DESCRIPTOR); |
||
| 146 | } |
||
| 147 | return $value; |
||
| 148 | } |
||
| 149 | |||
| 150 | public static function HL7ResultStatus (string $code, string $displayName, bool $xsi_attribute = TRUE): Value |
||
| 151 | { |
||
| 152 | $value = (new Value()) |
||
| 153 | ->setCode($code) |
||
| 154 | ->setDisplayName($displayName) |
||
| 155 | ->setCodeSystem('2.16.840.1.113883.12.123') |
||
| 156 | ->setCodeSystemName('HL7 Result Status'); |
||
| 157 | if ($xsi_attribute) |
||
| 158 | { |
||
| 159 | $value->setXSIType(XSITypeInterface::CONCEPT_DESCRIPTOR); |
||
| 160 | } |
||
| 161 | return $value; |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * @return ValueType |
||
| 167 | */ |
||
| 168 | public function getCode (): ValueType |
||
| 169 | { |
||
| 170 | return $this->code; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $code |
||
| 175 | * |
||
| 176 | * @return Value |
||
| 177 | */ |
||
| 178 | public function setCode (string $code): self |
||
| 179 | { |
||
| 180 | $this->code = new ValueType ($code, 'code'); |
||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return ValueType |
||
| 186 | */ |
||
| 187 | public function getCodeSystem (): ValueType |
||
| 188 | { |
||
| 189 | return $this->code_system; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $code_system |
||
| 194 | * |
||
| 195 | * @return Value |
||
| 196 | */ |
||
| 197 | public function setCodeSystem (string $code_system): self |
||
| 198 | { |
||
| 199 | $this->code_system = new ValueType ($code_system, 'codeSystem'); |
||
| 200 | return $this; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return ValueType |
||
| 205 | */ |
||
| 206 | public function getCodeSystemName (): ValueType |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $code_system_name |
||
| 213 | * |
||
| 214 | * @return Value |
||
| 215 | */ |
||
| 216 | public function setCodeSystemName (string $code_system_name): self |
||
| 217 | { |
||
| 218 | $this->code_system_name = new ValueType ($code_system_name, 'codeSystemName'); |
||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return ValueType |
||
| 224 | */ |
||
| 225 | public function getDisplayName (): ValueType |
||
| 226 | { |
||
| 227 | return $this->display_name; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $display_name |
||
| 232 | * |
||
| 233 | * @return Value |
||
| 234 | */ |
||
| 235 | public function setDisplayName (string $display_name): self |
||
| 236 | { |
||
| 237 | $this->display_name = new ValueType ($display_name, 'displayName'); |
||
| 238 | return $this; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return ValueType |
||
| 243 | */ |
||
| 244 | public function getUnits (): ValueType |
||
| 245 | { |
||
| 246 | return $this->units; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $units |
||
| 251 | * |
||
| 252 | * @return Value |
||
| 253 | */ |
||
| 254 | public function setUnits (string $units): self |
||
| 255 | { |
||
| 256 | $this->units = new ValueType ($units, 'unit'); |
||
| 257 | return $this; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getElementTag (): string |
||
| 264 | { |
||
| 265 | return 'value'; |
||
| 266 | } |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * @param \DOMDocument $doc |
||
| 271 | * |
||
| 272 | * @return \DOMElement |
||
| 273 | */ |
||
| 274 | public function toDOMElement (\DOMDocument $doc): \DOMElement |
||
| 275 | { |
||
| 276 | $attributes = array(); |
||
| 277 | if ($this->hasValueType()) |
||
| 278 | { |
||
| 279 | $attributes[] = 'value_type'; |
||
| 280 | } |
||
| 281 | if ($this->hasCode()) |
||
| 282 | { |
||
| 283 | $attributes[] = 'code'; |
||
| 284 | } |
||
| 285 | if ($this->hasCodeSystem()) |
||
| 286 | { |
||
| 287 | $attributes[] = 'code_system'; |
||
| 288 | } |
||
| 289 | if ($this->hasCodeSystemName()) |
||
| 290 | { |
||
| 291 | $attributes[] = 'code_system_name'; |
||
| 292 | } |
||
| 293 | if ($this->hasDisplayName()) |
||
| 294 | { |
||
| 295 | $attributes[] = 'display_name'; |
||
| 296 | } |
||
| 297 | if ($this->hasUnits()) |
||
| 298 | { |
||
| 299 | $attributes[] = 'units'; |
||
| 300 | } |
||
| 301 | |||
| 302 | $el = $this->createElement($doc, $attributes); |
||
| 303 | if ($this->content) |
||
| 304 | { |
||
| 305 | $el->appendChild(new \DOMText($this->getContent())); |
||
| 306 | } |
||
| 307 | elseif ($this->hasReferenceElement()) |
||
| 308 | { |
||
| 309 | $el->appendChild($this->getReferenceElement()->toDOMElement($doc)); |
||
| 310 | } |
||
| 311 | |||
| 312 | if ($this->hasLow()) |
||
| 313 | { |
||
| 314 | $el->appendChild($this->getLow()->toDOMElement($doc)); |
||
| 315 | } |
||
| 316 | if ($this->hasHigh()) |
||
| 317 | { |
||
| 318 | $el->appendChild($this->getHigh()->toDOMElement($doc)); |
||
| 319 | } |
||
| 320 | |||
| 321 | if ($this->hasTags()) |
||
| 322 | { |
||
| 323 | foreach ($this->getTags() as $tag => $value) |
||
| 324 | { |
||
| 325 | $el->appendChild($doc->createElement(CDA::getNS() . $tag, $value)); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | return $el; |
||
| 330 | } |
||
| 331 | |||
| 332 | public function hasCode (): bool |
||
| 333 | { |
||
| 334 | return NULL !== $this->code; |
||
| 335 | } |
||
| 336 | |||
| 337 | public function hasCodeSystem (): bool |
||
| 338 | { |
||
| 339 | return NULL !== $this->code_system; |
||
| 340 | } |
||
| 341 | |||
| 342 | public function hasCodeSystemName (): bool |
||
| 343 | { |
||
| 344 | return NULL !== $this->code_system_name; |
||
| 345 | } |
||
| 346 | |||
| 347 | public function hasDisplayName (): bool |
||
| 348 | { |
||
| 349 | return NULL !== $this->display_name; |
||
| 350 | } |
||
| 351 | |||
| 352 | public function hasUnits (): bool |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getContent (): string |
||
| 361 | { |
||
| 362 | return $this->content; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $content |
||
| 367 | * |
||
| 368 | * @return Value |
||
| 369 | */ |
||
| 370 | public function setContent (string $content): self |
||
| 371 | { |
||
| 372 | $this->content = $content; |
||
| 373 | return $this; |
||
| 374 | } |
||
| 375 | |||
| 376 | public function hasReferenceElement (): bool |
||
| 377 | { |
||
| 378 | return NULL !== $this->reference_element; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return ReferenceElement |
||
| 383 | */ |
||
| 384 | public function getReferenceElement (): ReferenceElement |
||
| 385 | { |
||
| 386 | return $this->reference_element; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param ReferenceElement $reference_element |
||
| 391 | * |
||
| 392 | * @return Value |
||
| 393 | */ |
||
| 394 | public function setReferenceElement (ReferenceElement $reference_element): Value |
||
| 395 | { |
||
| 396 | $this->reference_element = $reference_element; |
||
| 397 | return $this; |
||
| 398 | } |
||
| 399 | |||
| 400 | public function hasLow (): bool |
||
| 401 | { |
||
| 402 | return NULL !== $this->low; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return Low |
||
| 407 | */ |
||
| 408 | public function getLow (): Low |
||
| 409 | { |
||
| 410 | return $this->low; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param Low $low |
||
| 415 | * |
||
| 416 | * @return self |
||
| 417 | */ |
||
| 418 | public function setLow (Low $low): self |
||
| 419 | { |
||
| 420 | $this->low = $low; |
||
| 421 | return $this; |
||
| 422 | } |
||
| 423 | |||
| 424 | public function hasHigh (): bool |
||
| 425 | { |
||
| 426 | return NULL !== $this->high; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return High |
||
| 431 | */ |
||
| 432 | public function getHigh (): High |
||
| 433 | { |
||
| 434 | return $this->high; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param High $high |
||
| 439 | * |
||
| 440 | * @return self |
||
| 441 | */ |
||
| 442 | public function setHigh (High $high): self |
||
| 443 | { |
||
| 444 | $this->high = $high; |
||
| 445 | return $this; |
||
| 446 | } |
||
| 447 | |||
| 448 | public function hasTags (): bool |
||
| 449 | { |
||
| 450 | return \count($this->tags) > 0; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return array |
||
| 455 | */ |
||
| 456 | public function getTags (): array |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param $tag |
||
| 463 | * @param $value |
||
| 464 | * |
||
| 465 | * @return Value |
||
| 466 | */ |
||
| 467 | public function addTagValue ($tag, $value): Value |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @inheritDoc |
||
| 475 | */ |
||
| 476 | public function getMediaType (): string |
||
| 477 | { |
||
| 478 | return $this->media_type; |
||
| 479 | } |
||
| 480 | |||
| 481 | public function setMediaType (string $media_type): self |
||
| 482 | { |
||
| 483 | $this->media_type = $media_type; |
||
| 484 | return $this; |
||
| 485 | } |
||
| 486 | } |