StartSession   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 54
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSession() 0 7 1
A getSessionId() 0 4 1
A handle() 0 8 2
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Illuminate\Contracts\Auth\Guard;
6
use Illuminate\Session\SessionManager;
7
use Illuminate\Http\Request;
8
use Closure;
9
10
/**
11
 * Class StartSession
12
 * @package App\Http\Middleware
13
 */
14
class StartSession extends \Illuminate\Session\Middleware\StartSession
15
{
16
    /**
17
     * @var Guard
18
     */
19
    protected $auth;
20
21
    /**
22
     * Create a new session middleware.
23
     * StartSession constructor.
24
     * @param SessionManager $manager
25
     * @param Guard $auth
26
     */
27
    public function __construct(SessionManager $manager, Guard $auth)
28
    {
29
        parent::__construct($manager);
30
        $this->manager = $manager;
31
        $this->auth = $auth;
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function getSession(Request $request)
38
    {
39
        $session = $this->manager->driver();
40
        $session->setId($this->getSessionId($request, $session));
0 ignored issues
show
Unused Code introduced by
The call to StartSession::getSessionId() has too many arguments starting with $session.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
41
42
        return $session;
43
    }
44
45
    /**
46
     * @param Request $request
47
     * @return mixed
48
     */
49
    protected function getSessionId(Request $request)
50
    {
51
        return $this->auth->getSession($request);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Guard as the method getSession() does only exist in the following implementations of said interface: Illuminate\Auth\SessionGuard.

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...
52
    }
53
54
    /**
55
     * Handle an incoming request, but skip OPTIONS method
56
     *
57
     * @inheritdoc
58
     */
59
    public function handle($request, Closure $next)
60
    {
61
        if ($request->isMethod('options')) {
62
            return $next($request);
63
        }
64
65
        return parent::handle($request, $next);
66
    }
67
}
68