Thumbnail::handle()   C
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
c 0
b 0
f 0
rs 6.7272
cc 7
eloc 18
nc 6
nop 2
1
<?php
2
3
namespace yiicod\easyimage\tools;
4
5
use Exception;
6
use Imagine\Image\Box;
7
use Imagine\Image\BoxInterface;
8
use Imagine\Image\ManipulatorInterface;
9
use Imagine\Image\Point;
10
use yiicod\easyimage\base\ToolInterface;
11
12
/**
13
 * Class Thumbnail
14
 * Thumbnail image tool
15
 *
16
 * @author Virchenko Maksim <[email protected]>
17
 *
18
 * @package yiicod\easyimage\tools
19
 */
20
class Thumbnail implements ToolInterface
21
{
22
    /**
23
     * Handle image
24
     *
25
     * @param ManipulatorInterface $image
26
     * @param array $params
27
     *
28
     * @return ManipulatorInterface
29
     *
30
     * @throws Exception
31
     */
32
    public static function handle(ManipulatorInterface $image, array $params = []): ManipulatorInterface
33
    {
34
        if (false === isset($params['width']) && false === isset($params['height'])) {
35
            throw new Exception('Params "width" or "height" is required for action "Thumbnail"');
36
        }
37
38
        /** @var BoxInterface $size */
39
        $size = $image->getSize();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Imagine\Image\ManipulatorInterface as the method getSize() does only exist in the following implementations of said interface: Imagine\Gd\Image, Imagine\Gmagick\Image, Imagine\Image\AbstractImage, Imagine\Imagick\Image.

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...
40
41
        if (isset($params['width'], $params['height'])) {
42
            $newSize = $size->widen($params['width']);
43
44
            if ($newSize->getHeight() < $params['height']) {
45
                $newSize = $newSize->heighten($params['height']);
46
            }
47
48
            $image->resize($newSize);
49
            $result = $image->crop(new Point(
50
                max(0, round(($newSize->getWidth() - $params['width']) / 2)),
51
                max(0, round(($newSize->getHeight() - $params['height']) / 2))
52
            ), new Box($params['width'], $params['height']));
53
        } elseif (isset($params['width'])) {
54
            $result = $image->resize($size->widen($params['width']));
55
        } elseif (isset($params['height'])) {
56
            $result = $image->resize($size->heighten($params['height']));
57
        }
58
59
        return $result;
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
60
    }
61
}
62