| Total Complexity | 49 |
| Total Lines | 242 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AbstractElement 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 AbstractElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 62 | abstract class AbstractElement implements ElementInterface, NullFlavourInterface |
||
| 63 | { |
||
| 64 | use NullFlavourTrait; |
||
| 65 | use RealmCodesTrait; |
||
| 66 | use TypeIdTrait; |
||
| 67 | use TemplateIdsTrait; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $attributes = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * this is a total hack to add non-commonly used attributes. |
||
| 76 | * don't use it lazy bones! |
||
| 77 | * |
||
| 78 | * @param $attribute |
||
| 79 | * @param $value |
||
| 80 | * |
||
| 81 | * @return AbstractElement |
||
| 82 | * @deprecated |
||
| 83 | */ |
||
| 84 | public function addAttribute($attribute, $value): self |
||
| 85 | { |
||
| 86 | $this->attributes[$attribute] = $value; |
||
| 87 | return $this; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return TargetSiteCode |
||
| 92 | */ |
||
| 93 | public function returnTargetSiteCode(): TargetSiteCode |
||
| 94 | { |
||
| 95 | if ($this instanceof TargetSiteCode) { |
||
| 96 | return $this; |
||
| 97 | } |
||
| 98 | throw new \RuntimeException('The method must be an instance of TargetSiteCode'); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return Observation |
||
| 103 | */ |
||
| 104 | public function returnObservation(): Observation |
||
| 105 | { |
||
| 106 | if ($this instanceof Observation) { |
||
| 107 | return $this; |
||
| 108 | } |
||
| 109 | throw new \RuntimeException('The method must be an instance of Observation'); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return SubstanceAdministration |
||
| 114 | */ |
||
| 115 | public function returnSubstanceAdministration(): SubstanceAdministration |
||
| 116 | { |
||
| 117 | if ($this instanceof SubstanceAdministration) { |
||
| 118 | return $this; |
||
| 119 | } |
||
| 120 | throw new \RuntimeException('The method must be an instance of SubstanceAdministration'); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return SpecimenRole |
||
| 125 | */ |
||
| 126 | public function returnSpecimenRole(): SpecimenRole |
||
| 127 | { |
||
| 128 | if ($this instanceof SpecimenRole) { |
||
| 129 | return $this; |
||
| 130 | } |
||
| 131 | throw new \RuntimeException('The method must be an instance of SpecimenRole'); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return ManufacturedProduct |
||
| 136 | */ |
||
| 137 | public function returnManufacturedProduct(): ManufacturedProduct |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return SpecimenPlayingEntity |
||
| 147 | */ |
||
| 148 | public function returnSpecimenPlayingEntity(): SpecimenPlayingEntity |
||
| 149 | { |
||
| 150 | if ($this instanceof SpecimenPlayingEntity) { |
||
| 151 | return $this; |
||
| 152 | } |
||
| 153 | throw new \RuntimeException('The method must be an instance of SpecimenPlayingEntity'); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return Consumable |
||
| 158 | */ |
||
| 159 | public function returnConsumable(): Consumable |
||
| 160 | { |
||
| 161 | if ($this instanceof Consumable) { |
||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | throw new \RuntimeException('The method must be an instance of Consumable'); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return Performer |
||
| 169 | */ |
||
| 170 | public function returnPerformer(): Performer |
||
| 171 | { |
||
| 172 | if ($this instanceof Performer) { |
||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | throw new \RuntimeException('The method must be an instance of Performer'); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return AssignedPerson |
||
| 180 | */ |
||
| 181 | public function returnAssignedPerson(): AssignedPerson |
||
| 182 | { |
||
| 183 | if ($this instanceof AssignedPerson) { |
||
| 184 | return $this; |
||
| 185 | } |
||
| 186 | throw new \RuntimeException('The method must be an instance of AssignedPerson'); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return ExtParticipantRole |
||
| 191 | */ |
||
| 192 | public function returnExtParticipantRole(): ExtParticipantRole |
||
| 193 | { |
||
| 194 | if ($this instanceof ExtParticipantRole) { |
||
| 195 | return $this; |
||
| 196 | } |
||
| 197 | throw new \RuntimeException('The method must be an instance of ExtParticipantRole'); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return Supply |
||
| 202 | */ |
||
| 203 | public function returnSupply(): Supply |
||
| 204 | { |
||
| 205 | if ($this instanceof Supply) { |
||
| 206 | return $this; |
||
| 207 | } |
||
| 208 | throw new \RuntimeException('The method must be an instance of Supply'); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param \DOMDocument $doc |
||
| 213 | * @param array $properties |
||
| 214 | * |
||
| 215 | * @return \DOMElement |
||
| 216 | */ |
||
| 217 | protected function createElement(\DOMDocument $doc, array $properties = array()): \DOMElement |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * get the element tag name |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | abstract protected function getElementTag(): string; |
||
| 304 | |||
| 306 |