| Conditions | 10 |
| Paths | 10 |
| Total Lines | 47 |
| Code Lines | 37 |
| Lines | 12 |
| Ratio | 25.53 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 21 | protected function doCrop($width, $height, $x, $y) |
||
| 22 | { |
||
| 23 | $gravity = $this->image()->imagickGravity($this->gravity()); |
||
|
|
|||
| 24 | |||
| 25 | // This sets the gravity for the rest of the chain |
||
| 26 | $this->image()->imagick()->setGravity($gravity); |
||
| 27 | |||
| 28 | // Apply gravity to crop coordinates |
||
| 29 | |||
| 30 | $imageWidth = $this->image()->width(); |
||
| 31 | $imageHeight = $this->image()->height(); |
||
| 32 | |||
| 33 | switch ($this->image()->imagick()->getGravity()) { |
||
| 34 | case Imagick::GRAVITY_NORTHWEST: |
||
| 35 | break; |
||
| 36 | case Imagick::GRAVITY_NORTH: |
||
| 37 | $x = ($imageWidth / 2 - $width / 2); |
||
| 38 | break; |
||
| 39 | case Imagick::GRAVITY_NORTHEAST: |
||
| 40 | $x = ($imageWidth - $width); |
||
| 41 | break; |
||
| 42 | case Imagick::GRAVITY_WEST: |
||
| 43 | $y = ($imageHeight / 2 - $height / 2); |
||
| 44 | break; |
||
| 45 | View Code Duplication | case Imagick::GRAVITY_CENTER: |
|
| 46 | $x = ($imageWidth / 2 - $width / 2); |
||
| 47 | $y = ($imageHeight / 2 - $height / 2); |
||
| 48 | break; |
||
| 49 | View Code Duplication | case Imagick::GRAVITY_EAST: |
|
| 50 | $x = ($imageWidth - $width); |
||
| 51 | $y = ($imageHeight / 2 - $height / 2); |
||
| 52 | break; |
||
| 53 | case Imagick::GRAVITY_SOUTHWEST: |
||
| 54 | $y = ($imageHeight - $height); |
||
| 55 | break; |
||
| 56 | View Code Duplication | case Imagick::GRAVITY_SOUTH: |
|
| 57 | $x = ($imageWidth / 2 - $width / 2); |
||
| 58 | $y = ($imageHeight - $height); |
||
| 59 | break; |
||
| 60 | case Imagick::GRAVITY_SOUTHEAST: |
||
| 61 | $x = ($imageWidth - $width); |
||
| 62 | $y = ($imageHeight - $height); |
||
| 63 | break; |
||
| 64 | } |
||
| 65 | |||
| 66 | $this->image()->imagick()->cropImage($width, $height, $x, $y); |
||
| 67 | } |
||
| 68 | } |
||
| 69 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: