Skip to content

Commit 7150ec1

Browse files
authored
PHP 8.1 Deprecation fixes (#190)
* Apply changes to avoid PHP deprecations These changes are introduced to mitigate deprecations from PHP 8.1 and to avoid the lower boundaries test of PHP 8.1 to fail due to PHPUnit resorting to the use of `$GLOBALS`. These changes are introduced before the requirement updates to allow them to be forward and backward compatible. * Update project requirements This changes makes the project explicitly compatible with PHP 8.1 and enables testing with that version in the CI. Additionally, the configuration and requirements of PHPUnit are updated to be fully compatible with PHP 8.1.
1 parent 2601a5c commit 7150ec1

File tree

8 files changed

+41
-34
lines changed

8 files changed

+41
-34
lines changed

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
fail-fast: true
1313
matrix:
14-
php: [ '7.3', '7.4','8.0' ]
14+
php: [ '7.3', '7.4', '8.0', '8.1' ]
1515
stability: [ prefer-lowest, prefer-stable ]
1616

1717
name: PHP ${{ matrix.php }} - ${{ matrix.stability }}

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
"source": "https://github.com/messagebird/php-rest-api"
1717
},
1818
"require": {
19-
"php": ">=7.3|^8.0",
19+
"php": ">=7.3|~8.0.0|~8.1.0",
2020
"ext-curl": "*",
2121
"ext-json": "*",
2222
"firebase/php-jwt": "^5.4"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "^8.0|^9.0",
25+
"phpunit/phpunit": "^9.5.14",
2626
"vimeo/psalm": "4.18.1"
2727
},
2828
"autoload": {

phpunit.xml.dist

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
11-
bootstrap="autoload.php"
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
colors="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
processIsolation="false"
12+
stopOnFailure="false"
13+
bootstrap="autoload.php"
1214
>
1315
<testsuites>
1416
<testsuite name="php-rest-api">
1517
<directory>./tests/</directory>
1618
</testsuite>
1719
</testsuites>
18-
<filter>
19-
<whitelist>
20+
<coverage>
21+
<include>
2022
<directory suffix=".php">./src/</directory>
21-
</whitelist>
22-
</filter>
23+
</include>
24+
</coverage>
2325
</phpunit>

src/MessageBird/Resources/AvailablePhoneNumbers.php

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public function getList(string $countryCode, array $parameters = [])
6969
*/
7070
private function processRequest(?string $body): Objects\Number
7171
{
72+
if ($body === null) {
73+
throw new Exceptions\ServerException('Got an invalid JSON response from the server.');
74+
}
75+
7276
try {
7377
$body = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
7478
} catch (\JsonException $e) {

src/MessageBird/Resources/Base.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ public function create($object, ?array $query = null)
117117
*/
118118
public function processRequest(?string $body)
119119
{
120-
try {
121-
$body = json_decode($body, null, 512, \JSON_THROW_ON_ERROR);
122-
} catch (\JsonException $e) {
120+
if ($body === null) {
123121
throw new Exceptions\ServerException('Got an invalid JSON response from the server.');
124122
}
125123

126-
if ($body === null) {
124+
try {
125+
$body = json_decode($body, null, 512, \JSON_THROW_ON_ERROR);
126+
} catch (\JsonException $e) {
127127
throw new Exceptions\ServerException('Got an invalid JSON response from the server.');
128128
}
129129

src/MessageBird/Resources/Voice/Base.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ public function getList($parameters = [])
7070
*/
7171
public function processRequest(?string $body)
7272
{
73+
if ($body === null) {
74+
throw new Exceptions\ServerException('Got an invalid JSON response from the server.');
75+
}
76+
7377
try {
7478
$body = json_decode($body, null, 512, \JSON_THROW_ON_ERROR);
7579
} catch (\JsonException $e) {
7680
throw new Exceptions\ServerException('Got an invalid JSON response from the server.');
7781

7882
}
7983

80-
if ($body === null) {
81-
throw new Exceptions\ServerException('Got an invalid JSON response from the server.');
82-
}
83-
8484
if (empty($body->errors)) {
8585
return $this->object->loadFromArray($body->data[0]);
8686
}

src/MessageBird/Resources/Voice/Legs.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ public function getList(string $callId, array $parameters = [])
9292
*/
9393
public function processRequest(?string $body): Objects\Voice\Leg
9494
{
95-
try {
96-
$body = json_decode($body, null, 512, \JSON_THROW_ON_ERROR);
97-
} catch (\JsonException $e) {
95+
if ($body === null) {
9896
throw new Exceptions\ServerException('Got an invalid JSON response from the server.');
9997
}
10098

101-
if ($body === null) {
99+
try {
100+
$body = json_decode($body, null, 512, \JSON_THROW_ON_ERROR);
101+
} catch (\JsonException $e) {
102102
throw new Exceptions\ServerException('Got an invalid JSON response from the server.');
103103
}
104104

tests/Unit/RequestValidatorTest.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -173,27 +173,28 @@ public function __construct($array)
173173
$this->array = $array;
174174
}
175175

176-
function rewind()
176+
function rewind(): void
177177
{
178-
return reset($this->array);
178+
reset($this->array);
179179
}
180180

181-
function current()
181+
function current(): array
182182
{
183183
return [current($this->array)];
184184
}
185185

186+
#[\ReturnTypeWillChange]
186187
function key()
187188
{
188189
return current($this->array)['name'];
189190
}
190191

191-
function next()
192+
function next(): void
192193
{
193-
return [next($this->array)];
194+
next($this->array);
194195
}
195196

196-
function valid()
197+
function valid(): bool
197198
{
198199
return key($this->array) !== null;
199200
}

0 commit comments

Comments
 (0)