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