Skip to content

Commit 984f1f6

Browse files
committed
Update to psalm 5
1 parent f81b4a6 commit 984f1f6

25 files changed

+397
-464
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"rector/rector": "^0.16.0",
2525
"squizlabs/php_codesniffer": "^3.7",
2626
"symfony/phpunit-bridge": "^5.2",
27-
"vimeo/psalm": "^4.28"
27+
"vimeo/psalm": "^5.13"
2828
},
2929
"autoload": {
3030
"psr-4": { "MongoDB\\": "src/" },

examples/changestream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function toJSON(object $document): string
5353
$changeStream->next();
5454

5555
if (time() - $startTime > 3) {
56-
printf("Aborting after 3 seconds...\n");
56+
echo "Aborting after 3 seconds...\n";
5757
break;
5858
}
5959
}

examples/command_logger.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ public function commandStarted(CommandStartedEvent $event): void
3131
printf("%s command started\n", $event->getCommandName());
3232

3333
printf("command: %s\n", toJson($event->getCommand()));
34-
printf("\n");
34+
echo "\n";
3535
}
3636

3737
public function commandSucceeded(CommandSucceededEvent $event): void
3838
{
3939
printf("%s command succeeded\n", $event->getCommandName());
4040
printf("reply: %s\n", toJson($event->getReply()));
41-
printf("\n");
41+
echo "\n";
4242
}
4343

4444
public function commandFailed(CommandFailedEvent $event): void
@@ -50,7 +50,7 @@ public function commandFailed(CommandFailedEvent $event): void
5050
printf("exception: %s\n", get_class($exception));
5151
printf("exception.code: %d\n", $exception->getCode());
5252
printf("exception.message: %s\n", $exception->getMessage());
53-
printf("\n");
53+
echo "\n";
5454
}
5555
}
5656

psalm-baseline.xml

Lines changed: 323 additions & 440 deletions
Large diffs are not rendered by default.

psalm.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
errorLevel="1"
44
errorBaseline="psalm-baseline.xml"
55
resolveFromConfigFile="true"
6+
findUnusedBaselineEntry="false"
7+
findUnusedCode="false"
68
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
79
xmlns="https://getpsalm.org/schema/config"
810
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"

