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.
Completed
Push — master ( 2f9008...830571 )
by Bram
01:55
created

src/StrokerCache/Listener/CacheListener.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @author Bram Gerritsen [email protected]
4
 * @copyright (c) Bram Gerritsen 2013
5
 * @license http://opensource.org/licenses/mit-license.php
6
 */
7
8
namespace StrokerCache\Listener;
9
10
use StrokerCache\Options\ModuleOptions;
11
use StrokerCache\Service\CacheService;
12
use Zend\EventManager\AbstractListenerAggregate;
13
use Zend\EventManager\EventManagerInterface;
14
use Zend\Http\PhpEnvironment\Request as HttpRequest;
15
use Zend\Mvc\MvcEvent;
16
17
class CacheListener extends AbstractListenerAggregate
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $listeners = array();
23
24
    /**
25
     * @var CacheService
26
     */
27
    protected $cacheService;
28
29
    /**
30
     * @var ModuleOptions
31
     */
32
    protected $options;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $loadedFromCache = false;
38
39
    /**
40
     * Default constructor
41
     *
42
     * @param CacheService $cacheService
43
     * @param ModuleOptions $options
44
     */
45 8
    public function __construct(CacheService $cacheService, ModuleOptions $options)
46
    {
47 8
        $this->cacheService = $cacheService;
48 8
        $this->options      = $options;
49 8
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54 1
    public function attach(EventManagerInterface $events, $priority = 1)
55
    {
56 1
        $this->listeners[] = $events->attach('route', [$this, 'onRoute'], 100);
57 1
        $this->listeners[] = $events->attach('finish', [$this, 'onFinish'], -100);
58 1
    }
59
60
    /**
61
     * Load the page contents from the cache and set the response.
62
     *
63
     * @param  MvcEvent                            $e
64
     * @return \Zend\Stdlib\ResponseInterface|void
65
     */
66 6
    public function onRoute(MvcEvent $e)
67
    {
68 6
        if (!$e->getRequest() instanceof HttpRequest) {
69 1
            return;
70
        }
71
72 5
        $data = $this->getCacheService()->load($e);
73
74 5
        if ($data !== null) {
75 3
            $this->loadedFromCache = true;
76
77 3
            if ($this->getOptions()->getCacheResponse() === true) {
78 2
                $response = unserialize($data);
79 2
            } else {
80 1
                $response = $e->getResponse();
81 1
                $response->setContent($data);
82
            }
83
84 3
            $response->getHeaders()->addHeaderLine('X-Stroker-Cache', 'Hit');
85
86 3
            return $response;
87
        }
88 2
        $e->getResponse()->getHeaders()->addHeaderLine('X-Stroker-Cache', 'Miss');
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\ResponseInterface as the method getHeaders() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Response, Zend\Http\Response, Zend\Http\Response\Stream.

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...
89 2
    }
90
91
    /**
92
     * Save page contents to the cache
93
     *
94
     * @param  MvcEvent $e
95
     * @return void
96
     */
97 4
    public function onFinish(MvcEvent $e)
98
    {
99 4
        if (!$e->getRequest() instanceof HttpRequest || $this->loadedFromCache) {
100 3
            return;
101
        }
102
103 1
        $this->getCacheService()->save($e);
104 1
    }
105
106
    /**
107
     * @return CacheService
108
     */
109 5
    public function getCacheService()
110
    {
111 5
        return $this->cacheService;
112
    }
113
114
    /**
115
     * @return ModuleOptions
116
     */
117 3
    public function getOptions()
118
    {
119 3
        return $this->options;
120
    }
121
}
122