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 ( 6e271f...dbd39b )
by Cees-Jan
09:45 queued 08:31
created

SessionMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 66
ccs 23
cts 24
cp 0.9583
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 19 1
A getId() 0 14 3
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use HansOtt\PSR7Cookies\RequestCookies;
6
use HansOtt\PSR7Cookies\SetCookie;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use React\Cache\CacheInterface;
10
use Throwable;
11
use function React\Promise\resolve;
12
13
final class SessionMiddleware
14
{
15
    const ATTRIBUTE_NAME = 'wyrihaximus.react.http.middleware.session';
16
17
    /**
18
     * @var string
19
     */
20
    private $cookieName = '';
21
22
    /**
23
     * @var CacheInterface
24
     */
25
    private $cache;
26
27
    /**
28
     * @var array
29
     */
30
    private $cookieParams = [];
31
32
    /**
33
     * @param string         $cookieName
34
     * @param CacheInterface $cache
35
     * @param array          $cookieParams
36
     */
37 9
    public function __construct(string $cookieName, CacheInterface $cache, array $cookieParams = [])
38
    {
39 9
        $this->cookieName = $cookieName;
40 9
        $this->cache = $cache;
41 9
        $this->cookieParams = $cookieParams;
42 9
    }
43
44 9
    public function __invoke(ServerRequestInterface $request, callable $next)
45
    {
46 9
        $id = $this->getId($request);
47
48 8
        return $this->cache->get($id)->otherwise(function () {
49 8
            return resolve([]);
50
        })->then(function ($sessionData) use ($next, $request, $id) {
51 9
            $session = new Session($sessionData);
52 9
            $request = $request->withAttribute(self::ATTRIBUTE_NAME, $session);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $request, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
53
54 9
            return resolve($next($request))->then(function (ResponseInterface $response) use ($id, $session) {
55 9
                $this->cache->set($id, $session->getContents());
56 9
                $cookie = new SetCookie($this->cookieName, $id, ...$this->cookieParams);
0 ignored issues
show
Documentation introduced by
$this->cookieParams is of type array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57 9
                $response = $cookie->addToResponse($response);
58
59 9
                return $response;
60 9
            });
61 9
        });
62
    }
63
64 9
    private function getId(ServerRequestInterface $request): string
65
    {
66 9
        $cookies = RequestCookies::createFromRequest($request);
67
68
        try {
69 9
            if ($cookies->has($this->cookieName)) {
70 9
                return $cookies->get($this->cookieName)->getValue();
71
            }
72
        } catch (Throwable $et) {
73
            // Do nothing, only a not found will be thrown so generating our own id now
74
        }
75
76 8
        return bin2hex(random_bytes(128));
77
    }
78
}
79