| Total Complexity | 47 |
| Total Lines | 569 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Alert 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 Alert, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | final class Alert extends Widget |
||
| 22 | { |
||
| 23 | private array $attributes = []; |
||
| 24 | private array $buttonAttributes = []; |
||
| 25 | private string $buttonLabel = '×'; |
||
| 26 | private string $body = ''; |
||
| 27 | private array $bodyAttributes = []; |
||
| 28 | /** @psalm-var non-empty-string */ |
||
| 29 | private string|null $bodyContainer = 'span'; |
||
| 30 | private bool $bodyContainerPanel = false; |
||
| 31 | private array $bodyContainerAttributes = []; |
||
| 32 | private string $header = ''; |
||
| 33 | private array $headerAttributes = []; |
||
| 34 | private bool $headerContainer = false; |
||
| 35 | private array $headerContainerAttributes = []; |
||
| 36 | /** @psalm-var non-empty-string */ |
||
| 37 | private string $headerTag = 'span'; |
||
| 38 | private array $iconAttributes = []; |
||
| 39 | private array $iconContainerAttributes = []; |
||
| 40 | private string $iconText = ''; |
||
| 41 | private string $layoutHeader = ''; |
||
| 42 | private string $layoutBody = '{body}{button}'; |
||
| 43 | private array $parts = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The HTML attributes for the main widget tag. |
||
| 47 | * |
||
| 48 | * @param array $value Array of attribute name => attribute value pairs. |
||
| 49 | * |
||
| 50 | * @return static |
||
| 51 | */ |
||
| 52 | public function attributes(array $value): self |
||
| 53 | { |
||
| 54 | $new = clone $this; |
||
| 55 | $new->attributes = $value; |
||
| 56 | return $new; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The message body. |
||
| 61 | * |
||
| 62 | * @param string $value The message body. |
||
| 63 | * |
||
| 64 | * @return static |
||
| 65 | */ |
||
| 66 | public function body(string $value): self |
||
| 67 | { |
||
| 68 | $new = clone $this; |
||
| 69 | $new->body = $value; |
||
| 70 | return $new; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * HTML attributes for the message body tag. |
||
| 75 | * |
||
| 76 | * @param array $value Array of attribute name => attribute value pairs. |
||
| 77 | * |
||
| 78 | * @return static |
||
| 79 | * |
||
| 80 | * {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
||
| 81 | */ |
||
| 82 | public function bodyAttributes(array $value): self |
||
| 83 | { |
||
| 84 | $new = clone $this; |
||
| 85 | $new->bodyAttributes = $value; |
||
| 86 | return $new; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * CSS class for the message body tag. |
||
| 91 | * |
||
| 92 | * @param string $value CSS class name. |
||
| 93 | * |
||
| 94 | * @return static |
||
| 95 | */ |
||
| 96 | public function bodyClass(string $value): self |
||
| 97 | { |
||
| 98 | $new = clone $this; |
||
| 99 | Html::addCssClass($new->bodyAttributes, $value); |
||
| 100 | return $new; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Allows you to add an extra wrapper for the message body. |
||
| 105 | * |
||
| 106 | * @param string|null $tag |
||
| 107 | * |
||
| 108 | * @return static |
||
| 109 | */ |
||
| 110 | public function bodyContainer(?string $tag = null): self |
||
| 111 | { |
||
| 112 | if ($tag === '') { |
||
| 113 | throw new InvalidArgumentException('Body tag must be a string and cannot be empty.'); |
||
| 114 | } |
||
| 115 | |||
| 116 | $new = clone $this; |
||
| 117 | $new->bodyContainer = $tag; |
||
| 118 | return $new; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The attributes for rendering extra message wrapper. |
||
| 123 | * |
||
| 124 | * @param array $value |
||
| 125 | * |
||
| 126 | * @return static |
||
| 127 | * |
||
| 128 | * {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
||
| 129 | */ |
||
| 130 | public function bodyContainerAttributes(array $value): self |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * The CSS class for extra message wrapper. |
||
| 139 | * |
||
| 140 | * @param string $value |
||
| 141 | * |
||
| 142 | * @return static |
||
| 143 | */ |
||
| 144 | public function bodyContainerClass(string $value): self |
||
| 145 | { |
||
| 146 | $new = clone $this; |
||
| 147 | Html::addCssClass($new->bodyContainerAttributes, $value); |
||
| 148 | return $new; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Allows you to add an extra wrapper for the panel body. |
||
| 153 | * |
||
| 154 | * @param bool $value |
||
| 155 | * |
||
| 156 | * @return static |
||
| 157 | */ |
||
| 158 | public function bodyContainerPanel(bool $value): self |
||
| 159 | { |
||
| 160 | $new = clone $this; |
||
| 161 | $new->bodyContainerPanel = $value; |
||
| 162 | return $new; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * The attributes for rendering the button tag. |
||
| 167 | * |
||
| 168 | * The button is displayed in the header of the modal window. Clicking on the button will hide the modal. |
||
| 169 | * |
||
| 170 | * If {@see buttonEnabled} is `false`, no button will be rendered. |
||
| 171 | * |
||
| 172 | * The rest of the options will be rendered as the HTML attributes of the button tag. |
||
| 173 | * |
||
| 174 | * @param array $value |
||
| 175 | * |
||
| 176 | * @return static |
||
| 177 | * |
||
| 178 | * {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
||
| 179 | */ |
||
| 180 | public function buttonAttributes(array $value): self |
||
| 181 | { |
||
| 182 | $new = clone $this; |
||
| 183 | $new->buttonAttributes = $value; |
||
| 184 | return $new; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * The CSS class for the button. |
||
| 189 | * |
||
| 190 | * @param string $value |
||
| 191 | * |
||
| 192 | * @return static |
||
| 193 | */ |
||
| 194 | public function buttonClass(string $value): self |
||
| 195 | { |
||
| 196 | $new = clone $this; |
||
| 197 | Html::addCssClass($new->buttonAttributes, $value); |
||
| 198 | return $new; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * The label for the button. |
||
| 203 | * |
||
| 204 | * @param string $value |
||
| 205 | * |
||
| 206 | * @return static |
||
| 207 | */ |
||
| 208 | public function buttonLabel(string $value = ''): self |
||
| 209 | { |
||
| 210 | $new = clone $this; |
||
| 211 | $new->buttonLabel = $value; |
||
| 212 | return $new; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * The onclick JavaScript for the button. |
||
| 217 | * |
||
| 218 | * @param string $value |
||
| 219 | * |
||
| 220 | * @return static |
||
| 221 | */ |
||
| 222 | public function buttonOnClick(string $value): self |
||
| 223 | { |
||
| 224 | $new = clone $this; |
||
| 225 | $new->buttonAttributes['onclick'] = $value; |
||
| 226 | return $new; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Set attribute class for main widget tag. |
||
| 231 | * |
||
| 232 | * @param string $value |
||
| 233 | * |
||
| 234 | * @return static |
||
| 235 | */ |
||
| 236 | public function class(string $value): self |
||
| 237 | { |
||
| 238 | $new = clone $this; |
||
| 239 | Html::addCssClass($new->attributes, $value); |
||
| 240 | return $new; |
||
| 241 | } |
||
| 242 | |||
| 243 | public function id(?string $value): self |
||
| 244 | { |
||
| 245 | $new = clone $this; |
||
| 246 | $new->attributes['id'] = $value; |
||
| 247 | return $new; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * The header content. |
||
| 252 | * |
||
| 253 | * @param string $value |
||
| 254 | * |
||
| 255 | * @return static |
||
| 256 | */ |
||
| 257 | public function header(string $value): self |
||
| 258 | { |
||
| 259 | $new = clone $this; |
||
| 260 | $new->header = $value; |
||
| 261 | return $new; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * The attributes for rendering the header content. |
||
| 266 | * |
||
| 267 | * @param array $value |
||
| 268 | * |
||
| 269 | * @return static |
||
| 270 | * |
||
| 271 | * {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
||
| 272 | */ |
||
| 273 | public function headerAttributes(array $value): self |
||
| 274 | { |
||
| 275 | $new = clone $this; |
||
| 276 | $new->headerAttributes = $value; |
||
| 277 | return $new; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * The CSS class for the header. |
||
| 282 | * |
||
| 283 | * @param string $value |
||
| 284 | * |
||
| 285 | * @return static |
||
| 286 | */ |
||
| 287 | public function headerClass(string $value): self |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Allows you to add a div tag to the header extra wrapper. |
||
| 296 | * |
||
| 297 | * @param bool $value |
||
| 298 | * |
||
| 299 | * @return static |
||
| 300 | */ |
||
| 301 | public function headerContainer(bool $value = true): self |
||
| 302 | { |
||
| 303 | $new = clone $this; |
||
| 304 | $new->headerContainer = $value; |
||
| 305 | return $new; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * The CSS class for the header extra wrapper. |
||
| 310 | * |
||
| 311 | * @param string $value |
||
| 312 | * |
||
| 313 | * @return static |
||
| 314 | */ |
||
| 315 | public function headerContainerClass(string $value): self |
||
| 316 | { |
||
| 317 | $new = clone $this; |
||
| 318 | Html::addCssClass($new->headerContainerAttributes, $value); |
||
| 319 | return $new; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * The attributes for rendering the header. |
||
| 324 | * |
||
| 325 | * @param array $value |
||
| 326 | * |
||
| 327 | * @return static |
||
| 328 | * |
||
| 329 | * {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
||
| 330 | */ |
||
| 331 | public function headerContainerAttributes(array $value): self |
||
| 332 | { |
||
| 333 | $new = clone $this; |
||
| 334 | $new->headerContainerAttributes = $value; |
||
| 335 | return $new; |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Set tag name for the header. |
||
| 340 | * |
||
| 341 | * @param string $value |
||
| 342 | * |
||
| 343 | * @throws InvalidArgumentException |
||
| 344 | * |
||
| 345 | * @return static |
||
| 346 | */ |
||
| 347 | public function headerTag(string $value): self |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * The attributes for rendering the `<i>` tag for the icon. |
||
| 360 | * |
||
| 361 | * @param array $value |
||
| 362 | * |
||
| 363 | * @return static |
||
| 364 | * |
||
| 365 | * {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
||
| 366 | */ |
||
| 367 | public function iconAttributes(array $value): self |
||
| 368 | { |
||
| 369 | $new = clone $this; |
||
| 370 | $new->iconAttributes = $value; |
||
| 371 | return $new; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Set icon CSS class. |
||
| 376 | * |
||
| 377 | * @param string $value |
||
| 378 | * |
||
| 379 | * @return static |
||
| 380 | */ |
||
| 381 | public function iconClass(string $value): self |
||
| 382 | { |
||
| 383 | $new = clone $this; |
||
| 384 | Html::addCssClass($new->iconAttributes, $value); |
||
| 385 | return $new; |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * The attributes for rendering icon container. |
||
| 390 | * |
||
| 391 | * The rest of the options will be rendered as the HTML attributes of the icon container. |
||
| 392 | * |
||
| 393 | * @param array $value |
||
| 394 | * |
||
| 395 | * @return static |
||
| 396 | * |
||
| 397 | * {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
||
| 398 | */ |
||
| 399 | public function iconContainerAttributes(array $value): self |
||
| 400 | { |
||
| 401 | $new = clone $this; |
||
| 402 | $new->iconContainerAttributes = $value; |
||
| 403 | return $new; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * The CSS class for the icon container. |
||
| 408 | * |
||
| 409 | * @param string $value |
||
| 410 | * |
||
| 411 | * @return static |
||
| 412 | */ |
||
| 413 | public function iconContainerClass(string $value): self |
||
| 414 | { |
||
| 415 | $new = clone $this; |
||
| 416 | Html::addCssClass($new->iconContainerAttributes, $value); |
||
| 417 | return $new; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Set icon text. |
||
| 422 | * |
||
| 423 | * @param string $value |
||
| 424 | * |
||
| 425 | * @return static |
||
| 426 | */ |
||
| 427 | public function iconText(string $value): self |
||
| 428 | { |
||
| 429 | $new = clone $this; |
||
| 430 | $new->iconText = $value; |
||
| 431 | return $new; |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Set layout body. |
||
| 436 | * |
||
| 437 | * @param string $value |
||
| 438 | * |
||
| 439 | * @return static |
||
| 440 | */ |
||
| 441 | public function layoutBody(string $value): self |
||
| 442 | { |
||
| 443 | $new = clone $this; |
||
| 444 | $new->layoutBody = $value; |
||
| 445 | return $new; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Set layout header. |
||
| 450 | * |
||
| 451 | * @param string $value |
||
| 452 | * |
||
| 453 | * @return static |
||
| 454 | */ |
||
| 455 | public function layoutHeader(string $value): self |
||
| 456 | { |
||
| 457 | $new = clone $this; |
||
| 458 | $new->layoutHeader = $value; |
||
| 459 | return $new; |
||
| 460 | } |
||
| 461 | |||
| 462 | protected function run(): string |
||
| 463 | { |
||
| 464 | $new = clone $this; |
||
| 465 | $div = Div::tag(); |
||
| 466 | |||
| 467 | if (!array_key_exists('id', $this->attributes)) { |
||
| 468 | $div = $div->id(Html::generateId('alert-')); |
||
| 469 | } |
||
| 470 | |||
| 471 | if (!isset($new->parts['{button}'])) { |
||
| 472 | $new->renderButton($new); |
||
| 473 | } |
||
| 474 | |||
| 475 | if (!isset($new->parts['{icon}'])) { |
||
| 476 | $new->renderIcon($new); |
||
| 477 | } |
||
| 478 | |||
| 479 | if (!isset($new->parts['{body}'])) { |
||
| 480 | $new->renderBody($new); |
||
| 481 | } |
||
| 482 | |||
| 483 | if (!isset($new->parts['{header}'])) { |
||
| 484 | $new->renderHeader($new); |
||
| 485 | } |
||
| 486 | |||
| 487 | $contentAlert = $new->renderHeaderContainer($new) . PHP_EOL . $new->renderPanelBody($new); |
||
| 488 | |||
| 489 | return $new->body !== '' |
||
| 490 | ? $div |
||
| 491 | ->attribute('role', 'alert') |
||
| 492 | ->attributes($new->attributes) |
||
| 493 | ->content(PHP_EOL . trim($contentAlert) . PHP_EOL) |
||
| 494 | ->encode(false) |
||
| 495 | ->render() |
||
| 496 | : ''; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Renders close button. |
||
| 501 | */ |
||
| 502 | private function renderButton(self $new): void |
||
| 503 | { |
||
| 504 | $new->parts['{button}'] = PHP_EOL . |
||
| 505 | Button::tag() |
||
| 506 | ->attributes($new->buttonAttributes) |
||
| 507 | ->content($new->buttonLabel) |
||
| 508 | ->encode(false) |
||
| 509 | ->type('button') |
||
| 510 | ->render(); |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Render icon. |
||
| 515 | */ |
||
| 516 | private function renderIcon(self $new): void |
||
| 517 | { |
||
| 518 | $icon = CustomTag::name('i')->attributes($new->iconAttributes)->content($new->iconText)->render(); |
||
| 519 | |||
| 520 | $new->parts['{icon}'] = PHP_EOL . |
||
| 521 | Div::tag() |
||
| 522 | ->attributes($new->iconContainerAttributes) |
||
| 523 | ->content($icon) |
||
| 524 | ->encode(false) |
||
| 525 | ->render() . PHP_EOL; |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Render the alert message body. |
||
| 530 | */ |
||
| 531 | private function renderBody(self $new): void |
||
| 532 | { |
||
| 533 | if ($new->bodyContainer !== null) { |
||
| 534 | $new->parts['{body}'] = CustomTag::name($new->bodyContainer) |
||
| 535 | ->attributes($new->bodyAttributes) |
||
| 536 | ->content($new->body) |
||
| 537 | ->encode(false) |
||
| 538 | ->render(); |
||
| 539 | } else { |
||
| 540 | $new->parts['{body}'] = $new->body; |
||
| 541 | } |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Render the header. |
||
| 546 | */ |
||
| 547 | private function renderHeader(self $new): void |
||
| 548 | { |
||
| 549 | $new->parts['{header}'] = CustomTag::name($new->headerTag) |
||
| 550 | ->attributes($new->headerAttributes) |
||
| 551 | ->content($new->header) |
||
| 552 | ->encode(false) |
||
| 553 | ->render(); |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Render the header container. |
||
| 558 | */ |
||
| 559 | private function renderHeaderContainer(self $new): string |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Render the panel body. |
||
| 576 | */ |
||
| 577 | private function renderPanelBody(self $new): string |
||
| 578 | { |
||
| 579 | $bodyHtml = trim(strtr($new->layoutBody, $new->parts)); |
||
| 580 | |||
| 581 | if ($new->bodyContainerPanel) { |
||
| 582 | $bodyHtml = Div::tag() |
||
| 583 | ->attributes($new->bodyContainerAttributes) |
||
| 584 | ->content(PHP_EOL . $bodyHtml . PHP_EOL) |
||
| 585 | ->encode(false) |
||
| 586 | ->render(); |
||
| 587 | } |
||
| 588 | |||
| 590 | } |
||
| 591 | } |
||
| 592 |