Skip to content

Commit c2c0e90

Browse files
committed
Updated codebase to new style rulesets and php 7.4
2 parents 958e5ac + ad5cddd commit c2c0e90

19 files changed

+221
-120
lines changed

cs-fixer.php.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/Session package.

src/SessionContext.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@
1414

1515
interface SessionContext
1616
{
17+
/**
18+
* Orders to regenerate session id.
19+
*/
1720
public function reset(): void;
1821

22+
/**
23+
* Saves data from session storage.
24+
*
25+
* @param array $data
26+
*/
1927
public function commit(array $data): void;
2028
}

src/SessionContext/NativeSessionContext.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/Session package.
@@ -15,6 +15,7 @@
1515
use Polymorphine\Session\SessionContext;
1616
use Polymorphine\Session\SessionStorageProvider;
1717
use Polymorphine\Session\SessionStorage;
18+
use Polymorphine\Session\SessionStorage\ContextSessionStorage;
1819
use Polymorphine\Headers\Cookie;
1920
use Psr\Http\Server\RequestHandlerInterface;
2021
use Psr\Http\Message\ServerRequestInterface;
@@ -24,13 +25,15 @@
2425

2526
class NativeSessionContext implements MiddlewareInterface, SessionContext, SessionStorageProvider
2627
{
27-
/** @var SessionStorage\ContextSessionStorage */
28-
private $sessionData;
29-
private $cookie;
28+
private ContextSessionStorage $sessionData;
29+
private Cookie $cookie;
3030

31-
private $sessionStarted = false;
32-
private $regenerateId = false;
31+
private bool $sessionStarted = false;
32+
private bool $regenerateId = false;
3333

34+
/**
35+
* @param Cookie $cookie
36+
*/
3437
public function __construct(Cookie $cookie)
3538
{
3639
$this->cookie = $cookie;
@@ -39,7 +42,7 @@ public function __construct(Cookie $cookie)
3942
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
4043
{
4144
if ($this->containsSessionCookie($request)) { $this->start(); }
42-
$this->sessionData = new SessionStorage\ContextSessionStorage($this, $_SESSION ?? []);
45+
$this->sessionData = new ContextSessionStorage($this, $_SESSION ?? []);
4346

4447
$response = $handler->handle($request);
4548
$this->sessionData->commit();
@@ -49,7 +52,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4952

5053
public function storage(): SessionStorage
5154
{
52-
if (!$this->sessionData) {
55+
if (!isset($this->sessionData)) {
5356
throw new RuntimeException('Session context not started');
5457
}
5558
return $this->sessionData;

src/SessionStorage.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,48 @@ interface SessionStorage
1616
{
1717
public const USER_KEY = 'session.user.id';
1818

19-
public function userId();
20-
21-
public function newUserContext($userId = null): void;
22-
19+
/**
20+
* By default an alias to SessionStorage::get('session.user.id').
21+
*
22+
* @return mixed User id stored within session data
23+
*/
24+
public function userId(): ?string;
25+
26+
/**
27+
* By default an alias to SessionStorage::set('session.user.id', $value).
28+
*
29+
* @param string|null $userId
30+
*/
31+
public function newUserContext(string $userId = null): void;
32+
33+
/**
34+
* @param string $key
35+
*
36+
* @return bool
37+
*/
2338
public function has(string $key): bool;
2439

40+
/**
41+
* @param string $key
42+
* @param null $default
43+
*
44+
* @return mixed
45+
*/
2546
public function get(string $key, $default = null);
2647

48+
/**
49+
* @param string $key
50+
* @param $value
51+
*/
2752
public function set(string $key, $value): void;
2853

54+
/**
55+
* @param string $key
56+
*/
2957
public function remove(string $key): void;
3058

59+
/**
60+
* Removes all data stored in session.
61+
*/
3162
public function clear(): void;
3263
}

src/SessionStorage/ContextSessionStorage.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/Session package.
@@ -18,24 +18,28 @@
1818

1919
class ContextSessionStorage implements SessionStorage
2020
{
21-
private $context;
22-
private $userId;
23-
private $data;
21+
private SessionContext $context;
22+
private array $data;
23+
private ?string $userId;
2424

25+
/**
26+
* @param SessionContext $context
27+
* @param array $data
28+
*/
2529
public function __construct(SessionContext $context, array $data = [])
2630
{
2731
$this->context = $context;
2832
$this->userId = $this->pullUserId($data);
2933
$this->data = $data;
3034
}
3135

32-
public function newUserContext($userId = null): void
36+
public function newUserContext(string $userId = null): void
3337
{
3438
$this->userId = $userId;
3539
$this->context->reset();
3640
}
3741

38-
public function userId()
42+
public function userId(): ?string
3943
{
4044
return $this->userId;
4145
}
@@ -70,6 +74,9 @@ public function clear(): void
7074
$this->newUserContext();
7175
}
7276

77+
/**
78+
* Orders to pass stored data to session context.
79+
*/
7380
public function commit(): void
7481
{
7582
$userId = $this->userId ? [self::USER_KEY => $this->userId] : [];

src/SessionStorage/LazySessionStorage.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/Session package.
@@ -17,15 +17,18 @@
1717

1818
class LazySessionStorage implements SessionStorage
1919
{
20-
private $storage;
21-
private $provider;
20+
private SessionStorageProvider $provider;
21+
private SessionStorage $storage;
2222

23+
/**
24+
* @param SessionStorageProvider $provider
25+
*/
2326
public function __construct(SessionStorageProvider $provider)
2427
{
2528
$this->provider = $provider;
2629
}
2730

28-
public function userId()
31+
public function userId(): ?string
2932
{
3033
return $this->storage()->userId();
3134
}
@@ -62,6 +65,6 @@ public function clear(): void
6265

6366
private function storage(): SessionStorage
6467
{
65-
return $this->storage ?: $this->storage = $this->provider->storage();
68+
return $this->storage ??= $this->provider->storage();
6669
}
6770
}

src/SessionStorageProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414

1515
interface SessionStorageProvider
1616
{
17+
/**
18+
* @return SessionStorage Interface specialized to access & modify session data
19+
*/
1720
public function storage(): SessionStorage;
1821
}

tests/Doubles/DummyResponse.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/Session package.
@@ -17,59 +17,75 @@
1717

1818
class DummyResponse implements ResponseInterface
1919
{
20-
public function getProtocolVersion()
20+
public ?StreamInterface $stream;
21+
22+
public function getProtocolVersion(): string
2123
{
24+
return '1.1';
2225
}
2326

24-
public function withProtocolVersion($version)
27+
public function withProtocolVersion($version): self
2528
{
29+
return $this;
2630
}
2731

28-
public function getHeaders()
32+
public function getHeaders(): array
2933
{
34+
return [];
3035
}
3136

32-
public function hasHeader($name)
37+
public function hasHeader($name): bool
3338
{
39+
return false;
3440
}
3541

36-
public function getHeader($name)
42+
public function getHeader($name): array
3743
{
44+
return [];
3845
}
3946

40-
public function getHeaderLine($name)
47+
public function getHeaderLine($name): string
4148
{
49+
return 'Header: data';
4250
}
4351

44-
public function withHeader($name, $value)
52+
public function withHeader($name, $value): self
4553
{
54+
return $this;
4655
}
4756

48-
public function withAddedHeader($name, $value)
57+
public function withAddedHeader($name, $value): self
4958
{
59+
return $this;
5060
}
5161

52-
public function withoutHeader($name)
62+
public function withoutHeader($name): self
5363
{
64+
return $this;
5465
}
5566

56-
public function getBody()
67+
public function getBody(): StreamInterface
5768
{
69+
return $this->stream;
5870
}
5971

60-
public function withBody(StreamInterface $body)
72+
public function withBody(StreamInterface $body): self
6173
{
74+
return $this;
6275
}
6376

64-
public function getStatusCode()
77+
public function getStatusCode(): int
6578
{
79+
return 200;
6680
}
6781

68-
public function withStatus($code, $reasonPhrase = '')
82+
public function withStatus($code, $reasonPhrase = ''): self
6983
{
84+
return $this;
7085
}
7186

7287
public function getReasonPhrase()
7388
{
89+
return $this;
7490
}
7591
}

tests/Doubles/FakeRequestHandler.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of Polymorphine/Session package.
@@ -18,16 +18,16 @@
1818

1919
class FakeRequestHandler implements RequestHandlerInterface
2020
{
21-
private $sideEffect;
21+
private $process;
2222

23-
public function __construct(callable $sideEffect = null)
23+
public function __construct(callable $process = null)
2424
{
25-
$this->sideEffect = $sideEffect;
25+
$this->process = $process;
2626
}
2727

2828
public function handle(ServerRequestInterface $request): ResponseInterface
2929
{
30-
if ($this->sideEffect) { ($this->sideEffect)(); }
30+
if ($this->process) { ($this->process)(); }
3131
return new DummyResponse();
3232
}
3333
}

0 commit comments

Comments
 (0)