Skip to content

Commit ad807e2

Browse files
committed
Update to PHP 8.1
1 parent 3e0ce21 commit ad807e2

File tree

5 files changed

+63
-37
lines changed

5 files changed

+63
-37
lines changed

src/JwtSession.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace ByJG\Session;
44

55
use ByJG\Util\JwtWrapper;
6+
use ByJG\Util\JwtWrapperException;
7+
use Exception;
68
use SessionHandlerInterface;
79

810
class JwtSession implements SessionHandlerInterface
@@ -101,7 +103,7 @@ public function destroy(string $id): bool
101103
* Sessions that have not updated for
102104
* the last maxlifetime seconds will be removed.
103105
* </p>
104-
* @return bool <p>
106+
* @return int|false <p>
105107
* The return value (usually TRUE on success, FALSE on failure).
106108
* Note this value is returned internally to PHP for processing.
107109
* </p>
@@ -158,7 +160,7 @@ public function read(string $id): string
158160
return $data->data;
159161
}
160162
return '';
161-
} catch (\Exception $ex) {
163+
} catch (Exception $ex) {
162164
return '';
163165
}
164166
}
@@ -179,7 +181,7 @@ public function read(string $id): string
179181
* The return value (usually TRUE on success, FALSE on failure).
180182
* Note this value is returned internally to PHP for processing.
181183
* </p>
182-
* @throws \ByJG\Util\JwtWrapperException
184+
* @throws JwtWrapperException
183185
* @since 5.4.0
184186
*/
185187
public function write(string $id, string $data): bool

src/JwtSessionException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace ByJG\Session;
33

4-
class JwtSessionException extends \Exception
4+
use Exception;
5+
6+
class JwtSessionException extends Exception
57
{
68

79
}

