Skip to content

Commit 530d881

Browse files
authored
Merge pull request #42 from ingenerator/1.x-support-php-81
Support PHP8.1
2 parents e79ee97 + cf1d9e6 commit 530d881

File tree

8 files changed

+59
-50
lines changed

8 files changed

+59
-50
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ jobs:
1717
fail-fast: false
1818
matrix:
1919
php_version:
20-
- '7.4'
2120
- '8.0'
21+
- '8.1'
2222
dependencies:
2323
- 'default'
2424
include:
25-
- php_version: '7.4'
26-
dependencies: 'lowest'
2725
- php_version: '8.0'
2826
dependencies: 'lowest'
27+
- php_version: '8.1'
28+
dependencies: 'lowest'
2929
steps:
3030
- name: Setup PHP
3131
uses: shivammathur/setup-php@v2

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
### Unreleased
22

3+
### v1.16.0 (2022-10-10)
4+
5+
* Support PHP 8.1
6+
* Drop support for PHP 7.4
7+
38
### v1.15.0 (2021-11-24)
49

510
* Optionally report the count of log entries to a MetricsAgent- see `StackdriverApplicationLogger::withMetrics()`

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
],
1717
"require": {
1818
"ext-json": "*",
19-
"ext-mbstring": "^7 || ~8.0.0",
20-
"ext-pdo": "^7 || ~8.0.0",
21-
"ext-sodium": "^7.4 || ~8.0.0",
22-
"php": "^7.4 || ~8.0.0",
19+
"ext-mbstring": "~8.0.0 || ~8.1.0",
20+
"ext-pdo": "~8.0.0 || ~8.1.0",
21+
"ext-sodium": "~8.0.0 || ~8.1.0",
22+
"php": "~8.0.0 || ~8.1.0",
2323
"psr/log": "^1.1"
2424
},
2525
"require-dev": {
2626
"johnkary/phpunit-speedtrap": "^3.3",
27-
"mikey179/vfsstream": "^1.6.8",
28-
"phpunit/phpunit": "^9.5"
27+
"mikey179/vfsstream": "^1.6.10",
28+
"phpunit/phpunit": "^9.5.5"
2929
},
3030
"support": {
3131
"source": "https://github.com/ingenerator/php-utils",

src/DateTime/InvalidUserDateTime.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ public function __construct($input_string)
3030
$this->input_string = $input_string;
3131
}
3232

33-
public static function createFromFormat($format, $time, $timezone = NULL)
33+
public static function createFromFormat($format, $time, $timezone = NULL): \DateTimeImmutable
3434
{
3535
throw new \BadMethodCallException('Invalid call to '.__METHOD__);
3636
}
3737

38-
public static function createFromMutable($dateTime)
38+
public static function createFromMutable($dateTime): \DateTimeImmutable
3939
{
4040
throw new \BadMethodCallException('Invalid call to '.__METHOD__);
4141
}
4242

43-
public static function getLastErrors()
43+
public static function getLastErrors(): array|false
4444
{
4545
throw new \BadMethodCallException('Invalid call to '.__METHOD__);
4646
}
4747

48-
public static function __set_state(array $array)
48+
public static function __set_state(array $array): \DateTimeImmutable
4949
{
5050
throw new \BadMethodCallException('Invalid call to '.__METHOD__);
5151
}
5252

53-
public function format($format)
53+
public function format($format): string
5454
{
5555
return $this->input_string;
5656
}
@@ -60,7 +60,7 @@ public function __toString()
6060
return $this->input_string;
6161
}
6262

