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

tests/SpecTests/AtlasDataLakeSpecTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static function assertCommandMatches(stdClass $expected, stdClass $actual
5858
* @param string $databaseName Name of database under test
5959
* @param string $collectionName Name of collection under test
6060
*/
61-
public function testAtlasDataLake(stdClass $test, ?array $runOn = null, array $data, ?string $databaseName = null, ?string $collectionName = null): void
61+
public function testAtlasDataLake(stdClass $test, ?array $runOn, array $data, ?string $databaseName = null, ?string $collectionName = null): void
6262
{
6363
if (isset($runOn)) {
6464
$this->checkServerRequirements($runOn);

tests/SpecTests/ClientSideEncryptionSpecTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static function assertCommandMatches(stdClass $expected, stdClass $actual
7979
* @param string $databaseName Name of database under test
8080
* @param string $collectionName Name of collection under test
8181
*/
82-
public function testClientSideEncryption(stdClass $test, ?array $runOn = null, array $data, ?array $keyVaultData = null, $jsonSchema = null, ?string $databaseName = null, ?string $collectionName = null): void
82+
public function testClientSideEncryption(stdClass $test, ?array $runOn, array $data, ?array $keyVaultData = null, $jsonSchema = null, ?string $databaseName = null, ?string $collectionName = null): void
8383
{
8484
if (isset(self::$incompleteTests[$this->dataDescription()])) {
8585
$this->markTestIncomplete(self::$incompleteTests[$this->dataDescription()]);

tests/SpecTests/CommandExpectations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CommandExpectations implements CommandSubscriber
4848
private function __construct(array $events)
4949
{
5050
foreach ($events as $event) {
51-
switch (key($event)) {
51+
switch (key((array) $event)) {
5252
case 'command_failed_event':
5353
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
5454
$this->expectedEvents[] = [$event->command_failed_event, CommandFailedEvent::class];

tests/SpecTests/ReadWriteConcernSpecTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static function assertCommandMatches(stdClass $expected, stdClass $actual
4545
* @param string $databaseName Name of database under test
4646
* @param string $collectionName Name of collection under test
4747
*/
48-
public function testReadWriteConcern(stdClass $test, ?array $runOn = null, array $data, ?string $databaseName = null, ?string $collectionName = null): void
48+
public function testReadWriteConcern(stdClass $test, ?array $runOn, array $data, ?string $databaseName = null, ?string $collectionName = null): void
4949
{
5050
if (isset(self::$incompleteTests[$this->dataDescription()])) {
5151
$this->markTestIncomplete(self::$incompleteTests[$this->dataDescription()]);

tests/SpecTests/RetryableReadsSpecTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function assertCommandMatches(stdClass $expected, stdClass $actual
5353
* @group matrix-testing-exclude-server-4.4-driver-4.2
5454
* @group matrix-testing-exclude-server-5.0-driver-4.2
5555
*/
56-
public function testRetryableReads(stdClass $test, ?array $runOn = null, $data, string $databaseName, ?string $collectionName, ?string $bucketName): void
56+
public function testRetryableReads(stdClass $test, ?array $runOn, $data, string $databaseName, ?string $collectionName, ?string $bucketName): void
5757
{
5858
if (isset($runOn)) {
5959
$this->checkServerRequirements($runOn);

tests/SpecTests/RetryableWritesSpecTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class RetryableWritesSpecTest extends FunctionalTestCase
2424
* @param array $runOn Top-level "runOn" array with server requirements
2525
* @param array $data Top-level "data" array to initialize collection
2626
*/
27-
public function testRetryableWrites(stdClass $test, ?array $runOn = null, array $data): void
27+
public function testRetryableWrites(stdClass $test, ?array $runOn, array $data): void
2828
{
2929
if ($this->isShardedCluster() && ! $this->isShardedClusterUsingReplicasets()) {
3030
$this->markTestSkipped('Transaction numbers are only allowed on a replica set member or mongos (PHPC-1415)');

tests/SpecTests/TransactionsSpecTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static function assertCommandMatches(stdClass $expected, stdClass $actual
114114
* @dataProvider provideTransactionsTests
115115
* @group serverless
116116
*/
117-
public function testTransactions(stdClass $test, ?array $runOn = null, array $data, ?string $databaseName = null, ?string $collectionName = null): void
117+
public function testTransactions(stdClass $test, ?array $runOn, array $data, ?string $databaseName = null, ?string $collectionName = null): void
118118
{
119119
$this->runTransactionTest($test, $runOn, $data, $databaseName, $collectionName);
120120
}
@@ -127,7 +127,7 @@ public function provideTransactionsTests(): array
127127
/**
128128
* @dataProvider provideTransactionsConvenientApiTests
129129
*/
130-
public function testTransactionsConvenientApi(stdClass $test, ?array $runOn = null, array $data, ?string $databaseName = null, ?string $collectionName = null): void
130+
public function testTransactionsConvenientApi(stdClass $test, ?array $runOn, array $data, ?string $databaseName = null, ?string $collectionName = null): void
131131
{
132132
$this->runTransactionTest($test, $runOn, $data, $databaseName, $collectionName);
133133
}
@@ -146,7 +146,7 @@ public function provideTransactionsConvenientApiTests(): array
146146
* @param string $databaseName Name of database under test
147147
* @param string $collectionName Name of collection under test
148148
*/
149-
private function runTransactionTest(stdClass $test, ?array $runOn = null, array $data, ?string $databaseName = null, ?string $collectionName = null): void
149+
private function runTransactionTest(stdClass $test, ?array $runOn, array $data, ?string $databaseName = null, ?string $collectionName = null): void
150150
{
151151
if (isset(self::$incompleteTests[$this->dataDescription()])) {
152152
$this->markTestIncomplete(self::$incompleteTests[$this->dataDescription()]);

tests/UnifiedSpecTests/ExpectedError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ final class ExpectedError
6868
/** @var ExpectedResult|null */
6969
private $expectedResult;
7070

71-
public function __construct(?stdClass $o = null, EntityMap $entityMap)
71+
public function __construct(?stdClass $o, EntityMap $entityMap)
7272
{
7373
if ($o === null) {
7474
return;

0 commit comments

Comments
 (0)