Skip to content

Update code to be compatible with react/http 1.0 #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"hansott/psr7-cookies": "^2.0 || ^1.0",
"psr/http-message": "^1.0",
"react/cache": "^0.5.0 || ^0.6.0 || ^1.0.0",
"react/http": "^1.0.0",
"react/http": "^1.0 || ^0.8",
"react/promise": "^2.7"
},
"require-dev": {
"thecodingmachine/safe": "^0.1 || ^1.0",
"wyrihaximus/async-test-utilities": "^1.1"
},
"config": {
Expand Down
33 changes: 22 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ parameters:
- '#Constructor in [a-zA-Z0-9\\_]+ has parameter \$[a-zA-Z0-9_]+ with default value.#'
- '#Method [a-zA-Z0-9\\_]+::[a-zA-Z0-9_]+\(\) has parameter \$[a-zA-Z0-9_]+ with a nullable type declaration.#'
- '#Method [a-zA-Z0-9\\_]+::[a-zA-Z0-9_]+\(\) has parameter \$[a-zA-Z0-9_]+ with null as default value.#'
- '#Array \(array<string, mixed>\) does not accept key int|string.#'
- '#In method \"WyriHaximus\\React\\Http\\Middleware\\SessionMiddleware::fetchSessionFromRequest\", caught \"Throwable\" must be rethrown.#'
classesAllowedToBeExtended:
- ApiClients\Tools\TestUtilities\TestCase

includes:
- vendor/wyrihaximus/async-test-utilities/rules.neon
- vendor/wyrihaximus/async-test-utilities/rules.neon
19 changes: 11 additions & 8 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class Session
private $id;

/**
* @var array
* @var array<string, mixed>
*/
private $contents;

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

/**
* @param string $id
* @param array $contents
* @param SessionIdInterface $sessionId
* @param string $id
* @param array<string, mixed> $contents
* @param SessionIdInterface $sessionId
*/
public function __construct(string $id, array $contents, SessionIdInterface $sessionId)
{
Expand All @@ -54,15 +54,15 @@ public function getId(): string
}

/**
* @param array $contents
* @param array<string, mixed> $contents
*/
public function setContents(array $contents): void
{
$this->contents = $contents;
}

/**
* @return array
* @return array<string, mixed>
*/
public function getContents(): array
{
Expand Down Expand Up @@ -123,6 +123,9 @@ public function regenerate(): bool
return true;
}

/**
* @return array<string, mixed>
*/
public function toArray(): array
{
return [
Expand All @@ -134,14 +137,14 @@ public function toArray(): array
}

/**
* @param array $session
* @param array<string, mixed> $session
* @param bool $clone
* @throws \InvalidArgumentException
* @return Session
*/
public function fromArray(array $session, bool $clone = true): self
{
if (!isset($session['id']) || !isset($session['contents']) || !isset($session['oldIds']) || !isset($session['status'])) {
if (!\array_key_exists('id', $session) || !\array_key_exists('contents', $session) || !\array_key_exists('oldIds', $session) || !\array_key_exists('status', $session)) {
throw new \InvalidArgumentException('Session array most contain "id", "contents", "oldIds", and "status".');
}

Expand Down
3 changes: 2 additions & 1 deletion src/SessionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class SessionMiddleware
/**
* @param string $cookieName
* @param CacheInterface $cache
* @param array $cookieParams
* @param array<int, mixed> $cookieParams
* @param SessionIdInterface|null $sessionId
*/
public function __construct(
Expand Down Expand Up @@ -107,6 +107,7 @@ private function fetchSessionFromRequest(ServerRequestInterface $request): Promi
});
} catch (Throwable $et) {
// Do nothing, only a not found will be thrown so generating our own id now
// @ignoreException
}

return resolve(new Session($id, [], $this->sessionId));
Expand Down
10 changes: 5 additions & 5 deletions tests/InspectableArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

final class InspectableArrayCache implements CacheInterface
{
/** @var array */
/** @var array<string, mixed|null> */
private $data = [];

/**
* @return array
* @return array<string, mixed|null>
*/
public function getData(): array
{
Expand All @@ -27,7 +27,7 @@ public function getData(): array
*/
public function get($key, $default = null): PromiseInterface
{
if (!isset($this->data[$key])) {
if (!\array_key_exists($key, $this->data)) {
return resolve($default);
}

Expand Down Expand Up @@ -67,7 +67,7 @@ public function getMultiple(array $keys, $default = null)
{
$items = [];
foreach ($keys as $key) {
if (isset($this->data[$key])) {
if (!\array_key_exists($key, $this->data)) {
$items[$key] = $this->data[$key];

continue;
Expand Down Expand Up @@ -123,6 +123,6 @@ public function clear()
*/
public function has($key)
{
return resolve(isset($this->data[$key]));
return resolve(\array_key_exists($key, $this->data));
}
}
2 changes: 1 addition & 1 deletion tests/SessionId/RandomBytesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testGenerate(int $size): void
for ($i = 0; $i < 15; $i++) {
$id = $randomBytes->generate();
/** @var string $id */
$id = \hex2bin($id);
$id = \Safe\hex2bin($id);
self::assertSame($size, \strlen($id));
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/SessionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ function () {

return [
$t,
\sprintf(
\Safe\sprintf(
'; expires=%s',
\gmdate('D, d-M-Y H:i:s T', \time() + $t)
),
Expand All @@ -419,7 +419,7 @@ function () {

return [
$t,
\sprintf(
\Safe\sprintf(
'; expires=%s',
\gmdate('D, d-M-Y H:i:s T', \time() + $t)
),
Expand All @@ -433,7 +433,7 @@ function () {

return [
$t,
\sprintf(
\Safe\sprintf(
'; expires=%s',
\gmdate('D, d-M-Y H:i:s T', \time() + $t)
),
Expand Down
2 changes: 2 additions & 0 deletions tests/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ public function provideSessionArrayWithMissingItems(): iterable

/**
* @dataProvider provideSessionArrayWithMissingItems
*
* @param array<string, string> $session
*/
public function testFromArrayThrowsOnMissingElements(array $session): void
{
Expand Down