Completed
Push — master ( f9114f...683fbb )
by Chauncey
03:31
created

ImagemagickCropEffect   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B doCrop() 0 20 5
1
<?php
2
3
namespace Charcoal\Image\Imagemagick\Effect;
4
5
use \Exception;
6
7
use \Charcoal\Image\Effect\AbstractCropEffect;
8
9
/**
10
 * Reisze Effect for the Imagemagick driver.
11
 *
12
 * See {@link http://www.imagemagick.org/script/command-line-processing.php#geometry Image Geometry}
13
 * for complete details about the geometry argument.
14
 */
15
class ImagemagickCropEffect extends AbstractCropEffect
16
{
17
    /**
18
     * @param  integer $width  The crop width.
19
     * @param  integer $height The crop height.
20
     * @param  integer $x      The x-position (in pixel) of the crop.
21
     * @param  integer $y      The y-position (in pixel) of the crop.
22
     * @return void
23
     */
24
    protected function doCrop($width, $height, $x, $y)
25
    {
26
        $option = '-crop';
27
28
        $geometry = $this->geometry();
29
        if ($geometry) {
30
            $params = [ $option.' '.$geometry ];
31
        } else {
32
            $params = [ '-gravity "'.$this->gravity().'"' ];
33
34
            $size = $width.'x'.$height.($x >= 0 ? '+' : '').$x.($y >= 0 ? '+' : '').$y;
35
            $params[] = $option.' '.$size;
36
        }
37
38
        if ($this->repage()) {
39
            $params[] = '+repage';
40
        }
41
42
        $this->image()->applyCmd(implode(' ', $params));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Charcoal\Image\ImageInterface as the method applyCmd() does only exist in the following implementations of said interface: Charcoal\Image\Imagemagick\ImagemagickImage.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
43
    }
44
}
45