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.
Test Setup Failed
Push — master ( c5ade0...e039e4 )
by Gabriel
05:51
created

StartSession::startSession()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
nc 5
nop 1
dl 0
loc 21
rs 9.0534
c 1
b 0
f 0
1
<?php
2
3
namespace Nip\Session\Middleware;
4
5
use Interop\Http\ServerMiddleware\DelegateInterface;
6
use Nip\Cookie\Jar as CookieJar;
7
use Nip\Http\ServerMiddleware\Middlewares\ServerMiddlewareInterface;
8
use Nip\Request;
9
use Nip\Session\SessionManager;
10
use Psr\Http\Message\ServerRequestInterface;
11
12
/**
13
 * Class StartSession
14
 * @package Nip\Session\Middleware
15
 */
16
class StartSession implements ServerMiddlewareInterface
17
{
18
19
    /**
20
     * The session manager.
21
     *
22
     * @var SessionManager
23
     */
24
    protected $manager;
25
26
    /**
27
     * Indicates if the session was handled for the current request.
28
     *
29
     * @var bool
30
     */
31
    protected $sessionHandled = false;
32
33
    /**
34
     * Create a new session middleware.
35
     *
36
     * @param  SessionManager $manager
37
     */
38
    public function __construct(SessionManager $manager)
39
    {
40
        $this->manager = $manager;
41
    }
42
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
48
    {
49
        $this->sessionHandled = true;
50
        $this->startSession($request);
51
52
        return $delegate->process($request);
53
    }
54
55
    /**
56
     * Start the session for the given request.
57
     *
58
     * @param  ServerRequestInterface|Request $request
59
     */
60
    protected function startSession(ServerRequestInterface $request)
61
    {
62
63
        if ($request->isCLI() == false) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\ServerRequestInterface as the method isCLI() does only exist in the following implementations of said interface: Nip\Request.

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...
64
            $requestHTTP = $request->getHttp();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\ServerRequestInterface as the method getHttp() does only exist in the following implementations of said interface: Nip\Request.

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...
65
            $domain = $requestHTTP->getRootDomain();
66
            $sessionManager = $this->getManager();
67
68
            if (!$sessionManager->isAutoStart()) {
69
                $sessionManager->setRootDomain($domain);
70
//                $sessionManager->setLifetime(config('SESSION.lifetime'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
            }
72
73
            if ($domain != 'localhost') {
74
                CookieJar::instance()->setDefaults(
75
                    ['domain' => '.' . $domain]
76
                );
77
            }
78
            $sessionManager->init();
79
        }
80
    }
81
82
    /**
83
     * @return SessionManager
84
     */
85
    public function getManager(): SessionManager
86
    {
87
        return $this->manager;
88
    }
89
}
90