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.

DisplayImage::getAttributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace HtImgModule\View\Helper;
4
5
use Zend\View\Helper\AbstractHtmlElement;
6
7
class DisplayImage extends AbstractHtmlElement
8
{
9
    /**
10
     * Attributes for HTML image tag
11
     *
12
     * @var array
13
     */
14
    protected $attributes;
15
16
    /**
17
     * Gets valid HTML image tag or self when $relativeName is not provided
18
     *
19
     * @param  string $relativeName Relative Path
20
     * @param  string $filter       Filter Service
21
     * @param  array  $attributes   Attributes for HTML image tag
22
     * @return string
23
     */
24 1
    public function __invoke($relativeName = null, $filter = null, $attributes = null)
25
    {
26 1
        if ($relativeName === null) {
27 1
            return $this;
28
        }
29 1
        if ($attributes !== null) {
30
            $this->setAttributes($attributes);
31
        }
32
33 1
        return $this->getImgTag($relativeName, $filter);
34
    }
35
36
    /**
37
     * Return valid HTML image tag
38
     *
39
     * @param  string $relativeName
40
     * @param  string $filter
41
     * @return string
42
     */
43 1
    public function getImgTag($relativeName, $filter)
44
    {
45 1
        $imgUrlHelper = $this->getView()->plugin('HtImgModule\View\Helper\ImgUrl');
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 plugin() 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...
46 1
        $this->attributes['src'] = $imgUrlHelper($relativeName, $filter);;
47
        $html = '<img'
48 1
            . $this->htmlAttribs($this->getAttributes())
49 1
            . $this->getClosingBracket();
50
51 1
        return $html;
52
    }
53
54
    /**
55
     * Sets attributes of HTML image tag
56
     *
57
     * @param  array $attributes
58
     * @return self
59
     */
60 2
    public function setAttributes(array $attributes)
61
    {
62 2
        $this->attributes = $attributes;
63
64 2
        return $this;
65
    }
66
67
    /**
68
     * Gets attributes of img tag
69
     *
70
     * @return array
71
     */
72 2
    public function getAttributes()
73
    {
74 2
        return $this->attributes;
75
    }
76
77
}
78