Total Complexity | 47 |
Total Lines | 223 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Html 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 Html, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Html{ |
||
28 | |||
29 | /** |
||
30 | * Generate the html anchor link |
||
31 | * @param string $link the href attribute value |
||
32 | * @param string $anchor the displayed anchor |
||
33 | * @param array $attributes the additional attributes to be added |
||
34 | * @param boolean $return whether need return the generated html or just display it directly |
||
35 | * |
||
36 | * @return string|void the anchor link generated html if $return is true or display it if not |
||
37 | */ |
||
38 | public static function a($link = '', $anchor = null, array $attributes = array(), $return = true){ |
||
39 | if(! is_url($link)){ |
||
40 | $link = Url::site_url($link); |
||
41 | } |
||
42 | if(! $anchor){ |
||
43 | $anchor = $link; |
||
44 | } |
||
45 | $str = null; |
||
46 | $str .= '<a href = "'.$link.'"'; |
||
47 | $str .= attributes_to_string($attributes); |
||
48 | $str .= '>'; |
||
49 | $str .= $anchor; |
||
50 | $str .= '</a>'; |
||
51 | |||
52 | if($return){ |
||
53 | return $str; |
||
54 | } |
||
55 | echo $str; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Generate an mailto anchor link |
||
60 | * @param string $link the email address |
||
61 | * @param string $anchor the displayed value of the link |
||
62 | * @param array $attributes the additional attributes to be added |
||
63 | * @param boolean $return whether need return the generated html or just display it directly |
||
64 | * |
||
65 | * @return string|void the generated html for mailto link if $return is true or display it if not |
||
66 | */ |
||
67 | public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){ |
||
68 | if(! $anchor){ |
||
69 | $anchor = $link; |
||
70 | } |
||
71 | $str = null; |
||
72 | $str .= '<a href = "mailto:'.$link.'"'; |
||
73 | $str .= attributes_to_string($attributes); |
||
74 | $str .= '>'; |
||
75 | $str .= $anchor; |
||
76 | $str .= '</a>'; |
||
77 | |||
78 | if($return){ |
||
79 | return $str; |
||
80 | } |
||
81 | echo $str; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Generate the html "br" tag |
||
86 | * @param integer $nb the number of generated "<br />" tag |
||
87 | * @param boolean $return whether need return the generated html or just display it directly |
||
88 | * |
||
89 | * @return string|void the generated "br" html if $return is true or display it if not |
||
90 | */ |
||
91 | public static function br($nb = 1, $return = true){ |
||
92 | $nb = (int) $nb; |
||
93 | $str = null; |
||
94 | for ($i = 1; $i <= $nb; $i++) { |
||
95 | $str .= '<br />'; |
||
96 | } |
||
97 | |||
98 | if($return){ |
||
99 | return $str; |
||
100 | } |
||
101 | echo $str; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Generate the html content for tag "hr" |
||
106 | * @param integer $nb the number of generated "<hr />" tag |
||
107 | * @param array $attributes the tag attributes |
||
108 | * @param boolean $return whether need return the generated html or just display it directly |
||
109 | * |
||
110 | * @return string|void the generated "hr" html if $return is true or display it if not. |
||
111 | */ |
||
112 | public static function hr($nb = 1, array $attributes = array(), $return = true){ |
||
113 | $nb = (int) $nb; |
||
114 | $str = null; |
||
115 | for ($i = 1; $i <= $nb; $i++) { |
||
116 | $str .= '<hr' .attributes_to_string($attributes). ' />'; |
||
117 | } |
||
118 | if($return){ |
||
119 | return $str; |
||
120 | } |
||
121 | echo $str; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Generate the html content for tag like h1, h2, h3, h4, h5 and h6 |
||
126 | * @param integer $type the tag type 1 mean h1, 2 h2, etc, |
||
127 | * @param string $text the display text |
||
128 | * @param integer $nb the number of generated "<h{1,2,3,4,5,6}>" |
||
129 | * @param array $attributes the tag attributes |
||
130 | * @param boolean $return whether need return the generated html or just display it directly |
||
131 | * |
||
132 | * @return string|void the generated header html if $return is true or display it if not. |
||
133 | */ |
||
134 | public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
||
135 | $nb = (int) $nb; |
||
136 | $type = (int) $type; |
||
137 | if($type <= 0 || $type > 6){ |
||
138 | $type = 1; |
||
139 | } |
||
140 | $str = null; |
||
141 | for ($i = 1; $i <= $nb; $i++) { |
||
142 | $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
||
143 | } |
||
144 | if($return){ |
||
145 | return $str; |
||
146 | } |
||
147 | echo $str; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Generate the html "ul" tag |
||
152 | * @param array $data the data to use for each "li" tag |
||
153 | * @param array $attributes the "ul" properties attribute use the array index below for each tag: |
||
154 | * for ul "ul", for li "li". |
||
155 | * @param boolean $return whether need return the generated html or just display it directly |
||
156 | * |
||
157 | * @return string|void the generated "ul" html if $return is true or display it if not. |
||
158 | */ |
||
159 | public static function ul($data = array(), $attributes = array(), $return = true){ |
||
160 | $data = (array) $data; |
||
161 | $str = null; |
||
162 | $str .= '<ul' . (! empty($attributes['ul']) ? attributes_to_string($attributes['ul']):'') . '>'; |
||
163 | foreach ($data as $row) { |
||
164 | $str .= '<li' . (! empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
||
165 | } |
||
166 | $str .= '</ul>'; |
||
167 | if($return){ |
||
168 | return $str; |
||
169 | } |
||
170 | echo $str; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Generate the html "ol" tag |
||
175 | * @param array $data the data to use for each "li" tag |
||
176 | * @param array $attributes the "ol" properties attribute use the array index below for each tag: |
||
177 | * for ol "ol", for li "li". |
||
178 | * @param boolean $return whether need return the generated html or just display it directly |
||
179 | * @return string|void the generated "ol" html if $return is true or display it if not. |
||
180 | */ |
||
181 | public static function ol($data = array(), $attributes = array(), $return = true){ |
||
182 | $data = (array) $data; |
||
183 | $str = null; |
||
184 | $str .= '<ol' . (!empty($attributes['ol']) ? attributes_to_string($attributes['ol']):'') . '>'; |
||
185 | foreach ($data as $row) { |
||
186 | $str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>'; |
||
187 | } |
||
188 | $str .= '</ol>'; |
||
189 | if($return){ |
||
190 | return $str; |
||
191 | } |
||
192 | echo $str; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Generate the html "table" tag |
||
197 | * @param array $headers the table headers to use between (<thead>) |
||
198 | * @param array $body the table body values between (<tbody>) |
||
199 | * @param array $attributes the table properties attribute use the array index below for each tag: |
||
200 | * for table "table", for thead "thead", for thead tr "thead_tr", |
||
201 | * for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot", |
||
202 | * for tfoot tr "tfoot_tr", for tfoot th "tfoot_th". |
||
203 | * @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values |
||
204 | * @param boolean $return whether need return the generated html or just display it directly |
||
205 | * @return string|void the generated "table" html if $return is true or display it if not. |
||
206 | */ |
||
207 | public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
||
250 | } |
||
251 | } |
||
252 |