Conditions | 11 |
Paths | 12 |
Total Lines | 118 |
Lines | 46 |
Ratio | 38.98 % |
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 |
||
66 | function render_field( $field ) { |
||
67 | |||
68 | // decode value (convert to array) |
||
69 | $field['value'] = acf_get_array($field['value'], false); |
||
70 | |||
71 | |||
72 | // hiden input |
||
73 | acf_hidden_input(array( |
||
74 | 'type' => 'hidden', |
||
75 | 'name' => $field['name'], |
||
76 | )); |
||
77 | |||
78 | |||
79 | // vars |
||
80 | $i = 0; |
||
81 | $li = ''; |
||
82 | $all_checked = true; |
||
83 | |||
84 | |||
85 | // checkbox saves an array |
||
86 | $field['name'] .= '[]'; |
||
87 | |||
88 | |||
89 | // foreach choices |
||
90 | if( !empty($field['choices']) ) { |
||
91 | |||
92 | View Code Duplication | foreach( $field['choices'] as $value => $label ) { |
|
93 | |||
94 | // increase counter |
||
95 | $i++; |
||
96 | |||
97 | |||
98 | // vars |
||
99 | $atts = array( |
||
100 | 'type' => 'checkbox', |
||
101 | 'id' => $field['id'], |
||
102 | 'name' => $field['name'], |
||
103 | 'value' => $value, |
||
104 | ); |
||
105 | |||
106 | |||
107 | // is choice selected? |
||
108 | if( in_array($value, $field['value']) ) { |
||
109 | |||
110 | $atts['checked'] = 'checked'; |
||
111 | |||
112 | } else { |
||
113 | |||
114 | $all_checked = false; |
||
115 | |||
116 | } |
||
117 | |||
118 | |||
119 | if( isset($field['disabled']) && acf_in_array($value, $field['disabled']) ) { |
||
120 | |||
121 | $atts['disabled'] = 'disabled'; |
||
122 | |||
123 | } |
||
124 | |||
125 | |||
126 | // each input ID is generated with the $key, however, the first input must not use $key so that it matches the field's label for attribute |
||
127 | if( $i > 1 ) { |
||
128 | |||
129 | $atts['id'] .= '-' . $value; |
||
130 | |||
131 | } |
||
132 | |||
133 | |||
134 | // append HTML |
||
135 | $li .= '<li><label><input ' . acf_esc_attr( $atts ) . '/>' . $label . '</label></li>'; |
||
136 | |||
137 | } |
||
138 | |||
139 | |||
140 | // toggle all |
||
141 | if( $field['toggle'] ) { |
||
142 | |||
143 | // vars |
||
144 | $label = __("Toggle All", 'acf'); |
||
145 | $atts = array( |
||
146 | 'type' => 'checkbox', |
||
147 | 'class' => 'acf-checkbox-toggle' |
||
148 | ); |
||
149 | |||
150 | |||
151 | // custom label |
||
152 | if( is_string($field['toggle']) ) { |
||
153 | |||
154 | $label = $field['toggle']; |
||
155 | |||
156 | } |
||
157 | |||
158 | |||
159 | // checked |
||
160 | if( $all_checked ) { |
||
161 | |||
162 | $atts['checked'] = 'checked'; |
||
163 | |||
164 | } |
||
165 | |||
166 | |||
167 | // append HTML |
||
168 | $li = '<li><label><input ' . acf_esc_attr( $atts ) . '/>' . $label . '</label></li>' . $li; |
||
169 | |||
170 | } |
||
171 | |||
172 | } |
||
173 | |||
174 | |||
175 | // class |
||
176 | $field['class'] .= ' acf-checkbox-list'; |
||
177 | $field['class'] .= ($field['layout'] == 'horizontal') ? ' acf-hl' : ' acf-bl'; |
||
178 | |||
179 | |||
180 | // return |
||
181 | echo '<ul ' . acf_esc_attr(array( 'class' => $field['class'] )) . '>' . $li . '</ul>'; |
||
182 | |||
183 | } |
||
184 | |||
328 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.