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

ImagemagickResizeEffect::doResize()   C

Complexity

Conditions 8
Paths 20

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 40
rs 5.3846
c 2
b 0
f 0
cc 8
eloc 25
nc 20
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
        if ($this->adaptive()) {
26
            $option = '-adaptive-resize';
27
        } else {
28
            $option = '-resize';
29
        }
30
31
        $size = $this->size();
32
        if ($size) {
33
            $params = [ $option.' '.$size ];
34
        } else {
35
            if ($width === 0 && $height === 0) {
36
                return;
37
            }
38
39
            $params = [
40
                '-gravity "'.$this->gravity().'"',
41
                '-background "'.$this->backgroundColor().'"'
42
            ];
43
44
            if ($width === 0) {
45
                $width = '';
46
            }
47
48
            if ($height === 0) {
49
                $height = '';
50
            }
51
52
            $size = $width.'x'.$height;
53
            if ($bestFit) {
54
                $params[] = $option.' '.$size.'^';
55
                $params[] = '-extent '.$size;
56
            } else {
57
                $params[] = $option.' '.$size;
58
            }
59
        }
60
61
        $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...
62
    }
63
}
64