| Total Complexity | 51 |
| Total Lines | 319 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AbstractHtmlElement 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 AbstractHtmlElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | abstract class AbstractHtmlElement extends AbstractElement |
||
| 42 | { |
||
| 43 | /** @var string */ |
||
| 44 | protected $content; |
||
| 45 | |||
| 46 | /** @var array */ |
||
| 47 | protected $tags; |
||
| 48 | |||
| 49 | protected $tag_attributes; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * AbstractHtmlElement constructor. |
||
| 53 | * |
||
| 54 | * @param string $choice |
||
| 55 | */ |
||
| 56 | public function __construct($choice = '') |
||
| 57 | { |
||
| 58 | $this->tags = array(); |
||
| 59 | $this->tag_attributes = array(); |
||
| 60 | if ($choice) { |
||
| 61 | if (\is_string($choice)) { |
||
|
|
|||
| 62 | $this->setContent($choice); |
||
| 63 | } else { |
||
| 64 | $this->addTag($choice); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param $choice |
||
| 71 | * |
||
| 72 | * @return self |
||
| 73 | */ |
||
| 74 | public function addTag($choice): self |
||
| 75 | { |
||
| 76 | if ($this->canAddTag($choice)) { |
||
| 77 | $this->tags[] = $choice; |
||
| 78 | } |
||
| 79 | return $this; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Checks to see if the choice can be added to the tags array of this class. |
||
| 84 | * |
||
| 85 | * @param $choice |
||
| 86 | * |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | abstract protected function canAddTag($choice): bool; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @inheritDoc |
||
| 93 | */ |
||
| 94 | public function toDOMElement(\DOMDocument $doc): \DOMElement |
||
| 95 | { |
||
| 96 | $el = $this->createElement($doc, $this->tag_attributes); |
||
| 97 | if ($this->hasContent()) { |
||
| 98 | $el->appendChild($doc->createTextNode($this->getContent())); |
||
| 99 | } else { |
||
| 100 | foreach ($this->getTags() as $tag) { |
||
| 101 | $el->appendChild($tag->toDOMElement($doc)); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | return $el; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public function hasContent(): bool |
||
| 111 | { |
||
| 112 | return empty($this->content) === false; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getContent(): string |
||
| 119 | { |
||
| 120 | return $this->content; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $content |
||
| 125 | * |
||
| 126 | * @return self |
||
| 127 | */ |
||
| 128 | public function setContent(string $content): self |
||
| 129 | { |
||
| 130 | $this->content = $content; |
||
| 131 | return $this; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function getTags(): array |
||
| 138 | { |
||
| 139 | return $this->tags; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param array $tags |
||
| 144 | * |
||
| 145 | * @return self |
||
| 146 | */ |
||
| 147 | public function setTags(array $tags): self |
||
| 148 | { |
||
| 149 | $this->tags = $tags; |
||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return Br |
||
| 155 | */ |
||
| 156 | public function returnBr(): Br |
||
| 157 | { |
||
| 158 | if ($this instanceof Br) { |
||
| 159 | return $this; |
||
| 160 | } |
||
| 161 | throw new \RuntimeException('The element is not an instance of Class Br'); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return Caption |
||
| 166 | */ |
||
| 167 | public function returnCaption(): Caption |
||
| 168 | { |
||
| 169 | if ($this instanceof Caption) { |
||
| 170 | return $this; |
||
| 171 | } |
||
| 172 | throw new \RuntimeException('The element is not an instance of Class Caption'); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return FootNote |
||
| 177 | */ |
||
| 178 | public function returnFootNote(): FootNote |
||
| 179 | { |
||
| 180 | if ($this instanceof FootNote) { |
||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | throw new \RuntimeException('The element is not an instance of Class FootNote'); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return FootNoteRef |
||
| 188 | */ |
||
| 189 | public function returnFootNoteRef(): FootNoteRef |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return Item |
||
| 199 | */ |
||
| 200 | public function returnItem(): Item |
||
| 201 | { |
||
| 202 | if ($this instanceof Item) { |
||
| 203 | return $this; |
||
| 204 | } |
||
| 205 | throw new \RuntimeException('The element is not an instance of Class Item'); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return LinkHtml |
||
| 210 | */ |
||
| 211 | public function returnLinkHtml(): LinkHtml |
||
| 212 | { |
||
| 213 | if ($this instanceof LinkHtml) { |
||
| 214 | return $this; |
||
| 215 | } |
||
| 216 | throw new \RuntimeException('The element is not an instance of Class LinkHtml'); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return ListElement |
||
| 221 | */ |
||
| 222 | public function returnListElement(): ListElement |
||
| 223 | { |
||
| 224 | if ($this instanceof ListElement) { |
||
| 225 | return $this; |
||
| 226 | } |
||
| 227 | throw new \RuntimeException('The element is not an instance of Class ListElement'); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return Paragraph |
||
| 232 | */ |
||
| 233 | public function returnParagraph(): Paragraph |
||
| 234 | { |
||
| 235 | if ($this instanceof Paragraph) { |
||
| 236 | return $this; |
||
| 237 | } |
||
| 238 | throw new \RuntimeException('The element is not an instance of Class Paragraph'); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return ReferenceElement |
||
| 243 | */ |
||
| 244 | public function returnReferenceElement(): ReferenceElement |
||
| 245 | { |
||
| 246 | if ($this instanceof ReferenceElement) { |
||
| 247 | return $this; |
||
| 248 | } |
||
| 249 | throw new \RuntimeException('The element is not an instance of Class ReferenceElement'); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return RenderMultiMedia |
||
| 254 | */ |
||
| 255 | public function returnRenderMultiMedia(): RenderMultiMedia |
||
| 256 | { |
||
| 257 | if ($this instanceof RenderMultiMedia) { |
||
| 258 | return $this; |
||
| 259 | } |
||
| 260 | throw new \RuntimeException('The element is not an instance of Class RenderMultiMedia'); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return Sub |
||
| 265 | */ |
||
| 266 | public function returnSub(): Sub |
||
| 267 | { |
||
| 268 | if ($this instanceof Sub) { |
||
| 269 | return $this; |
||
| 270 | } |
||
| 271 | throw new \RuntimeException('The element is not an instance of Class Sub'); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return Sup |
||
| 276 | */ |
||
| 277 | public function returnSup(): Sup |
||
| 278 | { |
||
| 279 | if ($this instanceof Sup) { |
||
| 280 | return $this; |
||
| 281 | } |
||
| 282 | throw new \RuntimeException('The element is not an instance of Class Sup'); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return Table |
||
| 287 | */ |
||
| 288 | public function returnTable(): Table |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return TableBody |
||
| 298 | */ |
||
| 299 | public function returnTableBody(): TableBody |
||
| 300 | { |
||
| 301 | if ($this instanceof TableBody) { |
||
| 302 | return $this; |
||
| 303 | } |
||
| 304 | throw new \RuntimeException('The element is not an instance of Class TableBody'); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return TableCell |
||
| 309 | */ |
||
| 310 | public function returnTableCell(): TableCell |
||
| 311 | { |
||
| 312 | if ($this instanceof TableCell) { |
||
| 313 | return $this; |
||
| 314 | } |
||
| 315 | throw new \RuntimeException('The element is not an instance of Class TableCell'); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return TableHead |
||
| 320 | */ |
||
| 321 | public function returnTableHead(): TableHead |
||
| 322 | { |
||
| 323 | if ($this instanceof TableHead) { |
||
| 324 | return $this; |
||
| 325 | } |
||
| 326 | throw new \RuntimeException('The element is not an instance of Class TableHead'); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return TableRow |
||
| 331 | */ |
||
| 332 | public function returnTableRow(): TableRow |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return Text |
||
| 342 | */ |
||
| 343 | public function returnText(): Text |
||
| 344 | { |
||
| 345 | if ($this instanceof Text) { |
||
| 346 | return $this; |
||
| 347 | } |
||
| 348 | throw new \RuntimeException('The element is not an instance of Class Text'); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return Title |
||
| 353 | */ |
||
| 354 | public function returnTitle(): Title |
||
| 355 | { |
||
| 356 | if ($this instanceof Title) { |
||
| 360 | } |
||
| 361 | } |