| Conditions | 15 |
| Paths | 1153 |
| Total Lines | 71 |
| 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 |
||
| 76 | function foogallery_attachment_html_anchor( $foogallery_attachment, $args = array(), $output_image = true, $output_closing_tag = true ) { |
||
| 77 | if ( empty ( $this->url ) ) { |
||
| 78 | return ''; |
||
| 79 | } |
||
| 80 | |||
| 81 | $arg_defaults = array( |
||
| 82 | 'link' => 'image', |
||
| 83 | 'custom_link' => $this->custom_url |
||
| 84 | ); |
||
| 85 | |||
| 86 | $args = wp_parse_args( $args, $arg_defaults ); |
||
| 87 | |||
| 88 | $link = $args['link']; |
||
| 89 | |||
| 90 | $img = $this->html_img( $args ); |
||
| 91 | |||
| 92 | if ( 'page' === $link ) { |
||
| 93 | //get the URL to the attachment page |
||
| 94 | $url = get_attachment_link( $this->ID ); |
||
| 95 | } else if ( 'custom' === $link ) { |
||
| 96 | $url = $args['custom_link']; |
||
| 97 | } else { |
||
| 98 | $url = $this->url; |
||
| 99 | } |
||
| 100 | |||
| 101 | //fallback for images that might not have a custom url |
||
| 102 | if ( empty( $url ) ) { |
||
| 103 | $url = $this->url; |
||
| 104 | } |
||
| 105 | |||
| 106 | $attr = array(); |
||
| 107 | |||
| 108 | //only add href and target attributes to the anchor if the link is NOT set to 'none' |
||
| 109 | if ( $link !== 'none' ){ |
||
| 110 | $attr['href'] = $url; |
||
| 111 | if ( ! empty( $this->custom_target ) && 'default' !== $this->custom_target ) { |
||
| 112 | $attr['target'] = $this->custom_target; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | if ( ! empty( $this->caption ) ) { |
||
| 117 | $attr['data-caption-title'] = $this->caption; |
||
| 118 | } |
||
| 119 | |||
| 120 | if ( !empty( $this->description ) ) { |
||
| 121 | $attr['data-caption-desc'] = $this->description; |
||
| 122 | } |
||
| 123 | |||
| 124 | $attr['data-attachment-id'] = $this->ID; |
||
| 125 | |||
| 126 | //pull any custom attributes out the args |
||
| 127 | if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) { |
||
| 128 | $attr = array_merge( $attr, $args['link_attributes'] ); |
||
| 129 | } |
||
| 130 | |||
| 131 | $attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $foogallery_attachment ); |
||
| 132 | $attr = array_map( 'esc_attr', $attr ); |
||
| 133 | $html = '<a '; |
||
| 134 | foreach ( $attr as $name => $value ) { |
||
| 135 | $html .= " $name=" . '"' . $value . '"'; |
||
| 136 | } |
||
| 137 | $html .= '>'; |
||
| 138 | if ( $output_image ) { |
||
| 139 | $html .= $img; |
||
| 140 | } |
||
| 141 | if ( $output_closing_tag ) { |
||
| 142 | $html .= '</a>'; |
||
| 143 | }; |
||
| 144 | |||
| 145 | return apply_filters( 'foogallery_attachment_html_link', $html, $args, $foogallery_attachment ); |
||
| 146 | } |
||
| 147 | |||
| 173 | } |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.