Skip to content

Commit 41ee499

Browse files
authored
Merge pull request #33 from WyriHaximus/update-code-to-1.0
Update code to be compatible with react/http 1.0
2 parents cedbd9d + 227d618 commit 41ee499

File tree

9 files changed

+51
-31
lines changed

9 files changed

+51
-31
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
"hansott/psr7-cookies": "^2.0 || ^1.0",
1414
"psr/http-message": "^1.0",
1515
"react/cache": "^0.5.0 || ^0.6.0 || ^1.0.0",
16-
"react/http": "^1.0.0",
16+
"react/http": "^1.0 || ^0.8",
1717
"react/promise": "^2.7"
1818
},
1919
"require-dev": {
20+
"thecodingmachine/safe": "^0.1 || ^1.0",
2021
"wyrihaximus/async-test-utilities": "^1.1"
2122
},
2223
"config": {

composer.lock

Lines changed: 22 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ parameters:
33
- '#Constructor in [a-zA-Z0-9\\_]+ has parameter \$[a-zA-Z0-9_]+ with default value.#'
44
- '#Method [a-zA-Z0-9\\_]+::[a-zA-Z0-9_]+\(\) has parameter \$[a-zA-Z0-9_]+ with a nullable type declaration.#'
55
- '#Method [a-zA-Z0-9\\_]+::[a-zA-Z0-9_]+\(\) has parameter \$[a-zA-Z0-9_]+ with null as default value.#'
6+
- '#Array \(array<string, mixed>\) does not accept key int|string.#'
7+
- '#In method \"WyriHaximus\\React\\Http\\Middleware\\SessionMiddleware::fetchSessionFromRequest\", caught \"Throwable\" must be rethrown.#'
68
classesAllowedToBeExtended:
79
- ApiClients\Tools\TestUtilities\TestCase
810

911
includes:
10-
- vendor/wyrihaximus/async-test-utilities/rules.neon
12+
- vendor/wyrihaximus/async-test-utilities/rules.neon

src/Session.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class Session
1010
private $id;
1111

1212
/**
13-
* @var array
13+
* @var array<string, mixed>
1414
*/
1515
private $contents;
1616

@@ -30,9 +30,9 @@ final class Session
3030
private $status = \PHP_SESSION_NONE;
3131

3232
/**
33-
* @param string $id
34-
* @param array $contents
35-
* @param SessionIdInterface $sessionId
33+
* @param string $id
34+
* @param array<string, mixed> $contents
35+
* @param SessionIdInterface $sessionId
3636
*/
3737
public function __construct(string $id, array $contents, SessionIdInterface $sessionId)
3838
{
@@ -54,15 +54,15 @@ public function getId(): string
5454
}
5555

5656
/**
57-
* @param array $contents
57+
* @param array<string, mixed> $contents
5858
*/
5959
public function setContents(array $contents): void
6060
{
6161
$this->contents = $contents;
6262
}
6363

6464
/**
65-
* @return array
65+
* @return array<string, mixed>
6666
*/
6767
public function getContents(): array
6868
{
@@ -123,6 +123,9 @@ public function regenerate(): bool
123123
return true;
124124
}
125125

126+
/**
127+
* @return array<string, mixed>
128+
*/
126129
public function toArray(): array
127130
{
128131
return [
@@ -134,14 +137,14 @@ public function toArray(): array
134137
}
135138

136139
/**
137-
* @param array $session
140+
* @param array<string, mixed> $session
138141
* @param bool $clone
139142
* @throws \InvalidArgumentException
140143
* @return Session
141144
*/
142145
public function fromArray(array $session, bool $clone = true): self
143146
{
144-
if (!isset($session['id']) || !isset($session['contents']) || !isset($session['oldIds']) || !isset($session['status'])) {
147+
if (!\array_key_exists('id', $session) || !\array_key_exists('contents', $session) || !\array_key_exists('oldIds', $session) || !\array_key_exists('status', $session)) {
145148
throw new \InvalidArgumentException('Session array most contain "id", "contents", "oldIds", and "status".');
146149
}
147150

src/SessionMiddleware.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class SessionMiddleware
4747
/**
4848
* @param string $cookieName
4949
* @param CacheInterface $cache
50-
* @param array $cookieParams
50+
* @param array<int, mixed> $cookieParams
5151
* @param SessionIdInterface|null $sessionId
5252
*/
5353
public function __construct(
@@ -107,6 +107,7 @@ private function fetchSessionFromRequest(ServerRequestInterface $request): Promi
107107
});
108108
} catch (Throwable $et) {
109109
// Do nothing, only a not found will be thrown so generating our own id now
110+
// @ignoreException
110111
}
111112

112113
return resolve(new Session($id, [], $this->sessionId));

tests/InspectableArrayCache.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
final class InspectableArrayCache implements CacheInterface
1111
{
12-
/** @var array */
12+
/** @var array<string, mixed|null> */
1313
private $data = [];
1414

1515
/**
16-
* @return array
16+
* @return array<string, mixed|null>
1717
*/
1818
public function getData(): array
1919
{
@@ -27,7 +27,7 @@ public function getData(): array
2727
*/
2828
public function get($key, $default = null): PromiseInterface
2929
{
30-
if (!isset($this->data[$key])) {
30+
if (!\array_key_exists($key, $this->data)) {
3131
return resolve($default);
3232
}
3333

@@ -67,7 +67,7 @@ public function getMultiple(array $keys, $default = null)
6767
{
6868
$items = [];
6969
foreach ($keys as $key) {
70-
if (isset($this->data[$key])) {
70+
if (!\array_key_exists($key, $this->data)) {
7171
$items[$key] = $this->data[$key];
7272

7373
continue;
@@ -123,6 +123,6 @@ public function clear()
123123
*/
124124
public function has($key)
125125
{
126-
return resolve(isset($this->data[$key]));
126+
return resolve(\array_key_exists($key, $this->data));
127127
}
128128
}

tests/SessionId/RandomBytesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testGenerate(int $size): void
2828
for ($i = 0; $i < 15; $i++) {
2929
$id = $randomBytes->generate();
3030
/** @var string $id */
31-
$id = \hex2bin($id);
31+
$id = \Safe\hex2bin($id);
3232
self::assertSame($size, \strlen($id));
3333
}
3434
}

tests/SessionMiddlewareTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ function () {
405405

406406
return [
407407
$t,
408-
\sprintf(
408+
\Safe\sprintf(
409409
'; expires=%s',
410410
\gmdate('D, d-M-Y H:i:s T', \time() + $t)
411411
),
@@ -419,7 +419,7 @@ function () {
419419

420420
return [
421421
$t,
422-
\sprintf(
422+
\Safe\sprintf(
423423
'; expires=%s',
424424
\gmdate('D, d-M-Y H:i:s T', \time() + $t)
425425
),
@@ -433,7 +433,7 @@ function () {
433433

434434
return [
435435
$t,
436-
\sprintf(
436+
\Safe\sprintf(
437437
'; expires=%s',
438438
\gmdate('D, d-M-Y H:i:s T', \time() + $t)
439439
),

tests/SessionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ public function provideSessionArrayWithMissingItems(): iterable
177177

178178
/**
179179
* @dataProvider provideSessionArrayWithMissingItems
180+
*
181+
* @param array<string, string> $session
180182
*/
181183
public function testFromArrayThrowsOnMissingElements(array $session): void
182184
{

0 commit comments

Comments
 (0)