| Total Complexity | 51 | 
| Total Lines | 314 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like Util 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 Util, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 22 | class Util { | ||
| 23 | /** | ||
| 24 | * Remote get body. | ||
| 25 | * | ||
| 26 | * @see https://developer.wordpress.org/reference/functions/wp_remote_request/ | ||
| 27 | * | ||
| 28 | * @param string $url The URL to use for the remote request. | ||
| 29 | * @param int $required_response_code The required response code. | ||
| 30 | * @param array $args The WordPress HTTP API request arguments. | ||
| 31 | * | ||
| 32 | * @return string|WP_Error | ||
| 33 | */ | ||
| 34 | 	public static function remote_get_body( $url, $required_response_code = 200, array $args = array() ) { | ||
| 35 | $result = wp_remote_request( $url, $args ); | ||
| 36 | |||
| 37 | 		if ( ! is_array( $result ) ) { | ||
| 38 | return $result; | ||
| 39 | } | ||
| 40 | |||
| 41 | $response_code = wp_remote_retrieve_response_code( $result ); | ||
| 42 | |||
| 43 | 		if ( $response_code === $required_response_code ) { | ||
| 44 | return wp_remote_retrieve_body( $result ); | ||
| 45 | } | ||
| 46 | |||
| 47 | return new WP_Error( | ||
| 48 | 'wrong_response_code', | ||
| 49 | sprintf( | ||
| 50 | __( 'The response code (<code>%1$s<code>) was incorrect, required response code <code>%2$s</code>.', 'pronamic_ideal' ), | ||
| 51 | $response_code, | ||
| 52 | $required_response_code | ||
| 53 | ) | ||
| 54 | ); | ||
| 55 | } | ||
| 56 | |||
| 57 | /** | ||
| 58 | * SimpleXML load string. | ||
| 59 | * | ||
| 60 | * @param string $string The XML string to convert to a SimpleXMLElement object. | ||
| 61 | * | ||
| 62 | * @return SimpleXMLElement|WP_Error | ||
| 63 | */ | ||
| 64 | 	public static function simplexml_load_string( $string ) { | ||
| 65 | $result = false; | ||
| 66 | |||
| 67 | // Suppress all XML errors. | ||
| 68 | $use_errors = libxml_use_internal_errors( true ); | ||
| 69 | |||
| 70 | // Load. | ||
| 71 | $xml = simplexml_load_string( $string ); | ||
| 72 | |||
| 73 | 		if ( false !== $xml ) { | ||
| 74 | $result = $xml; | ||
| 75 | 		} else { | ||
| 76 | $error = new WP_Error( 'simplexml_load_error', __( 'Could not load the XML string.', 'pronamic_ideal' ) ); | ||
| 77 | |||
| 78 | 			foreach ( libxml_get_errors() as $e ) { | ||
| 79 | $error->add( 'libxml_error', $e->message, $e ); | ||
| 80 | } | ||
| 81 | |||
| 82 | libxml_clear_errors(); | ||
| 83 | |||
| 84 | $result = $error; | ||
| 85 | } | ||
| 86 | |||
| 87 | // Set back to previous value. | ||
| 88 | libxml_use_internal_errors( $use_errors ); | ||
| 89 | |||
| 90 | return $result; | ||
| 91 | } | ||
| 92 | |||
| 93 | /** | ||
| 94 | * Amount to cents. | ||
| 95 | * | ||
| 96 | * @param float $price The amount to convert to cents. | ||
| 97 | * | ||
| 98 | * @return int | ||
| 99 | */ | ||
| 100 | 	public static function amount_to_cents( $price ) { | ||
| 102 | } | ||
| 103 | |||
| 104 | /** | ||
| 105 | * Cents to amount. | ||
| 106 | * | ||
| 107 | * @param int $cents The numberof cents to convert to an amount. | ||
| 108 | * | ||
| 109 | * @return float | ||
| 110 | */ | ||
| 111 | 	public static function cents_to_amount( $cents ) { | ||
| 112 | return $cents / 100; | ||
| 113 | } | ||
| 114 | |||
| 115 | /** | ||
| 116 | * Convert boolean to an numceric boolean. | ||
| 117 | * | ||
| 118 | * @see https://github.com/eet-nu/buckaroo-ideal/blob/master/lib/buckaroo-ideal/request.rb#L136 | ||
| 119 | * | ||
| 120 | * @param boolean $boolean The boolean to convert to 1 or 0. | ||
| 121 | * | ||
| 122 | * @return int | ||
| 123 | */ | ||
| 124 | 	public static function boolean_to_numeric( $boolean ) { | ||
| 125 | return $boolean ? 1 : 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Convert boolean to an string boolean. | ||
| 130 | * | ||
| 131 | * @see https://github.com/eet-nu/buckaroo-ideal/blob/master/lib/buckaroo-ideal/request.rb#L136 | ||
| 132 | * | ||
| 133 | * @param boolean $boolean The boolean to convert to the string 'true' or 'false'. | ||
| 134 | * | ||
| 135 | * @return int | ||
| 136 | */ | ||
| 137 | 	public static function boolean_to_string( $boolean ) { | ||
| 139 | } | ||
| 140 | |||
| 141 | /** | ||
| 142 | * Format price. | ||
| 143 | * | ||
| 144 | * @see https://github.com/woothemes/woocommerce/blob/v2.2.3/includes/wc-formatting-functions.php#L306-L347 | ||
| 145 | * @see https://github.com/woothemes/woocommerce/blob/v2.2.3/includes/wc-core-functions.php#L299-L376 | ||
| 146 | * | ||
| 147 | * @param float $amount The amount to format. | ||
| 148 | * @param string $currency The currency code for the currency symbol. | ||
| 149 | * | ||
| 150 | * @return string | ||
| 151 | */ | ||
| 152 | 	public static function format_price( $amount, $currency = null ) { | ||
| 153 | $float = filter_var( $amount, FILTER_VALIDATE_FLOAT ); | ||
| 154 | |||
| 155 | 		if ( false === $float ) { | ||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | $currency = ( null === $currency ) ? 'EUR' : $currency; | ||
| 160 | |||
| 161 | $currency_symbol = $currency; | ||
| 162 | |||
| 163 | // Currencies. | ||
| 164 | 		switch ( $currency ) { | ||
| 165 | case 'EUR': | ||
| 166 | $currency_symbol = '€'; | ||
| 167 | $formatted_amount = number_format( $float, 2, ',', '.' ); | ||
| 168 | |||
| 169 | break; | ||
| 170 | case 'USD': | ||
| 171 | $currency_symbol = '$'; | ||
| 172 | $formatted_amount = number_format( $float, 2, '.', ',' ); | ||
| 173 | |||
| 174 | break; | ||
| 175 | case 'NLG': | ||
| 176 | $currency_symbol = 'G'; | ||
| 177 | $formatted_amount = number_format( $float, 4, '.', '' ); | ||
| 178 | |||
| 179 | break; | ||
| 180 | } | ||
| 181 | |||
| 182 | 		if ( ! isset( $formatted_amount ) ) { | ||
| 183 | $formatted_amount = number_format_i18n( $float, 2 ); | ||
| 184 | } | ||
| 185 | |||
| 186 | // @see https://en.wikipedia.org/wiki/Non-breaking_space#Keyboard_entry_methods | ||
| 187 | $non_breaking_space = ' '; | ||
| 188 | |||
| 189 | return '' . $currency_symbol . $non_breaking_space . $formatted_amount; | ||
| 190 | } | ||
| 191 | |||
| 192 | /** | ||
| 193 | * Format interval. | ||
| 194 | * | ||
| 195 | * @param int $interval The interval number. | ||
| 196 | * @param string $period The period indicator. | ||
| 197 | * | ||
| 198 | * @return string | ||
| 199 | */ | ||
| 200 | 	public static function format_interval( $interval, $period ) { | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | /** | ||
| 222 | * Convert single interval period character to full name. | ||
| 223 | * | ||
| 224 | * @param string $interval_period string Short interval period (D, W, M or Y). | ||
| 225 | * | ||
| 226 | * @return string | ||
| 227 | */ | ||
| 228 | 	public static function to_interval_name( $interval_period ) { | ||
| 229 | 		switch ( $interval_period ) { | ||
| 230 | case 'D': | ||
| 231 | return 'days'; | ||
| 232 | case 'W': | ||
| 233 | return 'weeks'; | ||
| 234 | case 'M': | ||
| 235 | return 'months'; | ||
| 236 | case 'Y': | ||
| 237 | return 'years'; | ||
| 238 | } | ||
| 239 | |||
| 240 | return $interval_period; | ||
| 241 | } | ||
| 242 | |||
| 243 | /** | ||
| 244 | * Format frequency. | ||
| 245 | * | ||
| 246 | * @param int $frequency The number of times. | ||
| 247 | * | ||
| 248 | * @return string | ||
| 249 | */ | ||
| 250 | 	public static function format_frequency( $frequency ) { | ||
| 251 | 		if ( empty( $frequency ) ) { | ||
| 252 | return _x( 'Unlimited', 'Recurring payment', 'pronamic_ideal' ); | ||
| 253 | } | ||
| 254 | |||
| 255 | return sprintf( _n( '%s time', '%s times', $frequency, 'pronamic_ideal' ), $frequency ); | ||
| 256 | } | ||
| 257 | |||
| 258 | /** | ||
| 259 | * Build URL with the specified parameters | ||
| 260 | * | ||
| 261 | * @param string $url The URL to extend with specified parameters. | ||
| 262 | * @param array $parameters The parameters to add to the specified URL. | ||
| 263 | * | ||
| 264 | * @return string | ||
| 265 | */ | ||
| 266 | 	public static function build_url( $url, array $parameters ) { | ||
| 267 | return $url . '?' . _http_build_query( $parameters, null, '&' ); | ||
| 268 | } | ||
| 269 | |||
| 270 | /** | ||
| 271 | * Get hidden inputs HTML for data. | ||
| 272 | * | ||
| 273 | * @param array $data Array with name and value pairs to convert to hidden HTML input eleemnts. | ||
| 274 | * | ||
| 275 | * @return string | ||
| 276 | */ | ||
| 277 | 	public static function html_hidden_fields( $data ) { | ||
| 278 | $html = ''; | ||
| 279 | |||
| 280 | 		foreach ( $data as $name => $value ) { | ||
| 281 | $html .= sprintf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $name ), esc_attr( $value ) ); | ||
| 282 | } | ||
| 283 | |||
| 284 | return $html; | ||
| 285 | } | ||
| 286 | |||
| 287 | /** | ||
| 288 | * Array to HTML attributes. | ||
| 289 | * | ||
| 290 | * @param array $attributes The key and value pairs to convert to HTML attributes. | ||
| 291 | * | ||
| 292 | * @return string | ||
| 293 | */ | ||
| 294 | 	public static function array_to_html_attributes( array $attributes ) { | ||
| 304 | } | ||
| 305 | |||
| 306 | /** | ||
| 307 | * Select options grouped. | ||
| 308 | * | ||
| 309 | * @param array $groups The grouped select options. | ||
| 310 | * @param string $selected_value The selected value. | ||
| 311 | * | ||
| 312 | * @return string | ||
| 313 | */ | ||
| 314 | 	public static function select_options_grouped( $groups, $selected_value = null ) { | ||
| 336 | } | ||
| 337 | } | ||
| 338 |