Background::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace yiicod\easyimage\tools;
4
5
use Exception;
6
use Imagine\Image\Box;
7
use Imagine\Image\Color;
8
use Imagine\Image\ManipulatorInterface;
9
use Imagine\Image\Point;
10
use yiicod\easyimage\base\ToolInterface;
11
use yiicod\easyimage\EasyImage;
12
13
/**
14
 * Class Background
15
 * Background image color tool
16
 *
17
 * @author Virchenko Maksim <[email protected]>
18
 *
19
 * @package yiicod\easyimage\tools
20
 */
21
class Background implements ToolInterface
22
{
23
    /**
24
     * Handle image
25
     *
26
     * @param ManipulatorInterface $image
27
     * @param array $params
28
     *
29
     * @return ManipulatorInterface
30
     *
31
     * @throws Exception
32
     */
33
    public static function handle(ManipulatorInterface $image, array $params = []): ManipulatorInterface
34
    {
35
        if (false === isset($params['color'])) {
36
            throw new Exception('Param "color" is required for action "Background"');
37
        }
38
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
        $box = new Box($size->getWidth(), $size->getHeight());
42
        $thumb = EasyImage::getImagine()->create($box, new Color(
43
            $params['color'],
44
            $params['alpha'] ?? 0
45
        ));
46
47
        $thumb->paste($image, new Point(0, 0));
0 ignored issues
show
Compatibility introduced by
$image of type object<Imagine\Image\ManipulatorInterface> is not a sub-type of object<Imagine\Image\ImageInterface>. It seems like you assume a child interface of the interface Imagine\Image\ManipulatorInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
48
49
        return $thumb;
50
    }
51
}
52