| Total Complexity | 46 |
| Total Lines | 315 |
| 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 | $link = Url::site_url($link); |
||
| 40 | if(! $anchor){ |
||
| 41 | $anchor = $link; |
||
| 42 | } |
||
| 43 | $str = null; |
||
| 44 | $str .= '<a href = "'.$link.'"'; |
||
| 45 | $str .= attributes_to_string($attributes); |
||
| 46 | $str .= '>'; |
||
| 47 | $str .= $anchor; |
||
| 48 | $str .= '</a>'; |
||
| 49 | |||
| 50 | if($return){ |
||
| 51 | return $str; |
||
| 52 | } |
||
| 53 | echo $str; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Generate an mailto anchor link |
||
| 58 | * @param string $link the email address |
||
| 59 | * @param string $anchor the displayed value of the link |
||
| 60 | * @param array $attributes the additional attributes to be added |
||
| 61 | * @param boolean $return whether need return the generated html or just display it directly |
||
| 62 | * |
||
| 63 | * @return string|void the generated html for mailto link if $return is true or display it if not |
||
| 64 | */ |
||
| 65 | public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){ |
||
| 66 | if(! $anchor){ |
||
| 67 | $anchor = $link; |
||
| 68 | } |
||
| 69 | $str = null; |
||
| 70 | $str .= '<a href = "mailto:'.$link.'"'; |
||
| 71 | $str .= attributes_to_string($attributes); |
||
| 72 | $str .= '>'; |
||
| 73 | $str .= $anchor; |
||
| 74 | $str .= '</a>'; |
||
| 75 | |||
| 76 | if($return){ |
||
| 77 | return $str; |
||
| 78 | } |
||
| 79 | echo $str; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Generate the html "br" tag |
||
| 84 | * @param integer $nb the number of generated "<br />" tag |
||
| 85 | * @param boolean $return whether need return the generated html or just display it directly |
||
| 86 | * |
||
| 87 | * @return string|void the generated "br" html if $return is true or display it if not |
||
| 88 | */ |
||
| 89 | public static function br($nb = 1, $return = true){ |
||
| 90 | $nb = (int) $nb; |
||
| 91 | $str = null; |
||
| 92 | for ($i = 1; $i <= $nb; $i++) { |
||
| 93 | $str .= '<br />'; |
||
| 94 | } |
||
| 95 | |||
| 96 | if($return){ |
||
| 97 | return $str; |
||
| 98 | } |
||
| 99 | echo $str; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Generate the html content for tag "hr" |
||
| 104 | * @param integer $nb the number of generated "<hr />" tag |
||
| 105 | * @param array $attributes the tag attributes |
||
| 106 | * @param boolean $return whether need return the generated html or just display it directly |
||
| 107 | * |
||
| 108 | * @return string|void the generated "hr" html if $return is true or display it if not. |
||
| 109 | */ |
||
| 110 | public static function hr($nb = 1, array $attributes = array(), $return = true){ |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Generate the html content for tag like h1, h2, h3, h4, h5 and h6 |
||
| 124 | * @param integer $type the tag type 1 mean h1, 2 h2, etc, |
||
| 125 | * @param string $text the display text |
||
| 126 | * @param integer $nb the number of generated "<h{1,2,3,4,5,6}>" |
||
| 127 | * @param array $attributes the tag attributes |
||
| 128 | * @param boolean $return whether need return the generated html or just display it directly |
||
| 129 | * |
||
| 130 | * @return string|void the generated header html if $return is true or display it if not. |
||
| 131 | */ |
||
| 132 | public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){ |
||
| 133 | $nb = (int) $nb; |
||
| 134 | $type = (int) $type; |
||
| 135 | $str = null; |
||
| 136 | for ($i = 1; $i <= $nb; $i++) { |
||
| 137 | $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
||
| 138 | } |
||
| 139 | if($return){ |
||
| 140 | return $str; |
||
| 141 | } |
||
| 142 | echo $str; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Generate the html "ul" tag |
||
| 147 | * @param array $data the data to use for each "li" tag |
||
| 148 | * @param array $attributes the "ul" properties attribute use the array index below for each tag: |
||
| 149 | * for ul "ul", for li "li". |
||
| 150 | * @param boolean $return whether need return the generated html or just display it directly |
||
| 151 | * |
||
| 152 | * @return string|void the generated "ul" html if $return is true or display it if not. |
||
| 153 | */ |
||
| 154 | public static function ul($data = array(), $attributes = array(), $return = true){ |
||
| 155 | if($return){ |
||
| 156 | return self::buildUlOl($data, $attributes, true, 'ul'); |
||
| 157 | } |
||
| 158 | self::buildUlOl($data, $attributes, false, 'ul'); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Generate the html "ol" tag |
||
| 163 | * @param array $data the data to use for each "li" tag |
||
| 164 | * @param array $attributes the "ol" properties attribute use the array index below for each tag: |
||
| 165 | * for ol "ol", for li "li". |
||
| 166 | * @param boolean $return whether need return the generated html or just display it directly |
||
| 167 | * @return string|void the generated "ol" html if $return is true or display it if not. |
||
| 168 | */ |
||
| 169 | public static function ol($data = array(), $attributes = array(), $return = true){ |
||
| 170 | if($return){ |
||
| 171 | return self::buildUlOl($data, $attributes, true, 'ol'); |
||
| 172 | } |
||
| 173 | self::buildUlOl($data, $attributes, false, 'ol'); |
||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * Generate the html "table" tag |
||
| 179 | * @param array $headers the table headers to use between (<thead>) |
||
| 180 | * @param array $body the table body values between (<tbody>) |
||
| 181 | * @param array $attributes the table properties attribute use the array index below for each tag: |
||
| 182 | * for table "table", for thead "thead", for thead tr "thead_tr", |
||
| 183 | * for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot", |
||
| 184 | * for tfoot tr "tfoot_tr", for tfoot th "tfoot_th". |
||
| 185 | * @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values |
||
| 186 | * @param boolean $return whether need return the generated html or just display it directly |
||
| 187 | * @return string|void the generated "table" html if $return is true or display it if not. |
||
| 188 | */ |
||
| 189 | public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * This method is used to build the header of the html table |
||
| 213 | * @see Html::table |
||
| 214 | * @return string|null |
||
| 215 | */ |
||
| 216 | protected static function buildTableHeader(array $headers, $attributes = array()){ |
||
| 217 | $str = null; |
||
| 218 | $theadAttributes = ''; |
||
| 219 | if(! empty($attributes['thead'])){ |
||
| 220 | $theadAttributes = ' ' . attributes_to_string($attributes['thead']); |
||
| 221 | } |
||
| 222 | $theadtrAttributes = ''; |
||
| 223 | if(! empty($attributes['thead_tr'])){ |
||
| 224 | $theadtrAttributes = ' ' . attributes_to_string($attributes['thead_tr']); |
||
| 225 | } |
||
| 226 | $thAttributes = ''; |
||
| 227 | if(! empty($attributes['thead_th'])){ |
||
| 228 | $thAttributes = ' ' . attributes_to_string($attributes['thead_th']); |
||
| 229 | } |
||
| 230 | $str .= '<thead' . $theadAttributes .'>'; |
||
| 231 | $str .= '<tr' . $theadtrAttributes .'>'; |
||
| 232 | foreach ($headers as $value) { |
||
| 233 | $str .= '<th' . $thAttributes .'>' .$value. '</th>'; |
||
| 234 | } |
||
| 235 | $str .= '</tr>'; |
||
| 236 | $str .= '</thead>'; |
||
| 237 | return $str; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * This method is used to build the body of the html table |
||
| 242 | * @see Html::table |
||
| 243 | * @return string|null |
||
| 244 | */ |
||
| 245 | protected static function buildTableBody(array $body, $attributes = array()){ |
||
| 246 | $str = null; |
||
| 247 | $tbodyAttributes = ''; |
||
| 248 | if(! empty($attributes['tbody'])){ |
||
| 249 | $tbodyAttributes = ' ' . attributes_to_string($attributes['tbody']); |
||
| 250 | } |
||
| 251 | $tbodytrAttributes = ''; |
||
| 252 | if(! empty($attributes['tbody_tr'])){ |
||
| 253 | $tbodytrAttributes = ' ' . attributes_to_string($attributes['tbody_tr']); |
||
| 254 | } |
||
| 255 | $tbodytdAttributes = ''; |
||
| 256 | if(! empty($attributes['tbody_td'])){ |
||
| 257 | $tbodytdAttributes = ' ' . attributes_to_string($attributes['tbody_td']); |
||
| 258 | } |
||
| 259 | $str .= '<tbody' . $tbodyAttributes .'>'; |
||
| 260 | $str .= self::buildTableBodyContent($body, $tbodytrAttributes, $tbodytdAttributes); |
||
| 261 | $str .= '</tbody>'; |
||
| 262 | return $str; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * This method is used to build the body content of the html table |
||
| 267 | * @param array $body the table body data |
||
| 268 | * @param string $tbodytrAttributes the html attributes for each tr in tbody |
||
| 269 | * @param string $tbodytdAttributes the html attributes for each td in tbody |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | protected static function buildTableBodyContent(array $body, $tbodytrAttributes, $tbodytdAttributes){ |
||
| 273 | $str = null; |
||
| 274 | foreach ($body as $row) { |
||
| 275 | if(is_array($row)){ |
||
| 276 | $str .= '<tr' . $tbodytrAttributes .'>'; |
||
| 277 | foreach ($row as $value) { |
||
| 278 | $str .= '<td' . $tbodytdAttributes .'>' .$value. '</td>'; |
||
| 279 | } |
||
| 280 | $str .= '</tr>'; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | return $str; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * This method is used to build the footer of the html table |
||
| 288 | * @see Html::table |
||
| 289 | * @return string|null |
||
| 290 | */ |
||
| 291 | protected static function buildTableFooter(array $footers, $attributes = array()){ |
||
| 292 | $str = null; |
||
| 293 | $tfootAttributes = ''; |
||
| 294 | if(! empty($attributes['tfoot'])){ |
||
| 295 | $tfootAttributes = ' ' . attributes_to_string($attributes['tfoot']); |
||
| 296 | } |
||
| 297 | $tfoottrAttributes = ''; |
||
| 298 | if(! empty($attributes['tfoot_tr'])){ |
||
| 299 | $tfoottrAttributes = ' ' . attributes_to_string($attributes['tfoot_tr']); |
||
| 300 | } |
||
| 301 | $thAttributes = ''; |
||
| 302 | if(! empty($attributes['tfoot_th'])){ |
||
| 303 | $thAttributes = ' ' . attributes_to_string($attributes['tfoot_th']); |
||
| 304 | } |
||
| 305 | $str .= '<tfoot' . $tfootAttributes .'>'; |
||
| 306 | $str .= '<tr' . $tfoottrAttributes .'>'; |
||
| 307 | foreach ($footers as $value) { |
||
| 308 | $str .= '<th' . $thAttributes .'>' .$value. '</th>'; |
||
| 309 | } |
||
| 310 | $str .= '</tr>'; |
||
| 311 | $str .= '</tfoot>'; |
||
| 312 | return $str; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Return the HTML content for ol or ul tags |
||
| 317 | * @see Html::ol |
||
| 318 | * @see Html::ul |
||
| 319 | * @param string $olul the type 'ol' or 'ul' |
||
| 320 | * @return void|string |
||
| 321 | */ |
||
| 322 | protected static function buildUlOl($data = array(), $attributes = array(), $return = true, $olul = 'ul'){ |
||
| 342 | } |
||
| 343 | } |
||
| 344 |