Conditions | 7 |
Paths | 19 |
Total Lines | 73 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types=1); |
||
14 | public function render(array $parameters, HTMLNode $previous): HTMLNode |
||
15 | { |
||
16 | $previous->addAttribute('class', 'btn'); |
||
17 | |||
18 | $color = $parameters[HTMLButton::COLOR] ?? 'btn-primary'; |
||
19 | $colorMap = [ |
||
20 | HTMLButton::COLOR_PRIMARY => 'btn-primary', |
||
21 | HTMLButton::COLOR_LINK => 'btn-link', |
||
22 | HTMLButton::COLOR_INFO => 'btn-info', |
||
23 | HTMLButton::COLOR_SUCCESS => 'btn-success', |
||
24 | HTMLButton::COLOR_WARNING => 'btn-warning', |
||
25 | HTMLButton::COLOR_ERROR => 'btn-error', |
||
26 | ]; |
||
27 | if (array_key_exists($color, $colorMap)) { |
||
28 | $color = $colorMap[$color]; |
||
29 | } else { |
||
30 | $colors = [ |
||
31 | 'btn-primary', |
||
32 | 'btn-secondary', |
||
33 | 'btn-success', |
||
34 | 'btn-danger', |
||
35 | 'btn-warning', |
||
36 | 'btn-info', |
||
37 | 'btn-light', |
||
38 | 'btn-dark', |
||
39 | 'btn-link', |
||
40 | |||
41 | 'btn-outline-primary', |
||
42 | 'btn-outline-secondary', |
||
43 | 'btn-outline-success', |
||
44 | 'btn-outline-danger', |
||
45 | 'btn-outline-warning', |
||
46 | 'btn-outline-info', |
||
47 | 'btn-outline-light', |
||
48 | 'btn-outline-dark', |
||
49 | ]; |
||
50 | if (!in_array($color, $colors)) { |
||
51 | throw new Exception('Invalid button color.'); |
||
52 | } |
||
53 | } |
||
54 | $previous->addAttribute('class', $color); |
||
55 | |||
56 | $size = $parameters[self::SIZE] ?? ''; |
||
57 | switch ($size) { |
||
58 | case self::SIZE_LARGE: |
||
59 | $previous->addAttribute('class', 'btn-lg'); |
||
60 | break; |
||
61 | case self::SIZE_SMALL: |
||
62 | $previous->addAttribute('class', 'btn-sm'); |
||
63 | break; |
||
64 | } |
||
65 | |||
66 | $icon = $parameters[self::ICON] ?? ''; |
||
67 | if ($icon) { |
||
68 | $iconData = []; |
||
69 | $iconPack = $parameters[self::ICON_PACK] ?? ''; |
||
70 | if ($iconPack) { |
||
71 | $iconData[] = $iconPack; |
||
72 | } |
||
73 | $iconData[] = $icon; |
||
74 | $iconElement = HTMLNode::factory( |
||
75 | 'i', |
||
76 | [ |
||
77 | 'class' => $iconData, |
||
78 | 'aria-hidden' => "true" |
||
79 | ], |
||
80 | [] |
||
81 | ); |
||
82 | $previous->addAttribute('class', 'has-icons-left'); |
||
83 | $previous->appendContent($iconElement); |
||
84 | } |
||
85 | |||
86 | return $previous; |
||
87 | } |
||
102 |