Skip to content

Commit 6bfc544

Browse files
jmikolafranmomu
andauthored
PHPLIB-766: Fix deprecation notices for PHP 8.1 (#874)
* Avoid calling strlen() on non-strings * Add return types to Iterator implementation * Make parameters required in tests Those arguments are always passed by the data providers. * Avoid passing objects to key() * Add missing ReturnTypeWillChange attribute to Iterator classes These were likely missed in 27f6648 Co-authored-by: Fran Moreno <[email protected]>
1 parent a2f3937 commit 6bfc544

18 files changed

+27
-23
lines changed

src/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ class Collection
140140
*/
141141
public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [])
142142
{
143-
if (strlen($databaseName) < 1) {
143+
if (strlen((string) $databaseName) < 1) {
144144
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
145145
}
146146

147-
if (strlen($collectionName) < 1) {
147+
if (strlen((string) $collectionName) < 1) {
148148
throw new InvalidArgumentException('$collectionName is invalid: ' . $collectionName);
149149
}
150150

src/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class Database
114114
*/
115115
public function __construct(Manager $manager, $databaseName, array $options = [])
116116
{
117-
if (strlen($databaseName) < 1) {
117+
if (strlen((string) $databaseName) < 1) {
118118
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
119119
}
120120

src/Model/CollectionInfoCommandIterator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace MongoDB\Model;
1919

2020
use IteratorIterator;
21+
use ReturnTypeWillChange;
2122
use Traversable;
2223

2324
/**
@@ -53,6 +54,7 @@ public function __construct(Traversable $iterator, $databaseName = null)
5354
* @see http://php.net/iterator.current
5455
* @return CollectionInfo
5556
*/
57+
#[ReturnTypeWillChange]
5658
public function current()
5759
{
5860
$info = parent::current();

src/Model/IndexInfoIteratorIterator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace MongoDB\Model;
1919

2020
use IteratorIterator;
21+
use ReturnTypeWillChange;
2122
use Traversable;
2223

2324
use function array_key_exists;
@@ -57,6 +58,7 @@ public function __construct(Traversable $iterator, $ns = null)
5758
* @see http://php.net/iterator.current
5859
* @return IndexInfo
5960
*/
61+
#[ReturnTypeWillChange]
6062
public function current()
6163
{
6264
$info = parent::current();

tests/FunctionalTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function configureFailPoint($command, ?Server $server = null): void
267267
throw new InvalidArgumentException('$command is not an array or stdClass instance');
268268
}
269269

270-
if (key($command) !== 'configureFailPoint') {
270+
if (key((array) $command) !== 'configureFailPoint') {
271271
throw new InvalidArgumentException('$command is not a configureFailPoint command');
272272
}
273273

tests/Model/BSONIteratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class BSONIteratorTest extends TestCase
1717
/**
1818
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
1919
*/
20-
public function testValidValues(?array $typeMap = null, $binaryString, array $expectedDocuments): void
20+
public function testValidValues(?array $typeMap, $binaryString, array $expectedDocuments): void
2121
{
2222
$bsonIt = new BSONIterator($binaryString, ['typeMap' => $typeMap]);
2323

tests/Model/CachingIteratorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function testWithWrongIterator(): void
122122
/** @var int */
123123
private $i = 0;
124124

125-
public function current()
125+
public function current(): int
126126
{
127127
return $this->i;
128128
}
@@ -132,12 +132,12 @@ public function next(): void
132132
$this->i++;
133133
}
134134

135-
public function key()
135+
public function key(): int
136136
{
137137
return $this->i;
138138
}
139139

140-
public function valid()
140+
public function valid(): bool
141141
{
142142
return $this->i == 0;
143143
}

tests/Operation/AggregateFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function (array $event): void {
170170
/**
171171
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
172172
*/
173-
public function testTypeMapOption(?array $typeMap = null, array $expectedDocuments): void
173+
public function testTypeMapOption(?array $typeMap, array $expectedDocuments): void
174174
{
175175
$this->createFixtures(3);
176176

@@ -185,7 +185,7 @@ public function testTypeMapOption(?array $typeMap = null, array $expectedDocumen
185185
/**
186186
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
187187
*/
188-
public function testTypeMapOptionWithoutCursor(?array $typeMap = null, array $expectedDocuments): void
188+
public function testTypeMapOptionWithoutCursor(?array $typeMap, array $expectedDocuments): void
189189
{
190190
if (version_compare($this->getServerVersion(), '3.6.0', '>=')) {
191191
$this->markTestSkipped('Aggregations with useCursor == false are not supported');

tests/Operation/FindAndModifyFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function (array $event): void {
162162
/**
163163
* @dataProvider provideTypeMapOptionsAndExpectedDocument
164164
*/
165-
public function testTypeMapOption(?array $typeMap = null, $expectedDocument): void
165+
public function testTypeMapOption(?array $typeMap, $expectedDocument): void
166166
{
167167
$this->createFixtures(1);
168168

tests/Operation/MapReduceFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ function (array $event): void {
235235
/**
236236
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
237237
*/
238-
public function testTypeMapOptionWithInlineResults(?array $typeMap = null, array $expectedDocuments): void
238+
public function testTypeMapOptionWithInlineResults(?array $typeMap, array $expectedDocuments): void
239239
{
240240
$this->createFixtures(3);
241241

@@ -282,7 +282,7 @@ public function provideTypeMapOptionsAndExpectedDocuments()
282282
/**
283283
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
284284
*/
285-
public function testTypeMapOptionWithOutputCollection(?array $typeMap = null, array $expectedDocuments): void
285+
public function testTypeMapOptionWithOutputCollection(?array $typeMap, array $expectedDocuments): void
286286
{
287287
$this->createFixtures(3);
288288

0 commit comments

Comments
 (0)