Conditions | 12 |
Paths | 12 |
Total Lines | 68 |
Lines | 43 |
Ratio | 63.24 % |
Changes | 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 |
||
74 | function spurs_wc_form_field_args( $args, $key, $value = null ) { |
||
75 | // Start field type switch case. |
||
76 | switch ( $args['type'] ) { |
||
77 | /* Targets all select input type elements, except the country and state select input types */ |
||
78 | View Code Duplication | case 'select' : |
|
|
|||
79 | // Add a class to the field's html element wrapper - woocommerce |
||
80 | // input types (fields) are often wrapped within a <p></p> tag. |
||
81 | $args['class'][] = 'form-group'; |
||
82 | // Add a class to the form input itself. |
||
83 | $args['input_class'] = array( 'form-control', 'input-lg' ); |
||
84 | $args['label_class'] = array( 'control-label' ); |
||
85 | $args['custom_attributes'] = array( |
||
86 | 'data-plugin' => 'select2', |
||
87 | 'data-allow-clear' => 'true', |
||
88 | 'aria-hidden' => 'true', |
||
89 | // Add custom data attributes to the form input itself. |
||
90 | ); |
||
91 | break; |
||
92 | // By default WooCommerce will populate a select with the country names - $args |
||
93 | // defined for this specific input type targets only the country select element. |
||
94 | case 'country' : |
||
95 | $args['class'][] = 'form-group single-country'; |
||
96 | $args['label_class'] = array( 'control-label' ); |
||
97 | break; |
||
98 | // By default WooCommerce will populate a select with state names - $args defined |
||
99 | // for this specific input type targets only the country select element. |
||
100 | View Code Duplication | case 'state' : |
|
101 | // Add class to the field's html element wrapper. |
||
102 | $args['class'][] = 'form-group'; |
||
103 | // add class to the form input itself. |
||
104 | $args['input_class'] = array( '', 'input-lg' ); |
||
105 | $args['label_class'] = array( 'control-label' ); |
||
106 | $args['custom_attributes'] = array( |
||
107 | 'data-plugin' => 'select2', |
||
108 | 'data-allow-clear' => 'true', |
||
109 | 'aria-hidden' => 'true', |
||
110 | ); |
||
111 | break; |
||
112 | case 'password' : |
||
113 | case 'text' : |
||
114 | case 'email' : |
||
115 | case 'tel' : |
||
116 | case 'number' : |
||
117 | $args['class'][] = 'form-group'; |
||
118 | $args['input_class'] = array( 'form-control', 'input-lg' ); |
||
119 | $args['label_class'] = array( 'control-label' ); |
||
120 | break; |
||
121 | View Code Duplication | case 'textarea' : |
|
122 | $args['input_class'] = array( 'form-control', 'input-lg' ); |
||
123 | $args['label_class'] = array( 'control-label' ); |
||
124 | break; |
||
125 | View Code Duplication | case 'checkbox' : |
|
126 | $args['label_class'] = array( 'custom-control custom-checkbox' ); |
||
127 | $args['input_class'] = array( 'custom-control-input', 'input-lg' ); |
||
128 | break; |
||
129 | View Code Duplication | case 'radio' : |
|
130 | $args['label_class'] = array( 'custom-control custom-radio' ); |
||
131 | $args['input_class'] = array( 'custom-control-input', 'input-lg' ); |
||
132 | break; |
||
133 | View Code Duplication | default : |
|
134 | $args['class'][] = 'form-group'; |
||
135 | $args['input_class'] = array( 'form-control', 'input-lg' ); |
||
136 | $args['label_class'] = array( 'control-label' ); |
||
137 | break; |
||
138 | } // end switch ($args). |
||
139 | |||
140 | return $args; |
||
141 | } |
||
142 | } |
||
179 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.