Skip to content

Commit d273370

Browse files
committed
PHPLIB-602: Add return type definitions where applicable
1 parent b0f110f commit d273370

37 files changed

+82
-87
lines changed

phpcs.xml.dist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@
133133

134134
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
135135
<properties>
136-
<!-- Requires PHP 7.2 -->
137-
<property name="enableObjectTypeHint" value="false" />
138136
<!-- Requires PHP 8.0 -->
139137
<property name="enableStaticTypeHint" value="false" />
140138
<!-- Requires PHP 8.0 -->

src/ChangeStream.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function valid()
197197
* @param RuntimeException $exception
198198
* @return boolean
199199
*/
200-
private function isResumableError(RuntimeException $exception)
200+
private function isResumableError(RuntimeException $exception): bool
201201
{
202202
if ($exception instanceof ConnectionException) {
203203
return true;
@@ -224,7 +224,7 @@ private function isResumableError(RuntimeException $exception)
224224
* @param boolean $incrementKey Increment $key if there is a current result
225225
* @throws ResumeTokenException
226226
*/
227-
private function onIteration(bool $incrementKey)
227+
private function onIteration(bool $incrementKey): void
228228
{
229229
/* If the cursorId is 0, the server has invalidated the cursor and we
230230
* will never perform another getMore nor need to resume since any
@@ -254,7 +254,7 @@ private function onIteration(bool $incrementKey)
254254
*
255255
* @return void
256256
*/
257-
private function resume()
257+
private function resume(): void
258258
{
259259
$this->iterator = call_user_func($this->resumeCallable, $this->getResumeToken(), $this->hasAdvanced);
260260
$this->iterator->rewind();
@@ -268,7 +268,7 @@ private function resume()
268268
* @param RuntimeException $exception
269269
* @throws RuntimeException
270270
*/
271-
private function resumeOrThrow(RuntimeException $exception)
271+
private function resumeOrThrow(RuntimeException $exception): void
272272
{
273273
if ($this->isResumableError($exception)) {
274274
$this->resume();

src/Command/ListCollections.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function execute(Server $server)
120120
*
121121
* @return Command
122122
*/
123-
private function createCommand()
123+
private function createCommand(): Command
124124
{
125125
$cmd = ['listCollections' => 1];
126126

@@ -146,7 +146,7 @@ private function createCommand()
146146
* @see https://php.net/manual/en/mongodb-driver-server.executecommand.php
147147
* @return array
148148
*/
149-
private function createOptions()
149+
private function createOptions(): array
150150
{
151151
$options = [];
152152

src/Command/ListDatabases.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function execute(Server $server)
122122
*
123123
* @return Command
124124
*/
125-
private function createCommand()
125+
private function createCommand(): Command
126126
{
127127
$cmd = ['listDatabases' => 1];
128128

@@ -148,7 +148,7 @@ private function createCommand()
148148
* @see https://php.net/manual/en/mongodb-driver-server.executecommand.php
149149
* @return array
150150
*/
151-
private function createOptions()
151+
private function createOptions(): array
152152
{
153153
$options = [];
154154

src/GridFS/Bucket.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ public function uploadFromStream(string $filename, $source, array $options = [])
643643
* @param stdClass $file GridFS file document
644644
* @return string
645645
*/
646-
private function createPathForFile(stdClass $file)
646+
private function createPathForFile(stdClass $file): string
647647
{
648648
if (! is_object($file->_id) || method_exists($file->_id, '__toString')) {
649649
$id = (string) $file->_id;
@@ -665,7 +665,7 @@ private function createPathForFile(stdClass $file)
665665
*
666666
* @return string
667667
*/
668-
private function createPathForUpload()
668+
private function createPathForUpload(): string
669669
{
670670
return sprintf(
671671
'%s://%s/%s.files',
@@ -680,7 +680,7 @@ private function createPathForUpload()
680680
*
681681
* @return string
682682
*/
683-
private function getFilesNamespace()
683+
private function getFilesNamespace(): string
684684
{
685685
return sprintf('%s.%s.files', $this->databaseName, $this->bucketName);
686686
}
@@ -695,7 +695,7 @@ private function getFilesNamespace()
695695
* @return stdClass
696696
* @throws InvalidArgumentException
697697
*/
698-
private function getRawFileDocumentForStream($stream)
698+
private function getRawFileDocumentForStream($stream): stdClass
699699
{
700700
if (! is_resource($stream) || get_resource_type($stream) != "stream") {
701701
throw InvalidArgumentException::invalidType('$stream', $stream, 'resource');
@@ -732,7 +732,7 @@ private function openDownloadStreamByFile(stdClass $file)
732732
/**
733733
* Registers the GridFS stream wrapper if it is not already registered.
734734
*/
735-
private function registerStreamWrapper()
735+
private function registerStreamWrapper(): void
736736
{
737737
if (in_array(self::$streamWrapperProtocol, stream_get_wrappers())) {
738738
return;

src/GridFS/CollectionWrapper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public function updateFilenameForId($id, string $filename)
292292
/**
293293
* Create an index on the chunks collection if it does not already exist.
294294
*/
295-
private function ensureChunksIndex()
295+
private function ensureChunksIndex(): void
296296
{
297297
$expectedIndex = ['files_id' => 1, 'n' => 1];
298298

@@ -308,7 +308,7 @@ private function ensureChunksIndex()
308308
/**
309309
* Create an index on the files collection if it does not already exist.
310310
*/
311-
private function ensureFilesIndex()
311+
private function ensureFilesIndex(): void
312312
{
313313
$expectedIndex = ['filename' => 1, 'uploadDate' => 1];
314314

@@ -327,7 +327,7 @@ private function ensureFilesIndex()
327327
* This method is called once before the first write operation on a GridFS
328328
* bucket. Indexes are only be created if the files collection is empty.
329329
*/
330-
private function ensureIndexes()
330+
private function ensureIndexes(): void
331331
{
332332
if ($this->checkedIndexes) {
333333
return;
@@ -378,7 +378,7 @@ private function indexKeysMatch(array $expectedKeys, array $actualKeys): bool
378378
*
379379
* @return boolean
380380
*/
381-
private function isFilesCollectionEmpty()
381+
private function isFilesCollectionEmpty(): bool
382382
{
383383
return null === $this->filesCollection->findOne([], [
384384
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),

src/GridFS/ReadableStream.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public function tell()
261261
* @return boolean Whether there was a current chunk to read
262262
* @throws CorruptFileException if an expected chunk could not be read successfully
263263
*/
264-
private function initBufferFromCurrentChunk()
264+
private function initBufferFromCurrentChunk(): bool
265265
{
266266
if ($this->chunkOffset === 0 && $this->numChunks === 0) {
267267
return false;
@@ -298,7 +298,7 @@ private function initBufferFromCurrentChunk()
298298
* @return boolean Whether there was a next chunk to read
299299
* @throws CorruptFileException if an expected chunk could not be read successfully
300300
*/
301-
private function initBufferFromNextChunk()
301+
private function initBufferFromNextChunk(): bool
302302
{
303303
if ($this->chunkOffset === $this->numChunks - 1) {
304304
return false;
@@ -314,7 +314,7 @@ private function initBufferFromNextChunk()
314314
/**
315315
* Initializes the chunk iterator starting from the current offset.
316316
*/
317-
private function initChunksIterator()
317+
private function initChunksIterator(): void
318318
{
319319
$this->chunksIterator = $this->collectionWrapper->findChunksByFileId($this->file->_id, $this->chunkOffset);
320320
$this->chunksIterator->rewind();

src/GridFS/StreamWrapper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public function stream_write(string $data)
256256
*
257257
* @return array
258258
*/
259-
private function getStatTemplate()
259+
private function getStatTemplate(): array
260260
{
261261
return [
262262
// phpcs:disable Squiz.Arrays.ArrayDeclaration.IndexNoNewline
@@ -283,7 +283,7 @@ private function getStatTemplate()
283283
* @see StreamWrapper::stream_open()
284284
* @param string $path
285285
*/
286-
private function initProtocol(string $path)
286+
private function initProtocol(string $path): void
287287
{
288288
$parts = explode('://', $path, 2);
289289
$this->protocol = $parts[0] ?: 'gridfs';
@@ -295,7 +295,7 @@ private function initProtocol(string $path)
295295
* @see StreamWrapper::stream_open()
296296
* @return boolean
297297
*/
298-
private function initReadableStream()
298+
private function initReadableStream(): bool
299299
{
300300
$context = stream_context_get_options($this->context);
301301

@@ -313,7 +313,7 @@ private function initReadableStream()
313313
* @see StreamWrapper::stream_open()
314314
* @return boolean
315315
*/
316-
private function initWritableStream()
316+
private function initWritableStream(): bool
317317
{
318318
$context = stream_context_get_options($this->context);
319319

src/GridFS/WritableStream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public function writeBytes(string $data)
251251
return $bytesRead;
252252
}
253253

254-
private function abort()
254+
private function abort(): void
255255
{
256256
try {
257257
$this->collectionWrapper->deleteChunksByFilesId($this->file['_id']);
@@ -285,7 +285,7 @@ private function fileCollectionInsert()
285285
return $this->file['_id'];
286286
}
287287

288-
private function insertChunkFromBuffer()
288+
private function insertChunkFromBuffer(): void
289289
{
290290
if (strlen($this->buffer) == 0) {
291291
return;

src/Model/BSONIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ public function rewind()
133133
* @return boolean
134134
*/
135135
#[ReturnTypeWillChange]
136-
public function valid()
136+
public function valid(): bool
137137
{
138138
return $this->current !== null;
139139
}
140140

141-
private function advance()
141+
private function advance(): void
142142
{
143143
if ($this->position === $this->bufferLength) {
144144
return;

src/Model/CachingIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function valid()
155155
/**
156156
* Ensures that the inner iterator is fully consumed and cached.
157157
*/
158-
private function exhaustIterator()
158+
private function exhaustIterator(): void
159159
{
160160
while (! $this->iteratorExhausted) {
161161
$this->next();
@@ -165,7 +165,7 @@ private function exhaustIterator()
165165
/**
166166
* Stores the current item in the cache.
167167
*/
168-
private function storeCurrentItem()
168+
private function storeCurrentItem(): void
169169
{
170170
if (! $this->iterator->valid()) {
171171
return;

src/Model/ChangeStreamIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ private function extractResumeToken($document)
273273
*
274274
* @return boolean
275275
*/
276-
private function isAtEndOfBatch()
276+
private function isAtEndOfBatch(): bool
277277
{
278278
return $this->batchPosition + 1 >= $this->batchSize;
279279
}
@@ -284,7 +284,7 @@ private function isAtEndOfBatch()
284284
* @see https://github.com/mongodb/specifications/blob/master/source/change-streams/change-streams.rst#updating-the-cached-resume-token
285285
* @param boolean $incrementBatchPosition
286286
*/
287-
private function onIteration(bool $incrementBatchPosition)
287+
private function onIteration(bool $incrementBatchPosition): void
288288
{
289289
$this->isValid = parent::valid();
290290

src/Operation/Aggregate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public function getCommandDocument(Server $server)
320320
*
321321
* @return array
322322
*/
323-
private function createCommandDocument()
323+
private function createCommandDocument(): array
324324
{
325325
$cmd = [
326326
'aggregate' => $this->collectionName ?? 1,

src/Operation/BulkWrite.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public function execute(Server $server)
354354
* @see https://php.net/manual/en/mongodb-driver-bulkwrite.construct.php
355355
* @return array
356356
*/
357-
private function createBulkWriteOptions()
357+
private function createBulkWriteOptions(): array
358358
{
359359
$options = ['ordered' => $this->options['ordered']];
360360

@@ -377,7 +377,7 @@ private function createBulkWriteOptions()
377377
* @see https://php.net/manual/en/mongodb-driver-server.executebulkwrite.php
378378
* @return array
379379
*/
380-
private function createExecuteOptions()
380+
private function createExecuteOptions(): array
381381
{
382382
$options = [];
383383

src/Operation/Count.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function getCommandDocument(Server $server)
183183
*
184184
* @return array
185185
*/
186-
private function createCommandDocument()
186+
private function createCommandDocument(): array
187187
{
188188
$cmd = ['count' => $this->collectionName];
189189

@@ -214,7 +214,7 @@ private function createCommandDocument()
214214
* @see https://php.net/manual/en/mongodb-driver-server.executereadcommand.php
215215
* @return array
216216
*/
217-
private function createOptions()
217+
private function createOptions(): array
218218
{
219219
$options = [];
220220

src/Operation/CountDocuments.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,7 @@ public function execute(Server $server)
146146
return (integer) $result->n;
147147
}
148148

149-
/**
150-
* @return Aggregate
151-
*/
152-
private function createAggregate()
149+
private function createAggregate(): Aggregate
153150
{
154151
$pipeline = [
155152
['$match' => (object) $this->filter],

src/Operation/CreateCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public function execute(Server $server)
285285
*
286286
* @return Command
287287
*/
288-
private function createCommand()
288+
private function createCommand(): Command
289289
{
290290
$cmd = ['create' => $this->collectionName];
291291

@@ -310,7 +310,7 @@ private function createCommand()
310310
* @see https://php.net/manual/en/mongodb-driver-server.executewritecommand.php
311311
* @return array
312312
*/
313-
private function createOptions()
313+
private function createOptions(): array
314314
{
315315
$options = [];
316316

src/Operation/CreateIndexes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function execute(Server $server)
160160
* @see https://php.net/manual/en/mongodb-driver-server.executewritecommand.php
161161
* @return array
162162
*/
163-
private function createOptions()
163+
private function createOptions(): array
164164
{
165165
$options = [];
166166

@@ -182,7 +182,7 @@ private function createOptions()
182182
* @param Server $server
183183
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
184184
*/
185-
private function executeCommand(Server $server)
185+
private function executeCommand(Server $server): void
186186
{
187187
$cmd = [
188188
'createIndexes' => $this->collectionName,

src/Operation/DatabaseCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function execute(Server $server)
112112
* @see https://php.net/manual/en/mongodb-driver-server.executecommand.php
113113
* @return array
114114
*/
115-
private function createOptions()
115+
private function createOptions(): array
116116
{
117117
$options = [];
118118

0 commit comments

Comments
 (0)