Conditions | 18 |
Paths | 2 |
Total Lines | 120 |
Lines | 0 |
Ratio | 0 % |
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 |
||
57 | public function add_fields( $form_fields, $post = null ) { |
||
58 | $custom_fields = $this->get_custom_fields(); |
||
59 | |||
60 | // If our fields array is not empty |
||
61 | if ( ! empty( $custom_fields ) ) { |
||
62 | // We browse our set of options |
||
63 | foreach ( $custom_fields as $field => $values ) { |
||
64 | //remove any help, as it just looks untidy |
||
65 | if ( isset( $values['helps'] ) ) { |
||
66 | unset( $values['helps'] ); |
||
67 | } |
||
68 | |||
69 | if ( empty( $values['exclusions'] ) ) { |
||
70 | $values['exclusions'] = array(); |
||
71 | } |
||
72 | |||
73 | // If the field matches the current attachment mime type |
||
74 | // and is not one of the exclusions |
||
75 | if ( !in_array( $post->post_mime_type, $values['exclusions'] ) ) { |
||
76 | // We get the already saved field meta value |
||
77 | $meta = apply_filters( 'foogallery_attachment_custom_field_value', get_post_meta( $post->ID, '_' . $field, true ), $post->ID, $field, $values ); |
||
78 | |||
79 | switch ( $values['input'] ) { |
||
80 | default: |
||
81 | case 'text': |
||
82 | $values['input'] = 'text'; |
||
83 | break; |
||
84 | |||
85 | case 'textarea': |
||
86 | $values['input'] = 'textarea'; |
||
87 | break; |
||
88 | |||
89 | case 'select': |
||
90 | |||
91 | // Select type doesn't exist, so we will create the html manually |
||
92 | // For this, we have to set the input type to 'html' |
||
93 | $values['input'] = 'html'; |
||
94 | |||
95 | // Create the select element with the right name (matches the one that wordpress creates for custom fields) |
||
96 | $html = '<select name="attachments[' . $post->ID . '][' . $field . ']">'; |
||
97 | |||
98 | // If options array is passed |
||
99 | if ( isset( $values['options'] ) ) { |
||
100 | // Browse and add the options |
||
101 | foreach ( $values['options'] as $k => $v ) { |
||
102 | // Set the option selected or not |
||
103 | if ( $meta == $k ) |
||
104 | $selected = ' selected="selected"'; |
||
105 | else |
||
106 | $selected = ''; |
||
107 | |||
108 | $html .= '<option' . $selected . ' value="' . $k . '">' . $v . '</option>'; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | $html .= '</select>'; |
||
113 | |||
114 | // Set the html content |
||
115 | $values['html'] = $html; |
||
116 | |||
117 | break; |
||
118 | |||
119 | case 'checkbox': |
||
120 | |||
121 | // Checkbox type doesn't exist either |
||
122 | $values['input'] = 'html'; |
||
123 | |||
124 | // Set the checkbox checked or not |
||
125 | if ( $meta == 'on' ) |
||
126 | $checked = ' checked="checked"'; |
||
127 | else |
||
128 | $checked = ''; |
||
129 | |||
130 | $html = '<input' . $checked . ' type="checkbox" name="attachments[' . $post->ID . '][' . $field . ']" id="attachments-' . $post->ID . '-' . $field . '" />'; |
||
131 | |||
132 | $values['html'] = $html; |
||
133 | |||
134 | break; |
||
135 | |||
136 | case 'radio': |
||
137 | |||
138 | // radio type doesn't exist either |
||
139 | $values['input'] = 'html'; |
||
140 | |||
141 | $html = ''; |
||
142 | |||
143 | if ( ! empty( $values['options'] ) ) { |
||
144 | $i = 0; |
||
145 | |||
146 | foreach ( $values['options'] as $k => $v ) { |
||
147 | if ( $meta == $k ) |
||
148 | $checked = ' checked="checked"'; |
||
149 | else |
||
150 | $checked = ''; |
||
151 | |||
152 | $html .= '<input' . $checked . ' value="' . $k . '" type="radio" name="attachments[' . $post->ID . '][' . $field . ']" id="' . sanitize_key( $field . '_' . $post->ID . '_' . $i ) . '" /> <label for="' . sanitize_key( $field . '_' . $post->ID . '_' . $i ) . '">' . $v . '</label><br />'; |
||
153 | $i++; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | $values['html'] = $html; |
||
158 | |||
159 | break; |
||
160 | } |
||
161 | |||
162 | // And set it to the field before building it |
||
163 | $values['value'] = $meta; |
||
164 | |||
165 | // We add our field into the $form_fields array |
||
166 | $form_fields[$field] = apply_filters( 'foogallery_attachment_field_' . $field, $values, $post->ID ); |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | //allow it to change |
||
172 | $form_fields = apply_filters( 'foogallery_attachment_add_fields', $form_fields ); |
||
173 | |||
174 | // We return the completed $form_fields array |
||
175 | return $form_fields; |
||
176 | } |
||
177 | |||
217 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.