src/ChangeStream.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @see https://mongodb.com/docs/manual/reference/method/db.watch/#mongodb-method-db.watch
3838
*
3939
* @psalm-type ResumeCallable = callable(array|object|null, bool): ChangeStreamIterator
40+
* @template-implements Iterator<int, array|object>
4041
*/
4142
class ChangeStream implements Iterator
4243
{
@@ -94,7 +95,7 @@ public function __construct(ChangeStreamIterator $iterator, callable $resumeCall
9495

9596
/**
9697
* @see https://php.net/iterator.current
97-
* @return mixed
98+
* @return array|object|null
9899
*/
99100
#[ReturnTypeWillChange]
100101
public function current()
@@ -124,7 +125,7 @@ public function getResumeToken()
124125

125126
/**
126127
* @see https://php.net/iterator.key
127-
* @return mixed
128+
* @return int|null
128129
*/
129130
#[ReturnTypeWillChange]
130131
public function key()

src/Codec/CodecLibrary.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
use MongoDB\Exception\InvalidArgumentException;
2121
use MongoDB\Exception\UnsupportedValueException;
2222

23+
/** @template-implements Codec<mixed, mixed> */
2324
class CodecLibrary implements Codec
2425
{
26+
/** @template-use DecodeIfSupported<mixed, mixed> */
2527
use DecodeIfSupported;
28+
/** @template-use EncodeIfSupported<mixed, mixed> */
2629
use EncodeIfSupported;
2730

2831
/** @var list<Decoder> */

src/Command/ListCollections.php

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

2020
use MongoDB\Driver\Command;
21+
use MongoDB\Driver\Cursor;
2122
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2223
use MongoDB\Driver\Server;
2324
use MongoDB\Driver\Session;
@@ -99,11 +100,13 @@ public function __construct(string $databaseName, array $options = [])
99100
/**
100101
* Execute the operation.
101102
*
103+
* @return CachingIterator<int, array>
102104
* @see Executable::execute()
103105
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
104106
*/
105107
public function execute(Server $server): CachingIterator
106108
{
109+
/** @var Cursor<array> $cursor */
107110
$cursor = $server->executeReadCommand($this->databaseName, $this->createCommand(), $this->createOptions());
108111
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
109112

src/MapReduceResult.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@
3333
*
3434
* @see \MongoDB\Collection::mapReduce()
3535
* @see https://mongodb.com/docs/manual/reference/command/mapReduce/
36+
* @template-implements IteratorAggregate<int, array|object>
37+
* @psalm-type MapReduceCallable = callable(): Traversable<int, array|object>
3638
*/
3739
class MapReduceResult implements IteratorAggregate
3840
{
39-
/** @var callable */
41+
/**
42+
* @var callable
43+
* @psalm-var MapReduceCallable
44+
*/
4045
private $getIterator;
4146

4247
private int $executionTimeMS;
@@ -49,6 +54,7 @@ class MapReduceResult implements IteratorAggregate
4954
* @internal
5055
* @param callable $getIterator Callback that returns a Traversable for mapReduce results
5156
* @param stdClass $result Result document from the mapReduce command
57+
* @psalm-param MapReduceCallable $getIterator
5258
*/
5359
public function __construct(callable $getIterator, stdClass $result)
5460
{
@@ -82,7 +88,7 @@ public function getExecutionTimeMS()
8288
* Return the mapReduce results as a Traversable.
8389
*
8490
* @see https://php.net/iteratoraggregate.getiterator
85-
* @return Traversable
91+
* @return Traversable<int, array|object>
8692
*/
8793
#[ReturnTypeWillChange]
8894
public function getIterator()

src/Model/BSONArray.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
*
3232
* The internal data will be filtered through array_values() during BSON
3333
* serialization to ensure that it becomes a BSON array.
34+
*
35+
* @template-extends ArrayObject<int, mixed>
3436
*/
3537
class BSONArray extends ArrayObject implements JsonSerializable, Serializable, Unserializable
3638
{
@@ -78,7 +80,7 @@ public function bsonSerialize()
7880
* Unserialize the document to BSON.
7981
*
8082
* @see https://php.net/mongodb-bson-unserializable.bsonunserialize
81-
* @param array $data Array data
83+
* @param array<int, mixed> $data Array data
8284
*/
8385
#[ReturnTypeWillChange]
8486
public function bsonUnserialize(array $data)

src/Model/BSONDocument.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
*
3131
* The internal data will be cast to an object during BSON serialization to
3232
* ensure that it becomes a BSON document.
33+
*
34+
* @template-extends ArrayObject<string, mixed>
3335
*/
3436
class BSONDocument extends ArrayObject implements JsonSerializable, Serializable, Unserializable
3537
{
@@ -48,6 +50,7 @@ public function __clone()
4850
* by default.
4951
*
5052
* @see https://php.net/arrayobject.construct
53+
* @param array<string, mixed> $input
5154
*/
5255
public function __construct(array $input = [], int $flags = ArrayObject::ARRAY_AS_PROPS, string $iteratorClass = 'ArrayIterator')
5356
{
@@ -85,7 +88,7 @@ public function bsonSerialize()
8588
* Unserialize the document to BSON.
8689
*
8790
* @see https://php.net/mongodb-bson-unserializable.bsonunserialize
88-
* @param array $data Array data
91+
* @param array<string, mixed> $data Array data
8992
*/
9093
#[ReturnTypeWillChange]
9194
public function bsonUnserialize(array $data)

src/Model/BSONIterator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
/**
3333
* Iterator for BSON documents.
34+
*
35+
* @template-implements Iterator<int, mixed>
3436
*/
3537
class BSONIterator implements Iterator
3638
{
@@ -89,7 +91,7 @@ public function current()
8991

9092
/**
9193
* @see https://php.net/iterator.key
92-
* @return mixed
94+
* @return int
9395
*/
9496
#[ReturnTypeWillChange]
9597
public function key()

src/Model/CachingIterator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@
3636
* those operations (e.g. MongoDB\Driver\Cursor).
3737
*
3838
* @internal
39+
* @template TKey of array-key
40+
* @template TValue
41+
* @template-implements Iterator<TKey, TValue>
3942
*/
4043
class CachingIterator implements Countable, Iterator
4144
{
4245
private const FIELD_KEY = 0;
4346
private const FIELD_VALUE = 1;
4447

48+
/** @var list<array{0: TKey, 1: TValue}> */
4549
private array $items = [];
4650

51+
/** @var Iterator<TKey, TValue> */
4752
private Iterator $iterator;
4853

4954
private bool $iteratorAdvanced = false;
@@ -56,7 +61,7 @@ class CachingIterator implements Countable, Iterator
5661
* Additionally, this mimics behavior of the SPL iterators and allows users
5762
* to omit an explicit call to rewind() before using the other methods.
5863
*
59-
* @param Traversable $traversable
64+
* @param Traversable<TKey, TValue> $traversable
6065
*/
6166
public function __construct(Traversable $traversable)
6267
{
@@ -83,12 +88,13 @@ public function current()
8388
{
8489
$currentItem = current($this->items);
8590

86-
return $currentItem !== false ? $currentItem[self::FIELD_VALUE] : false;
91+
return $currentItem !== false ? $currentItem[self::FIELD_VALUE] : null;
8792
}
8893

8994
/**
9095
* @see https://php.net/iterator.key
9196
* @return mixed
97+
* @psalm-return TKey|null
9298
*/
9399
#[ReturnTypeWillChange]
94100
public function key()

src/Model/ChangeStreamIterator.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
* rewind() do not execute getMore commands.
4747
*
4848
* @internal
49+
* @template TValue of array|object
50+
* @template-extends IteratorIterator<int, TValue, Cursor<TValue>>
4951
*/
5052
class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
5153
{
@@ -67,6 +69,7 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
6769
/**
6870
* @internal
6971
* @param array|object|null $initialResumeToken
72+
* @psalm-param Cursor<TValue> $cursor
7073
*/
7174
public function __construct(Cursor $cursor, int $firstBatchSize, $initialResumeToken, ?object $postBatchResumeToken)
7275
{
@@ -122,12 +125,13 @@ final public function commandSucceeded(CommandSucceededEvent $event): void
122125

123126
/**
124127
* @see https://php.net/iteratoriterator.current
125-
* @return mixed
128+
* @return array|object|null
129+
* @psalm-return TValue|null
126130
*/
127131
#[ReturnTypeWillChange]
128132
public function current()
129133
{
130-
return $this->isValid ? parent::current() : null;
134+
return $this->valid() ? parent::current() : null;
131135
}
132136

133137
/**
@@ -168,12 +172,12 @@ public function getServer(): Server
168172

169173
/**
170174
* @see https://php.net/iteratoriterator.key
171-
* @return mixed
175+
* @return int|null
172176
*/
173177
#[ReturnTypeWillChange]
174178
public function key()
175179
{
176-
return $this->isValid ? parent::key() : null;
180+
return $this->valid() ? parent::key() : null;
177181
}
178182

179183
/** @see https://php.net/iteratoriterator.rewind */
@@ -213,7 +217,10 @@ public function rewind(): void
213217
$this->onIteration(false);
214218
}
215219

216-
/** @see https://php.net/iteratoriterator.valid */
220+
/**
221+
* @see https://php.net/iteratoriterator.valid
222+
* @psalm-assert-if-true TValue $this->current()
223+
*/
217224
public function valid(): bool
218225
{
219226
return $this->isValid;
@@ -276,11 +283,11 @@ private function onIteration(bool $incrementBatchPosition): void
276283
/* Disable rewind()'s NOP behavior once we advance to a valid position.
277284
* This will allow the driver to throw a LogicException if rewind() is
278285
* called after the cursor has advanced past its first element. */
279-
if ($this->isRewindNop && $this->isValid) {
286+
if ($this->isRewindNop && $this->valid()) {
280287
$this->isRewindNop = false;
281288
}
282289

283-
if ($incrementBatchPosition && $this->isValid) {
290+
if ($incrementBatchPosition && $this->valid()) {
284291
$this->batchPosition++;
285292
}
286293

@@ -292,7 +299,7 @@ private function onIteration(bool $incrementBatchPosition): void
292299
* from the current document if possible. */
293300
if ($this->isAtEndOfBatch() && $this->postBatchResumeToken !== null) {
294301
$this->resumeToken = $this->postBatchResumeToken;
295-
} elseif ($this->isValid) {
302+
} elseif ($this->valid()) {
296303
$this->resumeToken = $this->extractResumeToken($this->current());
297304
}
298305
}

src/Model/CollectionInfo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*
3333
* @see \MongoDB\Database::listCollections()
3434
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-collections.rst
35+
* @template-implements ArrayAccess<string, mixed>
3536
*/
3637
class CollectionInfo implements ArrayAccess
3738
{

src/Model/CollectionInfoCommandIterator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
* @see \MongoDB\Database::listCollections()
3131
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-collections.rst
3232
* @see https://mongodb.com/docs/manual/reference/command/listCollections/
33+
* @template-extends IteratorIterator<int, array, Traversable<int, array>>
3334
*/
3435
class CollectionInfoCommandIterator extends IteratorIterator implements CollectionInfoIterator
3536
{
3637
private ?string $databaseName = null;
3738

39+
/** @param Traversable<int, array> $iterator */
3840
public function __construct(Traversable $iterator, ?string $databaseName = null)
3941
{
4042
parent::__construct($iterator);

src/Model/CollectionInfoIterator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* This iterator is used for enumerating collections in a database.
2727
*
2828
* @see \MongoDB\Database::listCollections()
29+
* @template-extends Iterator<int, CollectionInfo>
2930
*/
3031
interface CollectionInfoIterator extends Iterator
3132
{

src/Model/DatabaseInfo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*
3232
* @see \MongoDB\Client::listDatabases()
3333
* @see https://mongodb.com/docs/manual/reference/command/listDatabases/
34+
* @template-implements ArrayAccess<string, mixed>
3435
*/
3536
class DatabaseInfo implements ArrayAccess
3637
{

src/Model/DatabaseInfoIterator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* This iterator is used for enumerating databases on a server.
2727
*
2828
* @see \MongoDB\Client::listDatabases()
29+
* @template-extends Iterator<int, DatabaseInfo>
2930
*/
3031
interface DatabaseInfoIterator extends Iterator
3132
{

src/Model/IndexInfo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @see \MongoDB\Collection::listIndexes()
4141
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst
4242
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createIndex/
43+
* @template-implements ArrayAccess<string, mixed>
4344
*/
4445
class IndexInfo implements ArrayAccess
4546
{

src/Model/IndexInfoIterator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* This iterator is used for enumerating indexes in a collection.
2727
*
2828
* @see \MongoDB\Collection::listIndexes()
29+
* @template-extends Iterator<int, IndexInfo>
2930
*/
3031
interface IndexInfoIterator extends Iterator
3132
{

0 commit comments

Comments
 (0)