| Conditions | 10 |
| Paths | 52 |
| Total Lines | 58 |
| Code Lines | 43 |
| Lines | 14 |
| Ratio | 24.14 % |
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 |
||
| 74 | protected function get_target_sizes( $load_filename ) { |
||
| 75 | $image = wp_get_image_editor( $load_filename ); |
||
| 76 | $w = $this->w; |
||
| 77 | $h = $this->h; |
||
| 78 | $crop = $this->crop; |
||
| 79 | |||
| 80 | $current_size = $image->get_size(); |
||
| 81 | $src_w = $current_size['width']; |
||
| 82 | $src_h = $current_size['height']; |
||
| 83 | $src_ratio = $src_w / $src_h; |
||
| 84 | if ( !$h ) { |
||
| 85 | $h = round( $w / $src_ratio ); |
||
| 86 | } |
||
| 87 | if ( !$w ) { |
||
| 88 | //the user wants to resize based on constant height |
||
| 89 | $w = round( $h * $src_ratio ); |
||
| 90 | } |
||
| 91 | View Code Duplication | if ( !$crop ) { |
|
| 92 | return array( |
||
| 93 | 'x' => 0, 'y' => 0, |
||
| 94 | 'src_w' => $src_w, 'src_h' => $src_h, |
||
| 95 | 'target_w' => $w, 'target_h' => $h |
||
| 96 | ); |
||
| 97 | } |
||
| 98 | // Get ratios |
||
| 99 | $dest_ratio = $w / $h; |
||
| 100 | $src_wt = $src_h * $dest_ratio; |
||
| 101 | $src_ht = $src_w / $dest_ratio; |
||
| 102 | $src_x = $src_w / 2 - $src_wt / 2; |
||
| 103 | $src_y = ( $src_h - $src_ht ) / 6; |
||
| 104 | //now specific overrides based on options: |
||
| 105 | if ( $crop == 'center' ) { |
||
| 106 | // Get source x and y |
||
| 107 | $src_x = round( ( $src_w - $src_wt ) / 2 ); |
||
| 108 | $src_y = round( ( $src_h - $src_ht ) / 2 ); |
||
| 109 | } else if ( $crop == 'top' ) { |
||
| 110 | $src_y = 0; |
||
| 111 | } else if ( $crop == 'bottom' ) { |
||
| 112 | $src_y = $src_h - $src_ht; |
||
| 113 | } else if ( $crop == 'left' ) { |
||
| 114 | $src_x = 0; |
||
| 115 | } else if ( $crop == 'right' ) { |
||
| 116 | $src_x = $src_w - $src_wt; |
||
| 117 | } |
||
| 118 | // Crop the image |
||
| 119 | View Code Duplication | if ( $dest_ratio > $src_ratio ) { |
|
| 120 | return array( |
||
| 121 | 'x' => 0, 'y' => $src_y, |
||
| 122 | 'src_w' => $src_w, 'src_h' => $src_ht, |
||
| 123 | 'target_w' => $w, 'target_h' => $h |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | return array( |
||
| 127 | 'x' => $src_x, 'y' => 0, |
||
| 128 | 'src_w' => $src_wt, 'src_h' => $src_h, |
||
| 129 | 'target_w' => $w, 'target_h' => $h |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | |||
| 183 |