| Conditions | 12 |
| Paths | 68 |
| Total Lines | 75 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 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 |
||
| 78 | protected function get_target_sizes( $load_filename ) { |
||
| 79 | $image = wp_get_image_editor( $load_filename ); |
||
| 80 | $w = $this->w; |
||
| 81 | $h = $this->h; |
||
| 82 | $crop = $this->crop; |
||
| 83 | |||
| 84 | $current_size = $image->get_size(); |
||
| 85 | $src_w = $current_size['width']; |
||
| 86 | $src_h = $current_size['height']; |
||
| 87 | $src_ratio = $src_w / $src_h; |
||
| 88 | if ( !$h ) { |
||
| 89 | $h = round( $w / $src_ratio ); |
||
| 90 | } |
||
| 91 | if ( !$w ) { |
||
| 92 | //the user wants to resize based on constant height |
||
| 93 | $w = round( $h * $src_ratio ); |
||
| 94 | } |
||
| 95 | if ( !$crop ) { |
||
| 96 | return array( |
||
| 97 | 'x' => 0, 'y' => 0, |
||
| 98 | 'src_w' => $src_w, 'src_h' => $src_h, |
||
| 99 | 'target_w' => $w, 'target_h' => $h |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | // Get ratios |
||
| 103 | $dest_ratio = $w / $h; |
||
| 104 | $src_wt = $src_h * $dest_ratio; |
||
| 105 | $src_ht = $src_w / $dest_ratio; |
||
| 106 | $src_x = $src_w / 2 - $src_wt / 2; |
||
| 107 | $src_y = ( $src_h - $src_ht ) / 6; |
||
| 108 | //now specific overrides based on options: |
||
| 109 | switch ( $crop ) { |
||
| 110 | case 'center': |
||
| 111 | // Get source x and y |
||
| 112 | $src_x = round( ( $src_w - $src_wt ) / 2 ); |
||
| 113 | $src_y = round( ( $src_h - $src_ht ) / 2 ); |
||
| 114 | break; |
||
| 115 | |||
| 116 | case 'top': |
||
| 117 | $src_y = 0; |
||
| 118 | break; |
||
| 119 | |||
| 120 | case 'bottom': |
||
| 121 | $src_y = $src_h - $src_ht; |
||
| 122 | break; |
||
| 123 | |||
| 124 | case 'top-center': |
||
| 125 | $src_y = round( ( $src_h - $src_ht ) / 4 ); |
||
| 126 | break; |
||
| 127 | |||
| 128 | case 'bottom-center': |
||
| 129 | $src_y = $src_h - $src_ht - round( ( $src_h - $src_ht ) / 4 ); |
||
| 130 | break; |
||
| 131 | |||
| 132 | case 'left': |
||
| 133 | $src_x = 0; |
||
| 134 | break; |
||
| 135 | |||
| 136 | case 'right': |
||
| 137 | $src_x = $src_w - $src_wt; |
||
| 138 | break; |
||
| 139 | } |
||
| 140 | // Crop the image |
||
| 141 | return ( $dest_ratio > $src_ratio ) |
||
| 142 | ? array( |
||
| 143 | 'x' => 0, 'y' => $src_y, |
||
| 144 | 'src_w' => $src_w, 'src_h' => $src_ht, |
||
| 145 | 'target_w' => $w, 'target_h' => $h |
||
| 146 | ) |
||
| 147 | : array( |
||
| 148 | 'x' => $src_x, 'y' => 0, |
||
| 149 | 'src_w' => $src_wt, 'src_h' => $src_h, |
||
| 150 | 'target_w' => $w, 'target_h' => $h |
||
| 151 | ); |
||
| 152 | } |
||
| 153 | |||
| 204 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.