63-
public function add($interval)
63+
public function add($interval): \DateTimeImmutable
6464
{
6565
throw new \RuntimeException(
6666
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
@@ -78,84 +78,84 @@ public function add($interval)
7878
* @return static
7979
* Returns the {@link http://www.php.net/manual/en/class.datetimeimmutable.php DateTimeImmutable} object for method chaining or <b>FALSE</b> on failure.
8080
*/
81-
public function modify($modify)
81+
public function modify($modify): \DateTimeImmutable
8282
{
8383
throw new \RuntimeException(
8484
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
8585
);
8686
}
8787

88-
public function setDate($year, $month, $day)
88+
public function setDate($year, $month, $day): \DateTimeImmutable
8989
{
9090
throw new \RuntimeException(
9191
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
9292
);
9393
}
9494

95-
public function setISODate($year, $week, $day = 1)
95+
public function setISODate($year, $week, $day = 1): \DateTimeImmutable
9696
{
9797
throw new \RuntimeException(
9898
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
9999
);
100100
}
101101

102-
public function setTime($hour, $minute, $second = 0, $microseconds = 0)
102+
public function setTime($hour, $minute, $second = 0, $microseconds = 0): \DateTimeImmutable
103103
{
104104
throw new \RuntimeException(
105105
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
106106
);
107107
}
108108

109-
public function setTimestamp($unixtimestamp)
109+
public function setTimestamp($unixtimestamp): \DateTimeImmutable
110110
{
111111
throw new \RuntimeException(
112112
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
113113
);
114114
}
115115

116-
public function setTimezone($timezone)
116+
public function setTimezone($timezone): \DateTimeImmutable
117117
{
118118
throw new \RuntimeException(
119119
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
120120
);
121121
}
122122

123-
public function sub($interval)
123+
public function sub($interval): \DateTimeImmutable
124124
{
125125
throw new \RuntimeException(
126126
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
127127
);
128128
}
129129

130-
public function diff($datetime2, $absolute = FALSE)
130+
public function diff($datetime2, $absolute = FALSE): \DateInterval
131131
{
132132
throw new \RuntimeException(
133133
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
134134
);
135135
}
136136

137-
public function getOffset()
137+
public function getOffset(): int
138138
{
139139
throw new \RuntimeException(
140140
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
141141
);
142142
}
143143

144-
public function getTimestamp()
144+
public function getTimestamp(): int
145145
{
146146
throw new \RuntimeException(
147147
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
148148
);
149149
}
150150

151-
public function getTimezone()
151+
public function getTimezone(): \DateTimeZone|false
152152
{
153153
throw new \RuntimeException(
154154
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'
155155
);
156156
}
157157

158-
public function __wakeup()
158+
public function __wakeup(): void
159159
{
160160
throw new \RuntimeException(
161161
'Cannot '.__METHOD__.' on invalid user date/time `'.$this->input_string.'`'

src/Session/MysqlSession.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function __construct(PDO $db, $hash_salt, $lock_timeout = 20)
7272
/**
7373
* @return void
7474
*/
75-
public function initialise()
75+
public function initialise(): void
7676
{
7777
// strict mode is required both for general security, and to enable the create_sid and
7878
// validateId methods in this handler interface, which won't otherwise be called.
@@ -83,15 +83,15 @@ public function initialise()
8383
/**
8484
* {@inheritdoc}
8585
*/
86-
public function close()
86+
public function close(): bool
8787
{
8888
return $this->releaseLock();
8989
}
9090

9191
/**
9292
* {@inheritdoc}
9393
*/
94-
public function destroy($session_id)
94+
public function destroy($session_id): bool
9595
{
9696
return $this->db->prepare("DELETE FROM `sessions` WHERE `id` = :id")
9797
->execute(['id' => $session_id]);
@@ -102,15 +102,15 @@ public function destroy($session_id)
102102
*
103103
* @return int
104104
*/
105-
public function garbageCollect()
105+
public function garbageCollect(): bool|int
106106
{
107107
return $this->gc($this->session_lifetime);
108108
}
109109

110110
/**
111111
* {@inheritdoc}
112112
*/
113-
public function gc($maxlifetime)
113+
public function gc($maxlifetime): int|false
114114
{
115115
$now = new \DateTimeImmutable();
116116
$gc = $this->db->prepare("DELETE FROM `sessions` WHERE `last_active` < :expire");
@@ -124,7 +124,7 @@ public function gc($maxlifetime)
124124
/**
125125
* {@inheritdoc}
126126
*/
127-
public function open($save_path, $name)
127+
public function open($save_path, $name): bool
128128
{
129129
return TRUE;
130130
}
@@ -134,7 +134,7 @@ public function open($save_path, $name)
134134
*
135135
* {@inheritDoc}
136136
*/
137-
public function create_sid()
137+
public function create_sid(): string
138138
{
139139
$session_id = \session_create_id();
140140

@@ -183,7 +183,7 @@ public function create_sid()
183183
/**
184184
* {@inheritDoc}
185185
*/
186-
public function validateId($session_id)
186+
public function validateId($session_id): bool
187187
{
188188
$now = new \DateTimeImmutable();
189189
$this->getLock($session_id);
@@ -232,7 +232,7 @@ public function validateId($session_id)
232232
/**
233233
* {@inheritdoc}
234234
*/
235-
public function read($session_id)
235+
public function read($session_id): string|false
236236
{
237237
if ( ! isset($this->data_cache[$session_id])) {
238238
// This cannot realistically ever happen in real life. PHP will *always* have called
@@ -258,7 +258,7 @@ public function read($session_id)
258258
*
259259
* {@inheritdoc}
260260
*/
261-
public function write($session_id, $session_data)
261+
public function write($session_id, $session_data): bool
262262
{
263263
// There is no reason to update user_agent or hash
264264
// If user_agent has changed the hash will have changed
@@ -282,7 +282,7 @@ public function write($session_id, $session_data)
282282
/**
283283
* Called to mark a session still active even if the data hasn't changed
284284
*/
285-
public function updateTimestamp($session_id, $session_data)
285+
public function updateTimestamp($session_id, $session_data): bool
286286
{
287287
// There is no reason to update user_agent or hash
288288
// If user_agent has changed the hash will have changed
@@ -305,7 +305,7 @@ public function updateTimestamp($session_id, $session_data)
305305
/**
306306
* @return string
307307
*/
308-
protected function calculateHash()
308+
protected function calculateHash(): string
309309
{
310310
$hash = $this->client_user_agent.$this->hash_salt;
311311

@@ -318,7 +318,7 @@ protected function calculateHash()
318318
* @return bool
319319
* @throws \ErrorException
320320
*/
321-
protected function getLock($session_id)
321+
protected function getLock($session_id): bool
322322
{
323323
$this->session_lock = 'session_'.$session_id;
324324

@@ -336,7 +336,7 @@ protected function getLock($session_id)
336336
/**
337337
* @return bool
338338
*/
339-
protected function releaseLock()
339+
protected function releaseLock(): bool
340340
{
341341
if ( ! $this->session_lock) {
342342
// The lock has already been released e.g. due to validateSid releasing it because

src/StringEncoding/JSON.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
namespace Ingenerator\PHPUtils\StringEncoding;
44

55
use Ingenerator\PHPUtils\StringEncoding\InvalidJSONException;
6+
use function json_last_error_msg;
67

78
class JSON
89
{
910

1011
public static function decode(?string $json)
1112
{
12-
$result = json_decode($json, TRUE);
13+
try {
14+
$result = json_decode($json, TRUE);
15+
} catch (\ErrorException $e){
16+
throw new InvalidJSONException('Invalid JSON: ' . json_last_error_msg());
17+
}
1318
if (json_last_error() !== JSON_ERROR_NONE) {
1419
throw new InvalidJSONException('Invalid JSON: ' . json_last_error_msg());
1520
}

test/unit/Mutex/BasicPDOStatementStub.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace test\unit\Ingenerator\PHPUtils\unit\Mutex;
44

55
use PDO;
6+
use function array_map;
67
use const PHP_MAJOR_VERSION;
78

89
if (PHP_MAJOR_VERSION < 8) {
@@ -12,7 +13,7 @@ class BasicPDOStatementStub extends \PDOStatement
1213

1314
public function __construct(array $result) { $this->result = $result; }
1415

15-
public function fetchAll($fetch_style = NULL, $fetch_argument = NULL, $ctor_args = NULL)
16+
public function fetchAll($fetch_style = NULL, $fetch_argument = NULL, $ctor_args = NULL): array
1617
{
1718
if ($fetch_style === NULL) {
1819
return $this->result;
@@ -37,7 +38,7 @@ class BasicPDOStatementStub extends \PDOStatement
3738

3839
public function __construct(array $result) { $this->result = $result; }
3940

40-
public function fetchAll(int $fetch_style = NULL, mixed ...$fetch_argument)
41+
public function fetchAll(int $fetch_style = NULL, mixed ...$fetch_argument): array
4142
{
4243
if ($fetch_style === NULL) {
4344
return $this->result;

test/unit/Mutex/BasicPDOStub.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace test\unit\Ingenerator\PHPUtils\unit\Mutex;
44

55
use PDO;
6+
use PDOStatement;
67
use PHPUnit\Framework\Assert;
78
use const PHP_MAJOR_VERSION;
89

@@ -59,16 +60,13 @@ public static function withQueryStack(array $stack)
5960

6061
public function __construct() { }
6162

62-
public function quote($string, $type = PDO::PARAM_STR)
63+
public function quote($string, $type = PDO::PARAM_STR): string|false
6364
{
6465
return '{quoted-'.$string.'}';
6566
}
6667

67-
public function query(
68-
string $statement,
69-
?int $fetchMode = PDO::ATTR_DEFAULT_FETCH_MODE,
70-
mixed ...$fetchModeArgs
71-
) {
68+
public function query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, ...$fetch_mode_args): PDOStatement|false
69+
{
7270
Assert::assertNotEmpty($this->query_stack, 'No expectation defined for query '.$statement);
7371
$next_query = array_shift($this->query_stack);
7472
Assert::assertSame($next_query['sql'], $statement);

0 commit comments

Comments
 (0)