Conditions | 15 |
Paths | 1153 |
Total Lines | 77 |
Code Lines | 40 |
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 |
||
141 | public function html( $args = array(), $output_image = true, $output_closing_tag = true ) { |
||
142 | if ( empty ( $this->url ) ) { |
||
143 | return ''; |
||
144 | } |
||
145 | |||
146 | $arg_defaults = array( |
||
147 | 'link' => 'image', |
||
148 | 'custom_link' => $this->custom_url |
||
149 | ); |
||
150 | |||
151 | $args = wp_parse_args( $args, $arg_defaults ); |
||
152 | |||
153 | $link = $args['link']; |
||
154 | |||
155 | $img = $this->html_img( $args ); |
||
156 | |||
157 | /* 12 Apr 2016 - PLEASE NOTE |
||
158 | We no longer just return the image html when "no link" option is chosen. |
||
159 | It was decided that it is better to return an anchor link with no href or target attributes. |
||
160 | This results in more standardized HTML output for better CSS and JS code |
||
161 | */ |
||
162 | |||
163 | if ( 'page' === $link ) { |
||
164 | //get the URL to the attachment page |
||
165 | $url = get_attachment_link( $this->ID ); |
||
166 | } else if ( 'custom' === $link ) { |
||
167 | $url = $args['custom_link']; |
||
168 | } else { |
||
169 | $url = $this->url; |
||
170 | } |
||
171 | |||
172 | //fallback for images that might not have a custom url |
||
173 | if ( empty( $url ) ) { |
||
174 | $url = $this->url; |
||
175 | } |
||
176 | |||
177 | $attr = array(); |
||
178 | |||
179 | //only add href and target attributes to the anchor if the link is NOT set to 'none' |
||
180 | if ( $link !== 'none' ){ |
||
181 | $attr['href'] = $url; |
||
182 | if ( ! empty( $this->custom_target ) && 'default' !== $this->custom_target ) { |
||
183 | $attr['target'] = $this->custom_target; |
||
184 | } |
||
185 | } |
||
186 | |||
187 | if ( ! empty( $this->caption ) ) { |
||
188 | $attr['data-caption-title'] = $this->caption; |
||
189 | } |
||
190 | |||
191 | if ( !empty( $this->description ) ) { |
||
192 | $attr['data-caption-desc'] = $this->description; |
||
193 | } |
||
194 | |||
195 | $attr['data-attachment-id'] = $this->ID; |
||
196 | |||
197 | //pull any custom attributes out the args |
||
198 | if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) { |
||
199 | $attr = array_merge( $attr, $args['link_attributes'] ); |
||
200 | } |
||
201 | |||
202 | $attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $this ); |
||
203 | $attr = array_map( 'esc_attr', $attr ); |
||
204 | $html = '<a '; |
||
205 | foreach ( $attr as $name => $value ) { |
||
206 | $html .= " $name=" . '"' . $value . '"'; |
||
207 | } |
||
208 | $html .= '>'; |
||
209 | if ( $output_image ) { |
||
210 | $html .= $img; |
||
211 | } |
||
212 | if ( $output_closing_tag ) { |
||
213 | $html .= '</a>'; |
||
214 | }; |
||
215 | |||
216 | return apply_filters( 'foogallery_attachment_html_link', $html, $args, $this ); |
||
217 | } |
||
218 | |||
246 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.