| Total Complexity | 49 |
| Total Lines | 307 |
| 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){ |
||
| 111 | $nb = (int) $nb; |
||
| 112 | $str = null; |
||
| 113 | for ($i = 1; $i <= $nb; $i++) { |
||
| 114 | $str .= '<hr' .attributes_to_string($attributes). ' />'; |
||
| 115 | } |
||
| 116 | if($return){ |
||
| 117 | return $str; |
||
| 118 | } |
||
| 119 | echo $str; |
||
| 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 | if($type <= 0 || $type > 6){ |
||
| 136 | $type = 1; |
||
| 137 | } |
||
| 138 | $str = null; |
||
| 139 | for ($i = 1; $i <= $nb; $i++) { |
||
| 140 | $str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>'; |
||
| 141 | } |
||
| 142 | if($return){ |
||
| 143 | return $str; |
||
| 144 | } |
||
| 145 | echo $str; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Generate the html "ul" tag |
||
| 150 | * @param array $data the data to use for each "li" tag |
||
| 151 | * @param array $attributes the "ul" properties attribute use the array index below for each tag: |
||
| 152 | * for ul "ul", for li "li". |
||
| 153 | * @param boolean $return whether need return the generated html or just display it directly |
||
| 154 | * |
||
| 155 | * @return string|void the generated "ul" html if $return is true or display it if not. |
||
| 156 | */ |
||
| 157 | public static function ul($data = array(), $attributes = array(), $return = true){ |
||
| 158 | $data = (array) $data; |
||
| 159 | $str = null; |
||
| 160 | $ulAttributes = ''; |
||
| 161 | if(! empty($attributes['ul'])){ |
||
| 162 | $ulAttributes = ' ' . attributes_to_string($attributes['ul']); |
||
| 163 | } |
||
| 164 | $liAttributes = ''; |
||
| 165 | if(! empty($attributes['li'])){ |
||
| 166 | $liAttributes = ' ' . attributes_to_string($attributes['li']); |
||
| 167 | } |
||
| 168 | $str .= '<ul' . $ulAttributes . '>'; |
||
| 169 | foreach ($data as $row) { |
||
| 170 | $str .= '<li' . $liAttributes .'>' .$row. '</li>'; |
||
| 171 | } |
||
| 172 | $str .= '</ul>'; |
||
| 173 | if($return){ |
||
| 174 | return $str; |
||
| 175 | } |
||
| 176 | echo $str; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Generate the html "ol" tag |
||
| 181 | * @param array $data the data to use for each "li" tag |
||
| 182 | * @param array $attributes the "ol" properties attribute use the array index below for each tag: |
||
| 183 | * for ol "ol", for li "li". |
||
| 184 | * @param boolean $return whether need return the generated html or just display it directly |
||
| 185 | * @return string|void the generated "ol" html if $return is true or display it if not. |
||
| 186 | */ |
||
| 187 | public static function ol($data = array(), $attributes = array(), $return = true){ |
||
| 188 | $data = (array) $data; |
||
| 189 | $str = null; |
||
| 190 | $olAttributes = ''; |
||
| 191 | if(! empty($attributes['ol'])){ |
||
| 192 | $olAttributes = ' ' . attributes_to_string($attributes['ol']); |
||
| 193 | } |
||
| 194 | $liAttributes = ''; |
||
| 195 | if(! empty($attributes['li'])){ |
||
| 196 | $liAttributes = ' ' . attributes_to_string($attributes['li']); |
||
| 197 | } |
||
| 198 | $str .= '<ol' . $olAttributes . '>'; |
||
| 199 | foreach ($data as $row) { |
||
| 200 | $str .= '<li' . $liAttributes .'>' .$row. '</li>'; |
||
| 201 | } |
||
| 202 | $str .= '</ol>'; |
||
| 203 | if($return){ |
||
| 204 | return $str; |
||
| 205 | } |
||
| 206 | echo $str; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Generate the html "table" tag |
||
| 211 | * @param array $headers the table headers to use between (<thead>) |
||
| 212 | * @param array $body the table body values between (<tbody>) |
||
| 213 | * @param array $attributes the table properties attribute use the array index below for each tag: |
||
| 214 | * for table "table", for thead "thead", for thead tr "thead_tr", |
||
| 215 | * for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot", |
||
| 216 | * for tfoot tr "tfoot_tr", for tfoot th "tfoot_th". |
||
| 217 | * @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values |
||
| 218 | * @param boolean $return whether need return the generated html or just display it directly |
||
| 219 | * @return string|void the generated "table" html if $return is true or display it if not. |
||
| 220 | */ |
||
| 221 | public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){ |
||
| 222 | $headers = (array) $headers; |
||
| 223 | $body = (array) $body; |
||
| 224 | $str = null; |
||
| 225 | $tableAttributes = ''; |
||
| 226 | if(! empty($attributes['table'])){ |
||
| 227 | $tableAttributes = ' ' . attributes_to_string($attributes['table']); |
||
| 228 | } |
||
| 229 | $str .= '<table' . $tableAttributes . '>'; |
||
| 230 | $str .= self::buildTableHeader($headers, $attributes); |
||
| 231 | $str .= self::buildTableBody($body, $attributes); |
||
| 232 | |||
| 233 | if($use_footer){ |
||
| 234 | $str .= self::buildTableFooter($headers, $attributes); |
||
| 235 | } |
||
| 236 | $str .= '</table>'; |
||
| 237 | if($return){ |
||
| 238 | return $str; |
||
| 239 | } |
||
| 240 | echo $str; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * This method is used to build the header of the html table |
||
| 245 | * @see Html::table |
||
| 246 | * @return string|null |
||
| 247 | */ |
||
| 248 | protected static function buildTableHeader(array $headers, $attributes = array()){ |
||
| 249 | $str = null; |
||
| 250 | if(! empty($headers)){ |
||
| 251 | $theadAttributes = ''; |
||
| 252 | if(! empty($attributes['thead'])){ |
||
| 253 | $theadAttributes = ' ' . attributes_to_string($attributes['thead']); |
||
| 254 | } |
||
| 255 | $theadtrAttributes = ''; |
||
| 256 | if(! empty($attributes['thead_tr'])){ |
||
| 257 | $theadtrAttributes = ' ' . attributes_to_string($attributes['thead_tr']); |
||
| 258 | } |
||
| 259 | $thAttributes = ''; |
||
| 260 | if(! empty($attributes['thead_th'])){ |
||
| 261 | $thAttributes = ' ' . attributes_to_string($attributes['thead_th']); |
||
| 262 | } |
||
| 263 | $str .= '<thead' . $theadAttributes .'>'; |
||
| 264 | $str .= '<tr' . $theadtrAttributes .'>'; |
||
| 265 | foreach ($headers as $value) { |
||
| 266 | $str .= '<th' . $thAttributes .'>' .$value. '</th>'; |
||
| 267 | } |
||
| 268 | $str .= '</tr>'; |
||
| 269 | $str .= '</thead>'; |
||
| 270 | } |
||
| 271 | return $str; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * This method is used to build the body of the html table |
||
| 276 | * @see Html::table |
||
| 277 | * @return string|null |
||
| 278 | */ |
||
| 279 | protected static function buildTableBody(array $body, $attributes = array()){ |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * This method is used to build the footer of the html table |
||
| 309 | * @see Html::table |
||
| 310 | * @return string|null |
||
| 311 | */ |
||
| 312 | protected static function buildTableFooter(array $footers, $attributes = array()){ |
||
| 334 | } |
||
| 335 | } |
||
| 336 |