Test Setup Failed
Push — dependabot/composer/squizlabs/... ( de5def )
by
unknown
14:28
created

Module.php (1 issue)

Labels
Severity

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
use JwPersistentUser\Service\CookieAuthenticationService;
7
use Zend\EventManager\EventInterface;
8
use Zend\EventManager\EventManager;
9
use Zend\ModuleManager\Feature;
10
use Zend\ServiceManager\ServiceManager;
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
        /** @var ServiceManager $sm */
28
        $sm = $e->getApplication()->getServiceManager();
29
30
        $request = $e->getApplication()->getRequest();
31
        $response = $e->getApplication()->getResponse();
32
33
        // Try to login from Cookie if applicable
34
        $service = new CookieAuthenticationService($sm);
35
        $service->setEventManager(new EventManager($em->getSharedManager()));
36
        $service->loginFrom($request, $response);
37
38
        (new WriteTokenToCookie($sm))->attachShared($em->getSharedManager());
0 ignored issues
show
It seems like $em->getSharedManager() can be null; however, attachShared() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
39
    }
40
41
    public function getAutoloaderConfig()
42
    {
43
        return [
44
            'Zend\Loader\StandardAutoloader' => [
45
                'namespaces' => [
46
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
47
                ],
48
            ],
49
        ];
50
    }
51
}
52