Test Setup Failed
Push — master ( 4e7eeb...a41b36 )
by Roel van
17:34
created

Module.php (3 issues)

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
namespace JwPersistentUser;
4
5
use JwPersistentUser\Listener\WriteTokenToCookie,
6
    JwPersistentUser\Service\CookieAuthenticationService;
7
8
use Zend\ModuleManager\Feature,
9
    Zend\EventManager\EventManager,
10
    Zend\EventManager\EventInterface;
11
12
class Module implements
13
    Feature\ConfigProviderInterface,
14
    Feature\BootstrapListenerInterface,
15
    Feature\AutoloaderProviderInterface
16
{
17
    public function getConfig()
18
    {
19
        return include __DIR__ . '/config/module.config.php';
20
    }
21
22
    public function onBootstrap(EventInterface $e)
23
    {
24
        /** @var EventManager $em */
25
        $em = $e->getApplication()->getEventManager();
26
27
        $request = $e->getApplication()->getRequest();
28
        $response = $e->getApplication()->getResponse();
29
30
        // Write token to cookie after valid authentication
31
        $placeCookie = new WriteTokenToCookie;
32
        $placeCookie->setRequest($request);
33
        $placeCookie->setResponse($response);
34
        $placeCookie->setServiceLocator($e->getApplication()->getServiceManager());
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Zend\EventManager\EventInterface as the method getApplication() does only exist in the following implementations of said interface: Zend\Mvc\MvcEvent.

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...
35
        $em->getSharedManager()->attachAggregate($placeCookie);
0 ignored issues
show
The method attachAggregate() does not exist on Zend\EventManager\SharedEventManagerInterface. Did you maybe mean attach()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
36
37
        // Try to login from Cookie if applicable
38
        $service = new CookieAuthenticationService;
39
        $service->setServiceLocator($e->getApplication()->getServiceManager());
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Zend\EventManager\EventInterface as the method getApplication() does only exist in the following implementations of said interface: Zend\Mvc\MvcEvent.

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...
40
        $service->loginFrom($request, $response);
41
    }
42
43
    public function getAutoloaderConfig()
44
    {
45
        return [
46
            'Zend\Loader\StandardAutoloader' => [
47
                'namespaces' => [
48
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
49
                ],
50
            ],
51
        ];
52
    }
53
}
54