Conditions | 11 |
Paths | 26 |
Total Lines | 41 |
Code Lines | 21 |
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 |
||
135 | public function scale( $width, $height, $fit = true ) |
||
136 | { |
||
137 | if( ( $info = getimagesize( $this->getFilepath() ) ) === false ) { |
||
138 | throw new \Aimeos\MW\Media\Exception( 'Unable to retrieve image size for: ' . $this->getFilepath() ); |
||
139 | } |
||
140 | |||
141 | if( $fit === true ) |
||
142 | { |
||
143 | list( $width, $height ) = $this->getSizeFitted( $info[0], $info[1], $width, $height ); |
||
144 | |||
145 | if( $info[0] <= $width && $info[1] <= $height ) { |
||
146 | return; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | if( !isset( $this->origimage ) ) { |
||
151 | $this->origimage = $this->image; |
||
152 | } else { |
||
153 | imagedestroy( $this->image ); |
||
154 | } |
||
155 | |||
156 | if( ( $this->image = imagecreatetruecolor( $width, $height ) ) === false ) { |
||
157 | throw new \Aimeos\MW\Media\Exception( 'Unable to create new image' ); |
||
158 | } |
||
159 | |||
160 | if( imagealphablending( $this->image, false ) === false ) { |
||
161 | throw new \Aimeos\MW\Media\Exception( sprintf( 'GD library failed (imagealphablending)') ); |
||
162 | } |
||
163 | |||
164 | if( ( $transparent = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 ) ) === false ) { |
||
165 | throw new \Aimeos\MW\Media\Exception( sprintf( 'GD library failed (imagecolorallocatealpha)') ); |
||
166 | } |
||
167 | |||
168 | if( imagefilledrectangle( $this->image, 0, 0, $width, $height, $transparent ) === false ) { |
||
169 | throw new \Aimeos\MW\Media\Exception( sprintf( 'GD library failed (imagefilledrectangle)') ); |
||
170 | } |
||
171 | |||
172 | if( imagecopyresampled( $this->image, $this->origimage, 0, 0, 0, 0, $width, $height, $info[0], $info[1] ) === false ) { |
||
173 | throw new \Aimeos\MW\Media\Exception( 'Unable to resize image' ); |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 |