Passed
Push — master ( de7162...f256de )
by Brian
739:27 queued 623:54
created

AUI_Component_Alert::get()   F

Complexity

Conditions 17
Paths 1537

Size

Total Lines 68
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 17
eloc 38
c 1
b 0
f 1
nc 1537
nop 1
dl 0
loc 68
rs 1.0499

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * A component class for rendering a bootstrap alert.
9
 *
10
 * @since 1.0.0
11
 */
12
class AUI_Component_Alert {
13
14
	/**
15
	 * Build the component.
16
	 * 
17
	 * @param array $args
18
	 *
19
	 * @return string The rendered component.
20
	 */
21
	public static function get($args = array()){
22
		$defaults = array(
23
			'type'       => 'info',
24
			'class'      => '',
25
			'icon' => '',
26
			'heading'    => '',
27
			'content'    => '',
28
			'footer'     => '',
29
			'dismissible'=> false,
30
			'data'       => '',
31
		);
32
33
		/**
34
		 * Parse incoming $args into an array and merge it with $defaults
35
		 */
36
		$args   = wp_parse_args( $args, $defaults );
37
		$output = '';
38
		if ( ! empty( $args['content'] ) ) {
39
			$type = sanitize_html_class( $args['type'] );
40
			if($type=='error'){$type='danger';}
41
			$icon = !empty($args['icon']) ? "<i class='".esc_attr($args['icon'])."'></i>" : '';
42
43
			// set default icon
44
			if(!$icon && $args['icon']!==false && $type){
45
				if($type=='danger'){$icon = '<i class="fas fa-exclamation-circle"></i>';}
46
				elseif($type=='warning'){$icon = '<i class="fas fa-exclamation-triangle"></i>';}
47
				elseif($type=='success'){$icon = '<i class="fas fa-check-circle"></i>';}
48
				elseif($type=='info'){$icon = '<i class="fas fa-info-circle"></i>';}
49
			}
50
51
			$data = '';
52
			$class = !empty($args['class']) ? esc_attr($args['class']) : '';
53
			if($args['dismissible']){$class .= " alert-dismissible fade show";}
54
55
			// open
56
			$output .= '<div class="alert alert-' . $type . ' '.$class.'" role="alert" '.$data.'>';
57
58
			// heading
59
			if ( ! empty( $args['heading'] ) ) {
60
				$output .= '<h4 class="alert-heading">' . $args['heading'] . '</h4>';
61
			}
62
63
			// icon
64
			if ( ! empty( $icon) ) {
65
				$output .= $icon." ";
66
			}
67
68
			// content
69
			$output .= $args['content'];
70
71
			// dismissible
72
			if($args['dismissible']){
73
				$output .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
74
				$output .= '<span aria-hidden="true">&times;</span>';
75
				$output .= '</button>';
76
			}
77
78
			// footer
79
			if ( ! empty( $args['footer'] ) ) {
80
				$output .= '<hr>';
81
				$output .= '<p class="mb-0">' . $args['footer'] . '</p>';
82
			}
83
84
			// close
85
			$output .= '</div>';
86
		}
87
88
		return $output;
89
	}
90
91
}