| Conditions | 30 |
| Paths | 12096 |
| Total Lines | 124 |
| Code Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 213 | public function destination_facet_render( $params ) { |
||
| 214 | $facet = $params['facet']; |
||
| 215 | |||
| 216 | $output = ''; |
||
| 217 | $values = (array) $params['values']; |
||
| 218 | $selected_values = (array) $params['selected_values']; |
||
| 219 | $soft_limit = empty( $facet['soft_limit'] ) ? 0 : (int) $facet['soft_limit']; |
||
| 220 | $countries = array(); |
||
| 221 | $continents = array(); |
||
| 222 | |||
| 223 | $continent_terms = get_terms( |
||
| 224 | array( |
||
| 225 | 'taxonomy' => 'continent', |
||
| 226 | ) |
||
| 227 | ); |
||
| 228 | |||
| 229 | if ( ! is_wp_error( $continent_terms ) ) { |
||
| 230 | foreach ( $continent_terms as $continent ) { |
||
| 231 | $continents[ $continent->term_id ] = $continent->slug; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | //Create a relationship of the facet value and the their depths |
||
| 236 | $depths = array(); |
||
| 237 | $parents = array(); |
||
| 238 | foreach ( $values as $value ) { |
||
| 239 | $depths[ $value['facet_value'] ] = (int) $value['depth']; |
||
| 240 | $parents[ $value['facet_value'] ] = (int) $value['parent_id']; |
||
| 241 | } |
||
| 242 | |||
| 243 | //Determine the current depth and check if the selected values parents are in the selected array. |
||
| 244 | $current_depth = 0; |
||
| 245 | $additional_values = array(); |
||
| 246 | if ( ! empty( $selected_values ) ) { |
||
| 247 | foreach ( $selected_values as $selected ) { |
||
| 248 | if ( $depths[ $selected ] > $current_depth ) { |
||
| 249 | $current_depth = $depths[ $selected ]; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | $current_depth++; |
||
| 253 | } |
||
| 254 | |||
| 255 | if ( ! empty( $additional_values ) ) { |
||
| 256 | $selected_values = array_merge( $selected_values, $additional_values ); |
||
| 257 | } |
||
| 258 | |||
| 259 | // This is where the items are sorted by their depth |
||
| 260 | $sorted_values = array(); |
||
| 261 | $stored = $values; |
||
| 262 | |||
| 263 | //sort the options so |
||
| 264 | foreach ( $values as $key => $result ) { |
||
| 265 | if ( ! empty( $this->options['display']['engine_search_continent_filter'] ) ) { |
||
| 266 | if ( in_array( $result['facet_value'], $continents ) ) { |
||
| 267 | $sorted_values[] = $result; |
||
| 268 | $destinations = $this->get_countries( $stored, $result['facet_value'], $continents, '1' ); |
||
| 269 | |||
| 270 | if ( ! empty( $destinations ) ) { |
||
| 271 | foreach ( $destinations as $destination ) { |
||
| 272 | $sorted_values[] = $destination; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } else { |
||
| 277 | if ( '0' === $result['depth'] || 0 === $result['depth'] ) { |
||
| 278 | $sorted_values[] = $result; |
||
| 279 | $destinations = $this->get_regions( $stored, $result['facet_value'], '1' ); |
||
| 280 | |||
| 281 | if ( ! empty( $destinations ) ) { |
||
| 282 | foreach ( $destinations as $destination ) { |
||
| 283 | $sorted_values[] = $destination; |
||
| 284 | } |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } |
||
| 289 | $values = $sorted_values; |
||
| 290 | |||
| 291 | $continent_class = ''; |
||
| 292 | $country_class = ''; |
||
| 293 | |||
| 294 | // Run through each value and output the values. |
||
| 295 | foreach ( $values as $key => $facet ) { |
||
| 296 | $depth_type = ''; |
||
| 297 | |||
| 298 | if ( ! empty( $this->options['display']['engine_search_continent_filter'] ) ) { |
||
| 299 | switch ( $facet['depth'] ) { |
||
| 300 | case '0': |
||
| 301 | $depth_type = ''; |
||
| 302 | $continent_class = in_array( $facet['facet_value'], $selected_values ) ? $depth_type .= ' continent-checked' : ''; |
||
| 303 | break; |
||
| 304 | |||
| 305 | case '1': |
||
| 306 | $depth_type = 'country' . $continent_class; |
||
| 307 | $country_class = in_array( $facet['facet_value'], $selected_values ) ? $depth_type .= ' country-checked' : ''; |
||
| 308 | break; |
||
| 309 | |||
| 310 | case '2': |
||
| 311 | $depth_type = 'region' . $continent_class . $country_class; |
||
| 312 | break; |
||
| 313 | } |
||
| 314 | } else { |
||
| 315 | switch ( $facet['depth'] ) { |
||
| 316 | case '0': |
||
| 317 | $depth_type = 'country continent-checked'; |
||
| 318 | $country_class = in_array( $facet['facet_value'], $selected_values ) ? $depth_type .= ' country-checked' : ''; |
||
| 319 | break; |
||
| 320 | |||
| 321 | case '1': |
||
| 322 | $depth_type = 'region continent-checked' . $country_class; |
||
| 323 | break; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | if ( $facet['depth'] <= $current_depth ) { |
||
| 328 | $options[] = $this->format_single_facet( $key, $facet, $selected_values, $depth_type ); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | if ( ! empty( $options ) ) { |
||
| 333 | $output = implode( '', $options ); |
||
| 334 | } |
||
| 335 | |||
| 336 | return $output; |
||
| 337 | } |
||
| 388 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.