| Conditions | 3 |
| Paths | 3 |
| Total Lines | 58 |
| 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 |
||
| 91 | private function render_image( $image, $idx ) { |
||
| 92 | |||
| 93 | $attachment_id = $image['attachment_id']; |
||
| 94 | |||
| 95 | // Skip if the post doesn't exist anymore or has been fixed. |
||
| 96 | if ( ! $this->exists( $attachment_id ) ) { |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | |||
| 100 | $author = html_entity_decode( $image['author'] ); |
||
| 101 | |||
| 102 | $more_info_link_esc = esc_url( $image['more_info_link'] ); |
||
| 103 | |||
| 104 | $is_unknown_license = '#N/A' === $image['license']; |
||
| 105 | |||
| 106 | $caption_builder = new Caption_Builder( $image ); |
||
| 107 | $proposed_caption = $caption_builder->build(); |
||
| 108 | |||
| 109 | $script_id = "wl-image-$idx"; |
||
| 110 | $row_id = "wl-row-$idx"; |
||
| 111 | ?> |
||
| 112 | <tr id="<?php echo $row_id; ?>"> |
||
| 113 | <td><?php echo wp_get_attachment_image( $attachment_id, array( 100, ) ); ?></td> |
||
| 114 | <td><?php echo esc_html( $image['filename'] ); ?></td> |
||
| 115 | <td><?php echo esc_html( $image['license'] ); ?></td> |
||
| 116 | <td><?php echo $author; ?></td> |
||
| 117 | <td><?php echo $proposed_caption; ?></td> |
||
| 118 | <td> |
||
| 119 | <a href="<?php echo $more_info_link_esc; ?>" |
||
| 120 | target="_blank"><?php esc_html_e( 'More information', 'wordlift' ); ?></a> |
||
| 121 | </td> |
||
| 122 | <td> |
||
| 123 | <?php |
||
| 124 | $this->partial_used_in_posts( $image['posts_ids_as_featured_image'], __( 'Used as featured image in %d post(s):', 'wordlift' ) ); |
||
| 125 | $this->partial_used_in_posts( $image['posts_ids_as_embed'], __( 'Embedded in %d post(s):', 'wordlift' ) ); |
||
| 126 | ?> |
||
| 127 | </td> |
||
| 128 | <td> |
||
| 129 | <script type="application/json" |
||
| 130 | id="<?php echo $script_id; ?>"><?php echo json_encode( $image ); ?></script> |
||
| 131 | <button data-id="<?php echo $script_id; ?>" |
||
| 132 | data-row-id="<?php echo $row_id; ?>" |
||
| 133 | data-action="wl_remove_all_images_task" |
||
| 134 | class="button wl-action-btn"><?php esc_html_e( 'Remove image', 'wordlift' ); ?></button> |
||
| 135 | <?php if ( ! $is_unknown_license ) { ?> |
||
| 136 | <button data-id="<?php echo $script_id; ?>" |
||
| 137 | data-row-id="<?php echo $row_id; ?>" |
||
| 138 | data-action="wl_add_license_caption_or_remove" |
||
| 139 | class="button wl-action-btn"><?php esc_html_e( 'Add license caption', 'wordlift' ); ?></button> |
||
| 140 | <?php } ?> |
||
| 141 | <a class="button" |
||
| 142 | href=" <?php echo get_edit_post_link( $attachment_id ); ?>" |
||
| 143 | target="_blank"><?php esc_html_e( 'Edit image', 'wordlift' ); ?> <span |
||
| 144 | class="dashicons dashicons-external"></span></a> |
||
| 145 | </td> |
||
| 146 | </tr> |
||
| 147 | <?php |
||
| 148 | } |
||
| 149 | |||
| 205 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: