| Conditions | 7 |
| Paths | 17 |
| Total Lines | 58 |
| Code Lines | 50 |
| 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 |
||
| 49 | public function run($load_filename, $save_filename) { |
||
| 50 | $w = $this->w; |
||
| 51 | $h = $this->h; |
||
| 52 | |||
| 53 | $bg = imagecreatetruecolor( $w, $h ); |
||
| 54 | $c = self::hexrgb( $this->color ); |
||
| 55 | $bgColor = imagecolorallocate( $bg, $c['red'], $c['green'], $c['blue'] ); |
||
| 56 | imagefill( $bg, 0, 0, $bgColor ); |
||
| 57 | $image = wp_get_image_editor( $load_filename ); |
||
| 58 | if ( !is_wp_error( $image ) ) { |
||
| 59 | $current_size = $image->get_size(); |
||
| 60 | $quality = $image->get_quality(); |
||
| 61 | $ow = $current_size['width']; |
||
| 62 | $oh = $current_size['height']; |
||
| 63 | $new_aspect = $w / $h; |
||
| 64 | $old_aspect = $ow / $oh; |
||
| 65 | if ( $new_aspect > $old_aspect ) { |
||
| 66 | //taller than goal |
||
| 67 | $h_scale = $h / $oh; |
||
| 68 | $owt = $ow * $h_scale; |
||
| 69 | $y = 0; |
||
| 70 | $x = $w / 2 - $owt / 2; |
||
| 71 | $oht = $h; |
||
| 72 | $image->crop( 0, 0, $ow, $oh, $owt, $oht ); |
||
| 73 | } else { |
||
| 74 | $w_scale = $w / $ow; |
||
| 75 | $oht = $oh * $w_scale; |
||
| 76 | $x = 0; |
||
| 77 | $y = $h / 2 - $oht / 2; |
||
| 78 | $owt = $w; |
||
| 79 | $image->crop( 0, 0, $ow, $oh, $owt, $oht ); |
||
| 80 | } |
||
| 81 | $result = $image->save( $save_filename ); |
||
| 82 | $func = 'imagecreatefromjpeg'; |
||
| 83 | $save_func = 'imagejpeg'; |
||
| 84 | $ext = pathinfo( $save_filename, PATHINFO_EXTENSION ); |
||
| 85 | if ( $ext == 'gif' ) { |
||
| 86 | $func = 'imagecreatefromgif'; |
||
| 87 | $save_func = 'imagegif'; |
||
| 88 | } else if ( $ext == 'png' ) { |
||
| 89 | $func = 'imagecreatefrompng'; |
||
| 90 | $save_func = 'imagepng'; |
||
| 91 | if ($quality > 9) { |
||
| 92 | $quality = $quality/10; |
||
| 93 | $quality = round(10 - $quality); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | $image = $func( $save_filename ); |
||
| 97 | imagecopy( $bg, $image, $x, $y, 0, 0, $owt, $oht ); |
||
| 98 | if ($save_func === 'imagegif') { |
||
| 99 | return $save_func( $bg, $save_filename ); |
||
| 100 | } |
||
| 101 | return $save_func( $bg, $save_filename, $quality ); |
||
| 102 | } else { |
||
| 103 | TimberHelper::error_log( $image ); |
||
| 104 | } |
||
| 105 | return false; |
||
| 106 | } |
||
| 107 | } |
||
| 108 |
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.