Skip to content

PHPLIB-1123: Use array_is_list #1093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"ext-mongodb": "^1.16.0",
"jean85/pretty-package-versions": "^2.0.1",
"symfony/polyfill-php73": "^1.27",
"symfony/polyfill-php80": "^1.27"
"symfony/polyfill-php80": "^1.27",
"symfony/polyfill-php81": "^1.27"
},
"require-dev": {
"doctrine/coding-standard": "^11.1",
Expand Down
6 changes: 3 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,13 @@
</DocblockTypeContradiction>
</file>
<file src="src/Operation/CreateCollection.php">
<MixedArgument occurrences="3">
<code>$i</code>
<MixedArgument occurrences="2">
<code>$i</code>
<code>$this-&gt;options['typeMap']</code>
</MixedArgument>
<MixedAssignment occurrences="3">
<MixedAssignment occurrences="4">
<code>$cmd[$option]</code>
<code>$i</code>
<code>$options['session']</code>
<code>$options['writeConcern']</code>
</MixedAssignment>
Expand Down
11 changes: 4 additions & 7 deletions src/Operation/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use MongoDB\Exception\UnsupportedException;
use stdClass;

use function array_is_list;
use function current;
use function is_array;
use function is_bool;
Expand Down Expand Up @@ -137,18 +138,14 @@ class Aggregate implements Executable, Explainable
*/
public function __construct(string $databaseName, ?string $collectionName, array $pipeline, array $options = [])
{
$expectedIndex = 0;
if (! array_is_list($pipeline)) {
throw new InvalidArgumentException('$pipeline is not a list');
}

foreach ($pipeline as $i => $operation) {
if ($i !== $expectedIndex) {
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
}

if (! is_array($operation) && ! is_object($operation)) {
throw InvalidArgumentException::invalidType(sprintf('$pipeline[%d]', $i), $operation, 'array or object');
}

$expectedIndex += 1;
}

$options += ['useCursor' => true];
Expand Down
11 changes: 4 additions & 7 deletions src/Operation/BulkWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;

use function array_is_list;
use function array_key_exists;
use function count;
use function current;
Expand Down Expand Up @@ -132,13 +133,11 @@ public function __construct(string $databaseName, string $collectionName, array
throw new InvalidArgumentException('$operations is empty');
}

$expectedIndex = 0;
if (! array_is_list($operations)) {
throw new InvalidArgumentException('$operations is not a list');
}

foreach ($operations as $i => $operation) {
if ($i !== $expectedIndex) {
throw new InvalidArgumentException(sprintf('$operations is not a list (unexpected index: "%s")', $i));
}

if (! is_array($operation)) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]', $i), $operation, 'array');
}
Expand Down Expand Up @@ -271,8 +270,6 @@ public function __construct(string $databaseName, string $collectionName, array
default:
throw new InvalidArgumentException(sprintf('Unknown operation type "%s" in $operations[%d]', $type, $i));
}

$expectedIndex += 1;
}

$options += ['ordered' => true];
Expand Down
14 changes: 7 additions & 7 deletions src/Operation/CreateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;

use function array_is_list;
use function assert;
use function current;
use function is_array;
use function is_bool;
Expand Down Expand Up @@ -241,18 +243,16 @@ public function __construct(string $databaseName, string $collectionName, array
}

