Skip to content

Commit 8a05f1f

Browse files
committed
PHPLIB-954: Add return types to all methods
1 parent bef72b9 commit 8a05f1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+295
-696
lines changed

phpcs.xml.dist

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,6 @@
133133
</rule>
134134

135135

136-
<!-- *********************************************************** -->
137-
<!-- Require native type hints for all code without a BC promise -->
138-
<!-- *********************************************************** -->
139-
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint">
140-
<exclude-pattern>src</exclude-pattern>
141-
</rule>
142-
143-
144136
<!-- ************************************************************* -->
145137
<!-- Ignore errors for certain files where this is part of the API -->
146138
<!-- ************************************************************* -->

psalm-baseline.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,15 @@
320320
</InvalidParamDefault>
321321
<LessSpecificReturnStatement>
322322
<code><![CDATA[(object) $this->getArrayCopy()]]></code>
323+
<code><![CDATA[(object) $this->getArrayCopy()]]></code>
323324
</LessSpecificReturnStatement>
324325
<MixedAssignment>
325326
<code><![CDATA[$this[$key]]]></code>
326327
<code><![CDATA[$value]]></code>
327328
</MixedAssignment>
328329
<MoreSpecificReturnType>
329330
<code><![CDATA[stdClass]]></code>
331+
<code><![CDATA[stdClass]]></code>
330332
</MoreSpecificReturnType>
331333
</file>
332334
<file src="src/Model/BSONIterator.php">

