GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Watermark::__construct()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.2222
c 0
b 0
f 0
ccs 11
cts 11
cp 1
cc 7
eloc 10
nc 5
nop 3
crap 7
1
<?php
2
namespace HtImgModule\Imagine\Filter;
3
4
use Imagine\Image\ImageInterface;
5
use Imagine\Image\Box;
6
use Imagine\Image\Point;
7
use HtImgModule\Exception;
8
9
class Watermark implements \Imagine\Filter\FilterInterface
10
{
11
    /**
12
     * @var string|int|null
13
     */
14
    protected $size;
15
16
    /**
17
     * @var string
18
     */
19
    protected $position;
20
21
    /**
22
     * @var ImageInterface
23
     */
24
    protected $watermark;
25
26
    /**
27
     * Constructor
28
     *
29
     * @param ImageInterface  $watermark
30
     * @param string|int|null $size
31
     * @param string          $position
32
     */
33 4
    public function __construct(ImageInterface $watermark, $size = null, $position = 'center')
34
    {
35 4
        $this->watermark = $watermark;
36 4
        $this->position = $position;
37 4
        if (!is_null($size)) {
38 3
            if (is_string($size) && substr($size, -1) === '%') {
39 2
                $size = substr($size, 0, -1) / 100;
40
            }
41 3
            if (!is_integer($size) && !is_double($size) && !is_float($size)) {
42 1
                throw new Exception\InvalidArgumentException(
43 1
                    sprintf('"%s" expects parameter 2, size to be integer, %s provider instead', __METHOD__, gettype($size))
44
                );
45
            }
46 2
            $this->size = $size;
0 ignored issues
show
Documentation Bug introduced by
It seems like $size can also be of type double. However, the property $size is declared as type string|integer|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
        }
48 3
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 2
    public function apply(ImageInterface $image)
54
    {
55 2
        $watermark = $this->watermark;
56
57 2
        $size = $image->getSize();
58 2
        $watermarkSize = $watermark->getSize();
59
60
        // If 'null': Downscale if needed
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61 2
        if (!$this->size && ($size->getWidth() < $watermarkSize->getWidth() || $size->getHeight() < $watermarkSize->getHeight())) {
62
            $this->size = 1.0;
0 ignored issues
show
Documentation Bug introduced by
It seems like 1.0 of type double is incompatible with the declared type string|integer|null of property $size.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
        }
64
65 2
        if ($this->size) {
66 1
            $factor = $this->size * min($size->getWidth() / $watermarkSize->getWidth(), $size->getHeight() / $watermarkSize->getHeight());
67
68 1
            $watermark->resize(new Box($watermarkSize->getWidth() * $factor, $watermarkSize->getHeight() * $factor));
69 1
            $watermarkSize = $watermark->getSize();
70
        }
71
72 2
        switch ($this->position) {
73 2
            case 'topleft':
74
                $x = 0;
75
                $y = 0;
76
                break;
77 2
            case 'top':
78
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
79
                $y = 0;
80
                break;
81 2
            case 'topright':
82
                $x = $size->getWidth() - $watermarkSize->getWidth();
83
                $y = 0;
84
                break;
85 2
            case 'left':
86
                $x = 0;
87
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
88
                break;
89 2
            case 'center':
90 1
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
91 1
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
92 1
                break;
93 2
            case 'right':
94 1
                $x = $size->getWidth() - $watermarkSize->getWidth();
95 1
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
96 1
                break;
97 1
            case 'bottomleft':
98
                $x = 0;
99
                $y = $size->getHeight() - $watermarkSize->getHeight();
100
                break;
101 1
            case 'bottom':
102
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
103
                $y = $size->getHeight() - $watermarkSize->getHeight();
104
                break;
105 1
            case 'bottomright':
106
                $x = $size->getWidth() - $watermarkSize->getWidth();
107
                $y = $size->getHeight() - $watermarkSize->getHeight();
108
                break;
109
            default:
110 1
                throw new Exception\InvalidArgumentException(
111 1
                    sprintf('Unknown position "%s"', $this->position)
112
                );
113
        }
114
115 1
        return $image->paste($watermark, new Point($x, $y));
116
    }
117
}
118