Completed
Push — master ( b8b745...32257f )
by
unknown
08:12
created

ImagemagickResizeEffect::doResize()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 19
nc 6
nop 3
1
<?php
2
3
namespace Charcoal\Image\Imagemagick\Effect;
4
5
use \Exception;
6
7
use \Charcoal\Image\Effect\AbstractResizeEffect;
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 ImagemagickResizeEffect extends AbstractResizeEffect
16
{
17
    /**
18
     * @param integer $width   The target width.
19
     * @param integer $height  The target height.
20
     * @param boolean $bestFit The "best_fit" flag.
21
     * @return void
22
     */
23
    protected function doResize($width, $height, $bestFit = false)
24
    {
25
        $cmd = [
26
            '-gravity "'.$this->gravity().'"',
27
            '-background "'.$this->backgroundColor().'"'
28
        ];
29
30
        if ($this->adaptive()) {
31
            $option = '-adaptive-resize';
32
        } else {
33
            $option = '-resize';
34
        }
35
36
        $size = $this->size();
37
        if ($size) {
38
            $cmd[] = $option.' '.$size;
39
        } else {
40
            $size = $width.'x'.$height;
41
            if ($bestFit) {
42
                $cmd[] = $option.' '.$size.'^';
43
                $cmd[] = '-extent '.$size;
44
            } else {
45
                $cmd[] = $option.' '.$size;
46
            }
47
        }
48
49
        $this->image()->applyCmd(implode(' ', $cmd));
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...
50
    }
51
}
52