src/BulkWriteResult.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ public function __construct(private WriteResult $writeResult, private array $ins
3838
* This method should only be called if the write was acknowledged.
3939
*
4040
* @see BulkWriteResult::isAcknowledged()
41-
* @return integer|null
4241
* @throws BadMethodCallException if the write result is unacknowledged
4342
*/
44-
public function getDeletedCount()
43+
public function getDeletedCount(): ?int
4544
{
4645
if ($this->isAcknowledged) {
4746
return $this->writeResult->getDeletedCount();
@@ -56,10 +55,9 @@ public function getDeletedCount()
5655
* This method should only be called if the write was acknowledged.
5756
*
5857
* @see BulkWriteResult::isAcknowledged()
59-
* @return integer|null
6058
* @throws BadMethodCallException if the write result is unacknowledged
6159
*/
62-
public function getInsertedCount()
60+
public function getInsertedCount(): ?int
6361
{
6462
if ($this->isAcknowledged) {
6563
return $this->writeResult->getInsertedCount();
@@ -76,10 +74,8 @@ public function getInsertedCount()
7674
* the driver did not generate an ID), the index will contain its "_id"
7775
* field value. Any driver-generated ID will be a MongoDB\BSON\ObjectId
7876
* instance.
79-
*
80-
* @return array
8177
*/
82-
public function getInsertedIds()
78+
public function getInsertedIds(): array
8379
{
8480
return $this->insertedIds;
8581
}
@@ -90,10 +86,9 @@ public function getInsertedIds()
9086
* This method should only be called if the write was acknowledged.
9187
*
9288
* @see BulkWriteResult::isAcknowledged()
93-
* @return integer|null
9489
* @throws BadMethodCallException if the write result is unacknowledged
9590
*/
96-
public function getMatchedCount()
91+
public function getMatchedCount(): ?int
9792
{
9893
if ($this->isAcknowledged) {
9994
return $this->writeResult->getMatchedCount();
@@ -111,10 +106,9 @@ public function getMatchedCount()
111106
* This method should only be called if the write was acknowledged.
112107
*
113108
* @see BulkWriteResult::isAcknowledged()
114-
* @return integer|null
115109
* @throws BadMethodCallException if the write result is unacknowledged
116110
*/
117-
public function getModifiedCount()
111+
public function getModifiedCount(): ?int
118112
{
119113
if ($this->isAcknowledged) {
120114
return $this->writeResult->getModifiedCount();
@@ -129,10 +123,9 @@ public function getModifiedCount()
129123
* This method should only be called if the write was acknowledged.
130124
*
131125
* @see BulkWriteResult::isAcknowledged()
132-
* @return integer|null
133126
* @throws BadMethodCallException if the write result is unacknowledged
134127
*/
135-
public function getUpsertedCount()
128+
public function getUpsertedCount(): ?int
136129
{
137130
if ($this->isAcknowledged) {
138131
return $this->writeResult->getUpsertedCount();
@@ -152,10 +145,9 @@ public function getUpsertedCount()
152145
* This method should only be called if the write was acknowledged.
153146
*
154147
* @see BulkWriteResult::isAcknowledged()
155-
* @return array
156148
* @throws BadMethodCallException if the write result is unacknowledged
157149
*/
158-
public function getUpsertedIds()
150+
public function getUpsertedIds(): array
159151
{
160152
if ($this->isAcknowledged) {
161153
return $this->writeResult->getUpsertedIds();
@@ -169,10 +161,8 @@ public function getUpsertedIds()
169161
*
170162
* If the update was not acknowledged, other fields from the WriteResult
171163
* (e.g. matchedCount) will be undefined.
172-
*
173-
* @return boolean
174164
*/
175-
public function isAcknowledged()
165+
public function isAcknowledged(): bool
176166
{
177167
return $this->isAcknowledged;
178168
}

src/ChangeStream.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use MongoDB\Exception\BadMethodCallException;
2828
use MongoDB\Exception\ResumeTokenException;
2929
use MongoDB\Model\ChangeStreamIterator;
30-
use ReturnTypeWillChange;
3130

3231
use function assert;
3332
use function call_user_func;
@@ -108,20 +107,14 @@ public function getCursorId(): Int64
108107
* Null may be returned if no change documents have been iterated and the
109108
* server did not include a postBatchResumeToken in its aggregate or getMore
110109
* command response.
111-
*
112-
* @return array|object|null
113110
*/
114-
public function getResumeToken()
111+
public function getResumeToken(): array|object|null
115112
{
116113
return $this->iterator->getResumeToken();
117114
}
118115

119-
/**
120-
* @see https://php.net/iterator.key
121-
* @return int|null
122-
*/
123-
#[ReturnTypeWillChange]
124-
public function key()
116+
/** @see https://php.net/iterator.key */
117+
public function key(): ?int
125118
{
126119
if ($this->valid()) {
127120
return $this->key;
@@ -132,11 +125,9 @@ public function key()
132125

133126
/**
134127
* @see https://php.net/iterator.next
135-
* @return void
136128
* @throws ResumeTokenException
137129
*/
138-
#[ReturnTypeWillChange]
139-
public function next()
130+
public function next(): void
140131
{
141132
try {
142133
$this->iterator->next();
@@ -148,11 +139,9 @@ public function next()
148139

149140
/**
150141
* @see https://php.net/iterator.rewind
151-
* @return void
152142
* @throws ResumeTokenException
153143
*/
154-
#[ReturnTypeWillChange]
155-
public function rewind()
144+
public function rewind(): void
156145
{
157146
try {
158147
$this->iterator->rewind();
@@ -165,12 +154,8 @@ public function rewind()
165154
}
166155
}
167156

168-
/**
169-
* @see https://php.net/iterator.valid
170-
* @return boolean
171-
*/
172-
#[ReturnTypeWillChange]
173-
public function valid()
157+
/** @see https://php.net/iterator.valid */
158+
public function valid(): bool
174159
{
175160
return $this->iterator->valid();
176161
}

src/Client.php

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@ public function __construct(?string $uri = null, array $uriOptions = [], array $
143143
* Return internal properties for debugging purposes.
144144
*
145145
* @see https://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
146-
* @return array
147146
*/
148-
public function __debugInfo()
147+
public function __debugInfo(): array
149148
{
150149
return [
151150
'manager' => $this->manager,
@@ -166,19 +165,16 @@ public function __debugInfo()
166165
* @see https://php.net/oop5.overloading#object.get
167166
* @see https://php.net/types.string#language.types.string.parsing.complex
168167
* @param string $databaseName Name of the database to select
169-
* @return Database
170168
*/
171-
public function __get(string $databaseName)
169+
public function __get(string $databaseName): Database
172170
{
173171
return $this->selectDatabase($databaseName);
174172
}
175173

176174
/**
177175
* Return the connection string (i.e. URI).
178-
*
179-
* @return string
180176
*/
181-
public function __toString()
177+
public function __toString(): string
182178
{
183179
return $this->uri;
184180
}
@@ -197,10 +193,8 @@ final public function addSubscriber(Subscriber $subscriber): void
197193
* Returns a ClientEncryption instance for explicit encryption and decryption
198194
*
199195
* @param array $options Encryption options
200-
*
201-
* @return ClientEncryption
202196
*/
203-
public function createClientEncryption(array $options)
197+
public function createClientEncryption(array $options): ClientEncryption
204198
{
205199
if (isset($options['keyVaultClient'])) {
206200
if ($options['keyVaultClient'] instanceof self) {
@@ -224,7 +218,7 @@ public function createClientEncryption(array $options)
224218
* @throws InvalidArgumentException for parameter/option parsing errors
225219
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
226220
*/
227-
public function dropDatabase(string $databaseName, array $options = [])
221+
public function dropDatabase(string $databaseName, array $options = []): array|object
228222
{
229223
if (! isset($options['typeMap'])) {
230224
$options['typeMap'] = $this->typeMap;
@@ -243,10 +237,8 @@ public function dropDatabase(string $databaseName, array $options = [])
243237

244238
/**
245239
* Return the Manager.
246-
*
247-
* @return Manager
248240
*/
249-
public function getManager()
241+
public function getManager(): Manager
250242
{
251243
return $this->manager;
252244
}
@@ -255,29 +247,24 @@ public function getManager()
255247
* Return the read concern for this client.
256248
*
257249
* @see https://php.net/manual/en/mongodb-driver-readconcern.isdefault.php
258-
* @return ReadConcern
259250
*/
260-
public function getReadConcern()
251+
public function getReadConcern(): ReadConcern
261252
{
262253
return $this->readConcern;
263254
}
264255

265256
/**
266257
* Return the read preference for this client.
267-
*
268-
* @return ReadPreference
269258
*/
270-
public function getReadPreference()
259+
public function getReadPreference(): ReadPreference
271260
{
272261
return $this->readPreference;
273262
}
274263

275264
/**
276265
* Return the type map for this client.
277-
*
278-
* @return array
279266
*/
280-
public function getTypeMap()
267+
public function getTypeMap(): array
281268
{
282269
return $this->typeMap;
283270
}
@@ -286,9 +273,8 @@ public function getTypeMap()
286273
* Return the write concern for this client.
287274
*
288275
* @see https://php.net/manual/en/mongodb-driver-writeconcern.isdefault.php
289-
* @return WriteConcern
290276
*/
291-
public function getWriteConcern()
277+
public function getWriteConcern(): WriteConcern
292278
{
293279
return $this->writeConcern;
294280
}
@@ -313,12 +299,11 @@ public function listDatabaseNames(array $options = []): Iterator
313299
* List databases.
314300
*
315301
* @see ListDatabases::__construct() for supported options
316-
* @return DatabaseInfoIterator
317302
* @throws UnexpectedValueException if the command response was malformed
318303
* @throws InvalidArgumentException for parameter/option parsing errors
319304
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
320305
*/
321-
public function listDatabases(array $options = [])
306+
public function listDatabases(array $options = []): DatabaseInfoIterator
322307
{
323308
$operation = new ListDatabases($options);
324309
$server = select_server($this->manager, $options);
@@ -343,10 +328,9 @@ final public function removeSubscriber(Subscriber $subscriber): void
343328
* @param string $databaseName Name of the database containing the collection
344329
* @param string $collectionName Name of the collection to select
345330
* @param array $options Collection constructor options
346-
* @return Collection
347331
* @throws InvalidArgumentException for parameter/option parsing errors
348332
*/
349-
public function selectCollection(string $databaseName, string $collectionName, array $options = [])
333+
public function selectCollection(string $databaseName, string $collectionName, array $options = []): Collection
350334
{
351335
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
352336

@@ -359,10 +343,9 @@ public function selectCollection(string $databaseName, string $collectionName, a
359343
* @see Database::__construct() for supported options
360344
* @param string $databaseName Name of the database to select
361345
* @param array $options Database constructor options
362-
* @return Database
363346
* @throws InvalidArgumentException for parameter/option parsing errors
364347
*/
365-
public function selectDatabase(string $databaseName, array $options = [])
348+
public function selectDatabase(string $databaseName, array $options = []): Database
366349
{
367350
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
368351

@@ -374,9 +357,8 @@ public function selectDatabase(string $databaseName, array $options = [])
374357
*
375358
* @see https://php.net/manual/en/mongodb-driver-manager.startsession.php
376359
* @param array $options Session options
377-
* @return Session
378360
*/
379-
public function startSession(array $options = [])
361+
public function startSession(array $options = []): Session
380362
{
381363
return $this->manager->startSession($options);
382364
}
@@ -387,10 +369,9 @@ public function startSession(array $options = [])
387369
* @see Watch::__construct() for supported options
388370
* @param array $pipeline Aggregation pipeline
389371
* @param array $options Command options
390-
* @return ChangeStream
391372
* @throws InvalidArgumentException for parameter/option parsing errors
392373
*/
393-
public function watch(array $pipeline = [], array $options = [])
374+
public function watch(array $pipeline = [], array $options = []): ChangeStream
394375
{
395376
if (is_builder_pipeline($pipeline)) {
396377
$pipeline = new Pipeline(...$pipeline);

src/Codec/DecodeIfSupported.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,13 @@ abstract public function canDecode(mixed $value): bool;
3030

3131
/**
3232
* @psalm-param BSONType $value
33-
* @return mixed
3433
* @psalm-return NativeType
3534
* @throws UnsupportedValueException if the decoder does not support the value
3635
*/
37-
abstract public function decode(mixed $value);
36+
abstract public function decode(mixed $value): mixed;
3837

39-
/**
40-
* @return mixed
41-
* @psalm-return ($value is BSONType ? NativeType : $value)
42-
*/
43-
public function decodeIfSupported(mixed $value)
38+
/** @psalm-return ($value is BSONType ? NativeType : $value) */
39+
public function decodeIfSupported(mixed $value): mixed
4440
{
4541
return $this->canDecode($value) ? $this->decode($value) : $value;
4642
}

0 commit comments

Comments
 (0)