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.

ImgUrl::__invoke()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 19
rs 8.8571
c 2
b 0
f 0
ccs 11
cts 11
cp 1
cc 5
eloc 12
nc 6
nop 2
crap 5
1
<?php
2
3
namespace HtImgModule\View\Helper;
4
5
use Zend\View\Helper\AbstractHelper;
6
use HtImgModule\Service\CacheManagerInterface;
7
use HtImgModule\Imagine\Filter\FilterManagerInterface;
8
use HtImgModule\Imagine\Loader\LoaderManagerInterface;
9
10
class ImgUrl extends AbstractHelper
11
{
12
    /**
13
     * @var CacheManagerInterface
14
     */
15
    protected $cacheManager;
16
17
    /**
18
     * @var FilterManagerInterface
19
     */
20
    protected $filterManager;
21
22
    /**
23
     * @var LoaderManagerInterface
24
     */
25
    protected $loaderManager;
26
27
    /**
28
     * Constructor
29
     *
30
     * @param CacheManagerInterface  $cacheManager
31
     * @param FilterManagerInterface $filterManager
32
     * @param LoaderManagerInterface $loaderManager
33
     */
34 2
    public function __construct(
35
        CacheManagerInterface $cacheManager,
36
        FilterManagerInterface $filterManager,
37
        LoaderManagerInterface $loaderManager
38
    )
39
    {
40 2
        $this->cacheManager = $cacheManager;
41 2
        $this->filterManager = $filterManager;
42 2
        $this->loaderManager = $loaderManager;
43 2
    }
44
45
    /**
46
     * Gets url of image
47
     *
48
     * @param  string $relativeName Relative Path
49
     * @param  string $filter       Filter Service
50
     * @return string
51
     */
52 2
    public function __invoke($relativeName, $filter)
53
    {
54 2
        $filterOptions = $this->filterManager->getFilterOptions($filter);
55 2
        if (isset($filterOptions['format'])) {
56 1
            $format = $filterOptions['format'];
57
        } else {
58 1
            $binary = $this->loaderManager->loadBinary($relativeName, $filter);
59 1
            $format = $binary->getFormat() ?: 'png';
60
        }
61 2
        if ($this->cacheManager->isCachingEnabled($filter, $filterOptions) && $this->cacheManager->cacheExists($relativeName, $filter, $format)) {
62 1
            $basePathHelper = $this->getView()->plugin('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 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...
63
64 1
            return $basePathHelper() . '/'. $this->cacheManager->getCacheUrl($relativeName, $filter, $format);
65
        }
66
67 1
        $urlHelper = $this->getView()->plugin('url');
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...
68
69 1
        return $urlHelper('htimg/display', ['filter' => $filter], ['query' => ['relativePath' => $relativeName]]);
70
    }
71
}
72