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.

Resize::gamma()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace TckImageResizer\View\Helper;
4
5
use Zend\View\Helper\AbstractHelper;
6
use TckImageResizer\Util\UrlSafeBase64;
7
8
class Resize extends AbstractHelper
9
{
10
    /** @var  array */
11
    protected $imgParts;
12
    /** @var  string*/
13
    protected $commands;
14
15
    /**
16
     * @param $imgPath
17
     * @return $this
18
     */
19 33
    public function __invoke($imgPath)
20
    {
21 33
        $this->imgParts = pathinfo($imgPath);
0 ignored issues
show
Documentation Bug introduced by
It seems like pathinfo($imgPath) of type * is incompatible with the declared type array of property $imgParts.

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...
22 33
        $this->commands = '';
23
        
24 33
        return $this;
25
    }
26
27
    /**
28
     * @param $width
29
     * @param $height
30
     * @return $this
31
     */
32 3
    public function thumb($width, $height)
33
    {
34 3
        $this->commands .= '$thumb,' . $width . ',' . $height;
35
        
36 3
        return $this;
37
    }
38
39
    /**
40
     * @param $width
41
     * @param $height
42
     * @return $this
43
     */
44 3
    public function resize($width, $height)
45
    {
46 3
        $this->commands .= '$resize,' . $width . ',' . $height;
47
        
48 3
        return $this;
49
    }
50
51
    /**
52
     * @return $this
53
     */
54 3
    public function grayscale()
55
    {
56 3
        $this->commands .= '$grayscale';
57
        
58 3
        return $this;
59
    }
60
61
    /**
62
     * @return $this
63
     */
64 3
    public function negative()
65
    {
66 3
        $this->commands .= '$negative';
67
        
68 3
        return $this;
69
    }
70
71
    /**
72
     * @param $correction
73
     * @return $this
74
     */
75 3
    public function gamma($correction)
76
    {
77 3
        $this->commands .= '$gamma,' . $correction;
78
        
79 3
        return $this;
80
    }
81
82
    /**
83
     * @param $hexColor
84
     * @return $this
85
     */
86 3
    public function colorize($hexColor)
87
    {
88 3
        $this->commands .= '$colorize,' . $hexColor;
89
        
90 3
        return $this;
91
    }
92
93
    /**
94
     * @return $this
95
     */
96 3
    public function sharpen()
97
    {
98 3
        $this->commands .= '$sharpen';
99
        
100 3
        return $this;
101
    }
102
103
    /**
104
     * @param null $sigma
105
     * @return $this
106
     */
107 3
    public function blur($sigma = null)
108
    {
109 3
        $this->commands .= '$blur' . ($sigma !== null ? ',' . $sigma : '');
110
        
111 3
        return $this;
112
    }
113
114
    /**
115
     * @param null $text
116
     * @param null $backgroundColor
117
     * @param null $color
118
     * @param null $width
119
     * @param null $height
120
     * @return $this
121
     */
122 3
    public function x404($text = null, $backgroundColor = null, $color = null, $width = null, $height = null)
123
    {
124 3
        $this->commands .= '$404'
125 3
                . ($text !== null ? ',' . UrlSafeBase64::encode($text) : '')
126 3
                . ($backgroundColor !== null ? ',' . $backgroundColor : '')
127 3
                . ($color !== null ? ',' . $color : '')
128 3
                . ($width !== null ? ',' . $width : '')
129 3
                . ($height !== null ? ',' . $height : '');
130
        
131 3
        return $this;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 30
    public function __toString()
138
    {
139 30
        return $this->getView()->basePath(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\View\Renderer\RendererInterface as the method basePath() does only exist in the following implementations of said interface: Zend\View\Renderer\PhpRenderer.

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...
140
            'processed/'
141 30
            . ($this->imgParts['dirname'] && $this->imgParts['dirname'] !== '.' ? $this->imgParts['dirname'] . '/' : '')
142 30
            . $this->imgParts['filename'] . '.'
143 30
            . $this->commands . '.' . $this->imgParts['extension']
144 30
        );
145
    }
146
}
147