Skip to content

Commit 9ce9165

Browse files
author
Jonathon Hill
committed
Level 6 static analysis
1 parent 67d68f6 commit 9ce9165

23 files changed

+254
-136
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 4
2+
level: 6
33
paths:
44
- src
55
- tests

src/CacheControl.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class CacheControl
1010
{
1111
private const EXPIRED = 'Thu, 19 Nov 1981 08:52:00 GMT';
1212

13+
/**
14+
* @return non-empty-array<string, string>
15+
*/
1316
public static function createHeaders(
1417
string $limiter = 'nocache',
1518
int $maxAge = null,

src/Config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ public function getRegenerateIdInterval(): int
303303
return $this->regenerate_id_interval;
304304
}
305305

306+
/**
307+
* @return array<string, mixed>
308+
*/
306309
public function toArray(): array
307310
{
308311
$reflect = new ReflectionClass($this);

src/ExtraConfig.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ class ExtraConfig
88
{
99
protected int $cookie_lifetime = 0;
1010

11-
public function setCookieLifetime(int $cookie_lifetime)
11+
public function setCookieLifetime(int $cookie_lifetime): self
1212
{
1313
$this->cookie_lifetime = $cookie_lifetime;
14+
return $this;
1415
}
1516

1617
public function getCookieLifetime(): int
@@ -20,9 +21,10 @@ public function getCookieLifetime(): int
2021

2122
protected string $cookie_path = '/';
2223

23-
public function setCookiePath(string $cookie_path)
24+
public function setCookiePath(string $cookie_path): self
2425
{
2526
$this->cookie_path = $cookie_path;
27+
return $this;
2628
}
2729

2830
public function getCookiePath(): string
@@ -32,9 +34,10 @@ public function getCookiePath(): string
3234

3335
protected string $cookie_domain = '';
3436

35-
public function setCookieDomain(string $cookie_domain)
37+
public function setCookieDomain(string $cookie_domain): self
3638
{
3739
$this->cookie_domain = $cookie_domain;
40+
return $this;
3841
}
3942

4043
public function getCookieDomain(): string
@@ -44,9 +47,10 @@ public function getCookieDomain(): string
4447

4548
protected bool $cookie_secure = false;
4649

47-
public function setCookieSecure(bool $cookie_secure)
50+
public function setCookieSecure(bool $cookie_secure): self
4851
{
4952
$this->cookie_secure = $cookie_secure;
53+
return $this;
5054
}
5155

5256
public function getCookieSecure(): bool
@@ -56,9 +60,10 @@ public function getCookieSecure(): bool
5660

5761
protected bool $cookie_httponly = false;
5862

59-
public function setCookieHttpOnly(bool $cookie_httponly)
63+
public function setCookieHttpOnly(bool $cookie_httponly): self
6064
{
6165
$this->cookie_httponly = $cookie_httponly;
66+
return $this;
6267
}
6368

6469
public function getCookieHttpOnly(): bool
@@ -68,9 +73,10 @@ public function getCookieHttpOnly(): bool
6873

6974
protected bool $cookie_samesite = false;
7075

71-
public function setCookieSameSite(bool $cookie_samesite)
76+
public function setCookieSameSite(bool $cookie_samesite): self
7277
{
7378
$this->cookie_samesite = $cookie_samesite;
79+
return $this;
7480
}
7581

7682
public function getCookieSameSite(): bool
@@ -80,9 +86,10 @@ public function getCookieSameSite(): bool
8086

8187
protected bool $use_cookies = true;
8288

83-
public function setUseCookies(bool $use_cookies)
89+
public function setUseCookies(bool $use_cookies): self
8490
{
8591
$this->use_cookies = $use_cookies;
92+
return $this;
8693
}
8794

8895
public function getUseCookies(): bool
@@ -92,9 +99,10 @@ public function getUseCookies(): bool
9299

93100
protected bool $use_only_cookies = true;
94101

95-
public function setUseOnlyCookies(bool $use_only_cookies)
102+
public function setUseOnlyCookies(bool $use_only_cookies): self
96103
{
97104
$this->use_only_cookies = $use_only_cookies;
105+
return $this;
98106
}
99107

100108
public function getUseOnlyCookies(): bool
@@ -104,9 +112,10 @@ public function getUseOnlyCookies(): bool
104112

105113
protected string $cache_limiter = 'nocache';
106114

107-
public function setCacheLimiter(string $cache_limiter)
115+
public function setCacheLimiter(string $cache_limiter): self
108116
{
109117
$this->cache_limiter = $cache_limiter;
118+
return $this;
110119
}
111120

112121
public function getCacheLimiter(): string
@@ -116,9 +125,10 @@ public function getCacheLimiter(): string
116125

117126
protected int $cache_expire = 180;
118127

119-
public function setCacheExpire(int $cache_expire)
128+
public function setCacheExpire(int $cache_expire): self
120129
{
121130
$this->cache_expire = $cache_expire;
131+
return $this;
122132
}
123133

124134
public function getCacheExpire(): int

src/Factory.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
namespace Compwright\PhpSession;
66

7+
use InvalidArgumentException;
78
use Psr\SimpleCache\CacheInterface;
89

910
class Factory
1011
{
12+
/**
13+
* @param array<string, mixed> $settings
14+
*/
1115
public function configFromArray(array $settings): Config
1216
{
1317
$config = new Config();
@@ -115,11 +119,22 @@ public function configFromSystem(): Config
115119
return $config;
116120
}
117121

122+
/**
123+
* @param array<string, mixed>|Config|null $arrayOrConfig
124+
*/
118125
public function psr16Session(CacheInterface $store, $arrayOrConfig = null): Manager
119126
{
120-
$config = is_array($arrayOrConfig)
121-
? $this->configFromArray($arrayOrConfig)
122-
: $this->configFromSystem();
127+
if (is_array($arrayOrConfig)) {
128+
$config = $this->configFromArray($arrayOrConfig);
129+
} elseif (is_null($arrayOrConfig)) {
130+
$config = $this->configFromSystem();
131+
} elseif ($arrayOrConfig instanceof Config) {
132+
$config = $arrayOrConfig;
133+
} else {
134+
throw new InvalidArgumentException(
135+
'$arrayOrConfig must be an array, instance of Compwright\PhpSession\Config, or null'
136+
);
137+
}
123138

124139
$handler = new Handlers\Psr16Handler($config, $store);
125140
$config->setSaveHandler($handler);

src/Frameworks/Slim/registerSessionMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Compwright\PhpSession\Middleware\SessionMiddleware;
1111
use Slim\App;
1212

13-
function registerSessionMiddleware(App $app)
13+
function registerSessionMiddleware(App $app): void
1414
{
1515
// Slim middleware is executed in reverse order
1616
$app->add(SessionCacheControlMiddleware::class);

src/Handlers/ArrayHandler.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ class ArrayHandler implements
2929
private SessionId $sid;
3030

3131
/**
32-
* @var array [string $data, array $meta = [string $id, int $last_modified, bool? $destroyed]]
32+
* @var array<string, array{data: mixed, meta: array{id: string, last_modified: float, destroyed?: float}}>
3333
*/
3434
private array $store;
3535

36+
/**
37+
* @param array<string, array{data: mixed, meta: array{id: string, last_modified: float, destroyed?: float}}> $store
38+
*/
3639
public function __construct(Config $config, array $store = [])
3740
{
3841
$this->sid = new SessionId($config);
@@ -65,6 +68,10 @@ public function read($id)
6568
return $this->store[$id]['data'];
6669
}
6770

71+
/**
72+
* @param string $id
73+
* @return array{mixed, float}|false
74+
*/
6875
public function read_cas($id)
6976
{
7077
$data = $this->read($id);
@@ -93,6 +100,11 @@ public function write($id, $data): bool
93100
return true;
94101
}
95102

103+
/**
104+
* @param float $token
105+
* @param string $id
106+
* @param mixed $data
107+
*/
96108
public function write_cas($token, $id, $data): bool
97109
{
98110
if (
@@ -123,11 +135,15 @@ public function updateTimestamp($id, $data): bool
123135
return false;
124136
}
125137

126-
$this->store[$id]['meta']['modified'] = microtime(true);
138+
$this->store[$id]['meta']['last_modified'] = microtime(true);
127139

128140
return true;
129141
}
130142

143+
/**
144+
* @param string $id
145+
* @return float|false
146+
*/
131147
public function getTimestamp($id)
132148
{
133149
if (
@@ -137,7 +153,7 @@ public function getTimestamp($id)
137153
return false;
138154
}
139155

140-
return $this->store[$id]['meta']['modified'];
156+
return $this->store[$id]['meta']['last_modified'];
141157
}
142158

143159
public function destroy($id): bool
@@ -179,7 +195,10 @@ public function count(): int
179195
return count($this->store);
180196
}
181197

182-
public function toArray()
198+
/**
199+
* @return array<string, array{data: mixed, meta: array{id: string, last_modified: float, destroyed?: float}}>
200+
*/
201+
public function toArray(): array
183202
{
184203
return $this->store;
185204
}

src/Handlers/FileHandler.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,15 @@ public function count(): int
123123
return count(glob($this->getFilePath('*')));
124124
}
125125

126+
/**
127+
* @param string $id
128+
* @return float|false
129+
*/
126130
public function getTimestamp($id)
127131
{
128-
return filemtime($this->getFilePath($id));
132+
$timestamp = filemtime($this->getFilePath($id));
133+
return $timestamp === false
134+
? false
135+
: (float) $timestamp;
129136
}
130137
}

src/Handlers/Psr16Handler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public function gc($max_lifetime): bool
9494
return true;
9595
}
9696

97+
/**
98+
* @param string $id
99+
* @return float|false
100+
*/
97101
public function getTimestamp($id)
98102
{
99103
return $this->lastWriteTimestamp ?? false;

src/Handlers/ScrapbookHandler.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public function read($id)
7272
return $this->store->get($id);
7373
}
7474

75+
/**
76+
* @param string $id
77+
* @return array{0: mixed, 1: string}|false
78+
*/
7579
public function read_cas($id)
7680
{
7781
$data = $this->store->get($id);
@@ -94,6 +98,11 @@ public function write($id, $data): bool
9498
return $this->store->set($id, $data, $this->config->getGcMaxLifetime());
9599
}
96100

101+
/**
102+
* @param mixed $token
103+
* @param string $id
104+
* @param mixed $data
105+
*/
97106
public function write_cas($token, $id, $data): bool
98107
{
99108
$this->lastWriteTimestamp = microtime(true);
@@ -121,6 +130,10 @@ public function gc($max_lifetime): bool
121130
return true;
122131
}
123132

133+
/**
134+
* @param string $id
135+
* @return float|false
136+
*/
124137
public function getTimestamp($id)
125138
{
126139
return $this->lastWriteTimestamp ?? false;

0 commit comments

Comments
 (0)