if (isset($options['pipeline'])) {
$expectedIndex = 0;
$pipeline = $options['pipeline'];
assert(is_array($pipeline));
if (! array_is_list($pipeline)) {
throw new InvalidArgumentException('The "pipeline" option is not a list');
}

foreach ($options['pipeline'] as $i => $operation) {
if ($i !== $expectedIndex) {
throw new InvalidArgumentException(sprintf('The "pipeline" option is not a list (unexpected index: "%s")', $i));
}

if (! is_array($operation) && ! is_object($operation)) {
throw InvalidArgumentException::invalidType(sprintf('$options["pipeline"][%d]', $i), $operation, 'array or object');
}

$expectedIndex += 1;
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/Operation/CreateIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use MongoDB\Exception\UnsupportedException;
use MongoDB\Model\IndexInput;

use function array_is_list;
use function array_map;
use function is_array;
use function is_integer;
Expand Down Expand Up @@ -88,20 +89,16 @@ public function __construct(string $databaseName, string $collectionName, array
throw new InvalidArgumentException('$indexes is empty');
}

$expectedIndex = 0;
if (! array_is_list($indexes)) {
throw new InvalidArgumentException('$indexes is not a list');
}

foreach ($indexes as $i => $index) {
if ($i !== $expectedIndex) {
throw new InvalidArgumentException(sprintf('$indexes is not a list (unexpected index: "%s")', $i));
}

if (! is_array($index)) {
throw InvalidArgumentException::invalidType(sprintf('$index[%d]', $i), $index, 'array');
}

$this->indexes[] = new IndexInput($index);

$expectedIndex += 1;
}

if (isset($options['commitQuorum']) && ! is_string($options['commitQuorum']) && ! is_integer($options['commitQuorum'])) {
Expand Down
11 changes: 4 additions & 7 deletions src/Operation/InsertMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use MongoDB\Exception\UnsupportedException;
use MongoDB\InsertManyResult;

use function array_is_list;
use function is_array;
use function is_bool;
use function is_object;
Expand Down Expand Up @@ -84,18 +85,14 @@ public function __construct(string $databaseName, string $collectionName, array
throw new InvalidArgumentException('$documents is empty');
}

$expectedIndex = 0;
if (! array_is_list($documents)) {
throw new InvalidArgumentException('$documents is not a list');
}

foreach ($documents as $i => $document) {
if ($i !== $expectedIndex) {
throw new InvalidArgumentException(sprintf('$documents is not a list (unexpected index: "%s")', $i));
}

if (! is_array($document) && ! is_object($document)) {
throw InvalidArgumentException::invalidType(sprintf('$documents[%d]', $i), $document, 'array or object');
}

$expectedIndex += 1;
}

$options += ['ordered' => true];
Expand Down
13 changes: 5 additions & 8 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use ReflectionClass;
use ReflectionException;

use function array_is_list;
use function array_key_first;
use function assert;
use function end;
Expand Down Expand Up @@ -260,19 +261,15 @@ function is_pipeline($pipeline, bool $allowEmpty = false): bool
return $allowEmpty;
}

$expectedKey = 0;
if (! array_is_list($pipeline)) {
return false;
}

foreach ($pipeline as $key => $stage) {
foreach ($pipeline as $stage) {
if (! is_array($stage) && ! is_object($stage)) {
return false;
}

if ($expectedKey !== $key) {
return false;
}

$expectedKey++;

if (! is_first_key_operator($stage)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/AggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AggregateTest extends TestCase
public function testConstructorPipelineArgumentMustBeAList(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('$pipeline is not a list (unexpected index: "1")');
$this->expectExceptionMessage('$pipeline is not a list');
new Aggregate($this->getDatabaseName(), $this->getCollectionName(), [1 => ['$match' => ['x' => 1]]]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/BulkWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testOperationsMustNotBeEmpty(): void
public function testOperationsMustBeAList(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('$operations is not a list (unexpected index: "1")');
$this->expectExceptionMessage('$operations is not a list');
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
1 => [BulkWrite::INSERT_ONE => [['x' => 1]]],
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/CreateCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CreateCollectionTest extends TestCase
public function testConstructorPipelineOptionMustBeAList(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "pipeline" option is not a list (unexpected index: "1")');
$this->expectExceptionMessage('The "pipeline" option is not a list');
new CreateCollection($this->getDatabaseName(), $this->getCollectionName(), ['pipeline' => [1 => ['$match' => ['x' => 1]]]]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/CreateIndexesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CreateIndexesTest extends TestCase
public function testConstructorIndexesArgumentMustBeAList(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('$indexes is not a list (unexpected index: "1")');
$this->expectExceptionMessage('$indexes is not a list');
new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [1 => ['key' => ['x' => 1]]]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/InsertManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testConstructorDocumentsMustNotBeEmpty(): void
public function testConstructorDocumentsMustBeAList(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('$documents is not a list (unexpected index: "1")');
$this->expectExceptionMessage('$documents is not a list');
new InsertMany($this->getDatabaseName(), $this->getCollectionName(), [1 => ['x' => 1]]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Operation/WatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testConstructorCollectionNameShouldBeNullIfDatabaseNameIsNull():
public function testConstructorPipelineArgumentMustBeAList(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('$pipeline is not a list (unexpected index: "foo")');
$this->expectExceptionMessage('$pipeline is not a list');

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