src/SessionConfig.php

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
class SessionConfig
1010
{
11-
protected $serverName;
11+
protected string $serverName;
1212

13-
protected $sessionContext = 'default';
14-
protected $timeoutMinutes = 20;
15-
protected $cookieDomain = null;
16-
protected $cookiePath = '/';
17-
protected $jwtKey = null;
18-
protected $replaceSessionHandler = null;
13+
protected string $sessionContext = 'default';
14+
protected int $timeoutMinutes = 20;
15+
protected ?string $cookieDomain = null;
16+
protected string $cookiePath = '/';
17+
protected ?JwtKeyInterface $jwtKey = null;
18+
protected ?bool $replaceSessionHandler = null;
1919

2020
/**
2121
* SessionConfig constructor.
@@ -26,95 +26,104 @@ public function __construct($serverName)
2626
$this->serverName = $serverName;
2727
}
2828

29-
public function withSessionContext($context) {
29+
public function withSessionContext($context): static
30+
{
3031
$this->sessionContext = $context;
3132
return $this;
3233
}
3334

34-
public function withTimeoutMinutes($timeout) {
35+
public function withTimeoutMinutes($timeout): static
36+
{
3537
$this->timeoutMinutes = $timeout;
3638
return $this;
3739
}
3840

39-
public function withTimeoutHours($timeout) {
41+
public function withTimeoutHours($timeout): static
42+
{
4043
$this->timeoutMinutes = $timeout * 60;
4144
return $this;
4245
}
4346

44-
public function withCookie($domain, $path = "/") {
47+
public function withCookie($domain, $path = "/"): static
48+
{
4549
$this->cookieDomain = $domain;
4650
$this->cookiePath = $path;
4751
return $this;
4852
}
4953

50-
public function withSecret($secret) {
54+
public function withSecret($secret): static
55+
{
5156
$this->jwtKey = new JwtKeySecret($secret);
5257
return $this;
5358
}
5459

55-
public function withRsaSecret($private, $public) {
60+
public function withRsaSecret($private, $public): static
61+
{
5662
$this->jwtKey = new JwtRsaKey($private, $public);
5763
return $this;
5864
}
5965

60-
public function replaceSessionHandler($startSession = true) {
66+
public function replaceSessionHandler($startSession = true): static
67+
{
6168
$this->replaceSessionHandler = $startSession;
6269
return $this;
6370
}
6471

6572
/**
66-
* @return mixed
73+
* @return string
6774
*/
68-
public function getServerName()
75+
public function getServerName(): string
6976
{
7077
return $this->serverName;
7178
}
7279

7380
/**
7481
* @return string
7582
*/
76-
public function getSessionContext()
83+
public function getSessionContext(): string
7784
{
7885
return $this->sessionContext;
7986
}
8087

8188
/**
8289
* @return int
8390
*/
84-
public function getTimeoutMinutes()
91+
public function getTimeoutMinutes(): int
8592
{
8693
return $this->timeoutMinutes;
8794
}
8895

8996
/**
90-
* @return null
97+
* @return string|null
9198
*/
92-
public function getCookieDomain()
99+
public function getCookieDomain(): ?string
93100
{
94101
return $this->cookieDomain;
95102
}
96103

97104
/**
98105
* @return string
99106
*/
100-
public function getCookiePath()
107+
public function getCookiePath(): string
101108
{
102109
return $this->cookiePath;
103110
}
104111

105112
/**
106-
* @return JwtKeyInterface
113+
* @return JwtKeyInterface|null
107114
*/
108-
public function getKey()
115+
public function getKey(): ?JwtKeyInterface
109116
{
110117
return $this->jwtKey;
111118
}
112119

113-
public function isReplaceSession() {
120+
public function isReplaceSession(): bool
121+
{
114122
return $this->replaceSessionHandler !== null;
115123
}
116124

117-
public function isStartSession() {
125+
public function isStartSession(): bool
126+
{
118127
return $this->replaceSessionHandler === true;
119128
}
120129
}

tests/JwtSessionRsaTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<?php
22

33
use ByJG\Session\JwtSession;
4+
use ByJG\Session\JwtSessionException;
5+
use ByJG\Session\SessionConfig;
46

57
require_once __DIR__ . "/JwtSessionTest.php";
68

79
class JwtSessionRsaTest extends JwtSessionTest
810
{
11+
/**
12+
* @throws JwtSessionException
13+
*/
914
protected function setUp(): void
1015
{
1116
$secret = <<<PRIVATE
@@ -50,7 +55,7 @@ protected function setUp(): void
5055
-----END PUBLIC KEY-----
5156
PUBLIC;
5257

53-
$this->sessionConfig = (new \ByJG\Session\SessionConfig('example.com'))
58+
$this->sessionConfig = (new SessionConfig('example.com'))
5459
->withRsaSecret($secret, $public);
5560

5661
$this->object = new JwtSession($this->sessionConfig);

tests/JwtSessionTest.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
<?php
22

33
use ByJG\Session\JwtSession;
4+
use ByJG\Session\JwtSessionException;
5+
use ByJG\Session\SessionConfig;
6+
use ByJG\Util\JwtWrapperException;
7+
use PHPUnit\Framework\TestCase;
48

59
ob_start();
610
define("SETCOOKIE_FORTEST", "TESTCASE");
711

8-
class JwtSessionTest extends \PHPUnit\Framework\TestCase
12+
class JwtSessionTest extends TestCase
913
{
1014
/**
11-
* @var JwtSession
15+
* @var ?JwtSession
1216
*/
13-
protected $object;
17+
protected ?JwtSession $object;
1418

1519
/**
16-
* @var \ByJG\Session\SessionConfig
20+
* @var SessionConfig
1721
*/
18-
protected $sessionConfig;
22+
protected SessionConfig $sessionConfig;
1923

2024
const SESSION_ID = "sessionid";
2125

26+
/**
27+
* @throws JwtSessionException
28+
*/
2229
protected function setUp(): void
2330
{
24-
$this->sessionConfig = (new \ByJG\Session\SessionConfig('example.com'))
31+
$this->sessionConfig = (new SessionConfig('example.com'))
2532
->withSecret('secretKey');
2633

2734
$this->object = new JwtSession($this->sessionConfig);
@@ -50,7 +57,7 @@ public function testClose()
5057
$this->assertTrue($this->object->close());
5158
}
5259

53-
public function dataProvider()
60+
public function dataProvider(): array
5461
{
5562
$obj = new stdClass();
5663
$obj->prop1 = "value1";
@@ -139,6 +146,7 @@ public function testUnserializeData($expected, $input)
139146
* @dataProvider dataProvider
140147
* @param $object
141148
* @param $serialize
149+
* @throws JwtWrapperException
142150
*/
143151
public function testReadWrite($object, $serialize)
144152
{

0 commit comments

Comments
 (0)