Skip to content

Commit 7e63041

Browse files
authored
PHPLIB-1123: Use array_is_list (#1093)
1 parent 7e9df62 commit 7e63041

14 files changed

+39
-53
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"ext-mongodb": "^1.16.0",
1616
"jean85/pretty-package-versions": "^2.0.1",
1717
"symfony/polyfill-php73": "^1.27",
18-
"symfony/polyfill-php80": "^1.27"
18+
"symfony/polyfill-php80": "^1.27",
19+
"symfony/polyfill-php81": "^1.27"
1920
},
2021
"require-dev": {
2122
"doctrine/coding-standard": "^11.1",

psalm-baseline.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,13 @@
417417
</DocblockTypeContradiction>
418418
</file>
419419
<file src="src/Operation/CreateCollection.php">
420-
<MixedArgument occurrences="3">
421-
<code>$i</code>
420+
<MixedArgument occurrences="2">
422421
<code>$i</code>
423422
<code>$this-&gt;options['typeMap']</code>
424423
</MixedArgument>
425-
<MixedAssignment occurrences="3">
424+
<MixedAssignment occurrences="4">
426425
<code>$cmd[$option]</code>
426+
<code>$i</code>
427427
<code>$options['session']</code>
428428
<code>$options['writeConcern']</code>
429429
</MixedAssignment>

src/Operation/Aggregate.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use MongoDB\Exception\UnsupportedException;
3232
use stdClass;
3333

34+
use function array_is_list;
3435
use function current;
3536
use function is_array;
3637
use function is_bool;
@@ -137,18 +138,14 @@ class Aggregate implements Executable, Explainable
137138
*/
138139
public function __construct(string $databaseName, ?string $collectionName, array $pipeline, array $options = [])
139140
{
140-
$expectedIndex = 0;
141+
if (! array_is_list($pipeline)) {
142+
throw new InvalidArgumentException('$pipeline is not a list');
143+
}
141144

142145
foreach ($pipeline as $i => $operation) {
143-
if ($i !== $expectedIndex) {
144-
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
145-
}
146-
147146
if (! is_array($operation) && ! is_object($operation)) {
148147
throw InvalidArgumentException::invalidType(sprintf('$pipeline[%d]', $i), $operation, 'array or object');
149148
}
150-
151-
$expectedIndex += 1;
152149
}
153150

154151
$options += ['useCursor' => true];

src/Operation/BulkWrite.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use MongoDB\Exception\InvalidArgumentException;
2727
use MongoDB\Exception\UnsupportedException;
2828

29+
use function array_is_list;
2930
use function array_key_exists;
3031
use function count;
3132
use function current;
@@ -132,13 +133,11 @@ public function __construct(string $databaseName, string $collectionName, array
132133
throw new InvalidArgumentException('$operations is empty');
133134
}
134135

135-
$expectedIndex = 0;
136+
if (! array_is_list($operations)) {
137+
throw new InvalidArgumentException('$operations is not a list');
138+
}
136139

137140
foreach ($operations as $i => $operation) {
138-
if ($i !== $expectedIndex) {
139-
throw new InvalidArgumentException(sprintf('$operations is not a list (unexpected index: "%s")', $i));
140-
}
141-
142141
if (! is_array($operation)) {
143142
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]', $i), $operation, 'array');
144143
}
@@ -271,8 +270,6 @@ public function __construct(string $databaseName, string $collectionName, array
271270
default:
272271
throw new InvalidArgumentException(sprintf('Unknown operation type "%s" in $operations[%d]', $type, $i));
273272
}
274-
275-
$expectedIndex += 1;
276273
}
277274

278275
$options += ['ordered' => true];

src/Operation/CreateCollection.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use MongoDB\Driver\WriteConcern;
2525
use MongoDB\Exception\InvalidArgumentException;
2626

27+
use function array_is_list;
28+
use function assert;
2729
use function current;
2830
use function is_array;
2931
use function is_bool;
@@ -241,18 +243,16 @@ public function __construct(string $databaseName, string $collectionName, array
241243
}
242244

243245
if (isset($options['pipeline'])) {
244-
$expectedIndex = 0;
246+
$pipeline = $options['pipeline'];
247+
assert(is_array($pipeline));
248+
if (! array_is_list($pipeline)) {
249+
throw new InvalidArgumentException('The "pipeline" option is not a list');
250+
}
245251

246252
foreach ($options['pipeline'] as $i => $operation) {
247-
if ($i !== $expectedIndex) {
248-
throw new InvalidArgumentException(sprintf('The "pipeline" option is not a list (unexpected index: "%s")', $i));
249-
}
250-
251253
if (! is_array($operation) && ! is_object($operation)) {
252254
throw InvalidArgumentException::invalidType(sprintf('$options["pipeline"][%d]', $i), $operation, 'array or object');
253255
}
254-
255-
$expectedIndex += 1;
256256
}
257257
}
258258

src/Operation/CreateIndexes.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use MongoDB\Exception\UnsupportedException;
2727
use MongoDB\Model\IndexInput;
2828

29+
use function array_is_list;
2930
use function array_map;
3031
use function is_array;
3132
use function is_integer;
@@ -88,20 +89,16 @@ public function __construct(string $databaseName, string $collectionName, array
8889
throw new InvalidArgumentException('$indexes is empty');
8990
}
9091

91-
$expectedIndex = 0;
92+
if (! array_is_list($indexes)) {
93+
throw new InvalidArgumentException('$indexes is not a list');
94+
}
9295

9396
foreach ($indexes as $i => $index) {
94-
if ($i !== $expectedIndex) {
95-
throw new InvalidArgumentException(sprintf('$indexes is not a list (unexpected index: "%s")', $i));
96-
}
97-
9897
if (! is_array($index)) {
9998
throw InvalidArgumentException::invalidType(sprintf('$index[%d]', $i), $index, 'array');
10099
}
101100

102101
$this->indexes[] = new IndexInput($index);
103-
104-
$expectedIndex += 1;
105102
}
106103

107104
if (isset($options['commitQuorum']) && ! is_string($options['commitQuorum']) && ! is_integer($options['commitQuorum'])) {

src/Operation/InsertMany.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use MongoDB\Exception\UnsupportedException;
2727
use MongoDB\InsertManyResult;
2828

29+
use function array_is_list;
2930
use function is_array;
3031
use function is_bool;
3132
use function is_object;
@@ -84,18 +85,14 @@ public function __construct(string $databaseName, string $collectionName, array
8485
throw new InvalidArgumentException('$documents is empty');
8586
}
8687

87-
$expectedIndex = 0;
88+
if (! array_is_list($documents)) {
89+
throw new InvalidArgumentException('$documents is not a list');
90+
}
8891

8992
foreach ($documents as $i => $document) {
90-
if ($i !== $expectedIndex) {
91-
throw new InvalidArgumentException(sprintf('$documents is not a list (unexpected index: "%s")', $i));
92-
}
93-
9493
if (! is_array($document) && ! is_object($document)) {
9594
throw InvalidArgumentException::invalidType(sprintf('$documents[%d]', $i), $document, 'array or object');
9695
}
97-
98-
$expectedIndex += 1;
9996
}
10097

10198
$options += ['ordered' => true];

src/functions.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use ReflectionClass;
3535
use ReflectionException;
3636

37+
use function array_is_list;
3738
use function array_key_first;
3839
use function assert;
3940
use function end;
@@ -260,19 +261,15 @@ function is_pipeline($pipeline, bool $allowEmpty = false): bool
260261
return $allowEmpty;
261262
}
262263

263-
$expectedKey = 0;
264+
if (! array_is_list($pipeline)) {
265+
return false;
266+
}
264267

265-
foreach ($pipeline as $key => $stage) {
268+
foreach ($pipeline as $stage) {
266269
if (! is_array($stage) && ! is_object($stage)) {
267270
return false;
268271
}
269272

270-
if ($expectedKey !== $key) {
271-
return false;
272-
}
273-
274-
$expectedKey++;
275-
276273
if (! is_first_key_operator($stage)) {
277274
return false;
278275
}

tests/Operation/AggregateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class AggregateTest extends TestCase
1010
public function testConstructorPipelineArgumentMustBeAList(): void
1111
{
1212
$this->expectException(InvalidArgumentException::class);
13-
$this->expectExceptionMessage('$pipeline is not a list (unexpected index: "1")');
13+
$this->expectExceptionMessage('$pipeline is not a list');
1414
new Aggregate($this->getDatabaseName(), $this->getCollectionName(), [1 => ['$match' => ['x' => 1]]]);
1515
}
1616

tests/Operation/BulkWriteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testOperationsMustNotBeEmpty(): void
1717
public function testOperationsMustBeAList(): void
1818
{
1919
$this->expectException(InvalidArgumentException::class);
20-
$this->expectExceptionMessage('$operations is not a list (unexpected index: "1")');
20+
$this->expectExceptionMessage('$operations is not a list');
2121
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
2222
1 => [BulkWrite::INSERT_ONE => [['x' => 1]]],
2323
]);

tests/Operation/CreateCollectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CreateCollectionTest extends TestCase
1010
public function testConstructorPipelineOptionMustBeAList(): void
1111
{
1212
$this->expectException(InvalidArgumentException::class);
13-
$this->expectExceptionMessage('The "pipeline" option is not a list (unexpected index: "1")');
13+
$this->expectExceptionMessage('The "pipeline" option is not a list');
1414
new CreateCollection($this->getDatabaseName(), $this->getCollectionName(), ['pipeline' => [1 => ['$match' => ['x' => 1]]]]);
1515
}
1616

tests/Operation/CreateIndexesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CreateIndexesTest extends TestCase
1414
public function testConstructorIndexesArgumentMustBeAList(): void
1515
{
1616
$this->expectException(InvalidArgumentException::class);
17-
$this->expectExceptionMessage('$indexes is not a list (unexpected index: "1")');
17+
$this->expectExceptionMessage('$indexes is not a list');
1818
new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [1 => ['key' => ['x' => 1]]]);
1919
}
2020

tests/Operation/InsertManyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testConstructorDocumentsMustNotBeEmpty(): void
1717
public function testConstructorDocumentsMustBeAList(): void
1818
{
1919
$this->expectException(InvalidArgumentException::class);
20-
$this->expectExceptionMessage('$documents is not a list (unexpected index: "1")');
20+
$this->expectExceptionMessage('$documents is not a list');
2121
new InsertMany($this->getDatabaseName(), $this->getCollectionName(), [1 => ['x' => 1]]);
2222
}
2323

tests/Operation/WatchTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testConstructorCollectionNameShouldBeNullIfDatabaseNameIsNull():
2323
public function testConstructorPipelineArgumentMustBeAList(): void
2424
{
2525
$this->expectException(InvalidArgumentException::class);
26-
$this->expectExceptionMessage('$pipeline is not a list (unexpected index: "foo")');
26+
$this->expectExceptionMessage('$pipeline is not a list');
2727

2828
/* Note: Watch uses array_unshift() to prepend the $changeStream stage
2929
* to the pipeline. Since array_unshift() reindexes numeric keys, we'll

0 commit comments

Comments
 (0)