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
Pull Request — master (#3)
by Cees-Jan
01:57
created

Session   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 151
Duplicated Lines 15.89 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.45%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 24
loc 151
ccs 49
cts 53
cp 0.9245
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getId() 0 4 1
A setContents() 0 4 1
A getContents() 0 4 1
A getOldIds() 0 4 1
A begin() 12 12 3
A end() 0 11 2
A isActive() 0 4 1
A regenerate() 12 12 2
A toArray() 0 9 1
A fromArray() 0 14 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
final class Session
6
{
7
    /**
8
     * @var string
9
     */
10
    private $id;
11
12
    /**
13
     * @var array
14
     */
15
    private $contents;
16
17
    /**
18
     * @var SessionIdInterface
19
     */
20
    private $sessionId;
21
22
    /**
23
     * @var string[]
24
     */
25
    private $oldIds = [];
26
27
    /**
28
     * @var int
29
     */
30
    private $status = \PHP_SESSION_NONE;
31
32
    /**
33
     * @param string             $id
34
     * @param array              $contents
35
     * @param SessionIdInterface $sessionId
36
     */
37 16
    public function __construct(string $id, array $contents, SessionIdInterface $sessionId)
38
    {
39 16
        $this->id = $id;
40 16
        $this->contents = $contents;
41 16
        $this->sessionId = $sessionId;
42
43 16
        if ($this->id !== '') {
44 4
            $this->status = PHP_SESSION_ACTIVE;
45
        }
46 16
    }
47
48
    /**
49
     * @return string
50
     */
51 13
    public function getId(): string
52
    {
53 13
        return $this->id;
54
    }
55
56
    /**
57
     * @param array $contents
58
     */
59 4
    public function setContents(array $contents)
60
    {
61 4
        $this->contents = $contents;
62 4
    }
63
64
    /**
65
     * @return array
66
     */
67 12
    public function getContents(): array
68
    {
69 12
        return $this->contents;
70
    }
71
72
    /**
73
     * @return string[]
74
     */
75 13
    public function getOldIds(): array
76
    {
77 13
        return $this->oldIds;
78
    }
79
80 11 View Code Duplication
    public function begin()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82 11
        if ($this->status === \PHP_SESSION_ACTIVE) {
83
            return true;
84
        }
85
86 11
        $this->status = \PHP_SESSION_ACTIVE;
87
88 11
        if ($this->id === '') {
89 11
            $this->id = $this->sessionId->generate();
90
        }
91 11
    }
92
93 2
    public function end()
94
    {
95 2
        if ($this->status === \PHP_SESSION_NONE) {
96
            return true;
97
        }
98
99 2
        $this->oldIds[] = $this->id;
100 2
        $this->status = \PHP_SESSION_NONE;
101 2
        $this->id = '';
102 2
        $this->contents = [];
103 2
    }
104
105
    /**
106
     * @return bool
107
     */
108 13
    public function isActive(): bool
109
    {
110 13
        return $this->status === \PHP_SESSION_ACTIVE;
111
    }
112
113 1 View Code Duplication
    public function regenerate(): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        // Can only regenerate active sessions
116 1
        if ($this->status !== \PHP_SESSION_ACTIVE) {
117
            return false;
118
        }
119
120 1
        $this->oldIds[] = $this->id;
121 1
        $this->id = $this->sessionId->generate();
122
123 1
        return true;
124
    }
125
126 1
    public function toArray(): array
127
    {
128
        return [
129 1
            'id' => $this->id,
130 1
            'contents' => $this->contents,
131 1
            'oldIds' => $this->oldIds,
132 1
            'status' => $this->status,
133
        ];
134
    }
135
136
    /**
137
     * @param  array                     $session
138
     * @throws \InvalidArgumentException
139
     * @return Session
140
     */
141 1
    public function fromArray(array $session): self
142
    {
143 1
        if (!isset($session['id']) || !isset($session['contents']) || !isset($session['oldIds']) || !isset($session['oldIds'])) {
144
            throw new \InvalidArgumentException('Session array most contain "id", "contents", "oldIds", and "status".');
145
        }
146
147 1
        $clone = clone $this;
148 1
        $clone->id = $session['id'];
149 1
        $clone->contents = $session['contents'];
150 1
        $clone->oldIds = $session['oldIds'];
151 1
        $clone->status = $session['status'];
152
153 1
        return $clone;
154
    }
155
}
156