Skip to content

Commit 1b29a6d

Browse files
authored
PHPLIB-599: Typing improvements (#959)
* PHPLIB-600: Add type definitions for parameters * PHPLIB-602: Add return type definitions where applicable * Enable object type for parameters * Add comment about using declare * Remove useless param and return annotations * Remove redundant type casts * Add return types to internal classes * Use object consistently in GridFS classes * Remove useless type annotations * Remove unnecessary cast
1 parent 1e20e8d commit 1b29a6d

File tree

105 files changed

+351
-855
lines changed

Some content is hidden

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

105 files changed

+351
-855
lines changed

phpcs.xml.dist

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,13 @@
105105
<!-- **************************************************************************** -->
106106
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
107107
<properties>
108-
<!-- Requires PHP 7.4 -->
109-
<property name="enableObjectTypeHint" value="false" />
110108
<!-- Requires PHP 8.0 -->
111109
<property name="enableMixedTypeHint" value="false" />
112110
<!-- Requires PHP 8.0 -->
113111
<property name="enableUnionTypeHint" value="false" />
114112
</properties>
115113

116114
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification" />
117-
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.UselessAnnotation" />
118115
</rule>
119116

120117
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
@@ -128,13 +125,10 @@
128125
</properties>
129126

130127
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification" />
131-
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.UselessAnnotation" />
132128
</rule>
133129

134130
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
135131
<properties>
136-
<!-- Requires PHP 7.2 -->
137-
<property name="enableObjectTypeHint" value="false" />
138132
<!-- Requires PHP 8.0 -->
139133
<property name="enableStaticTypeHint" value="false" />
140134
<!-- Requires PHP 8.0 -->
@@ -144,7 +138,6 @@
144138
</properties>
145139

146140
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification" />
147-
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation" />
148141
</rule>
149142

150143

@@ -162,12 +155,9 @@
162155
</rule>
163156

164157

165-
<!-- *********************************************************************************** -->
166-
<!-- Require native type hints for all parameters, properties, and return types in tests -->
167-
<!-- *********************************************************************************** -->
168-
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint">
169-
<exclude-pattern>src</exclude-pattern>
170-
</rule>
158+
<!-- *********************************************************** -->
159+
<!-- Require native type hints for all code without a BC promise -->
160+
<!-- *********************************************************** -->
171161
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint">
172162
<exclude-pattern>src</exclude-pattern>
173163
</rule>

src/BulkWriteResult.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@ class BulkWriteResult
2828
/** @var WriteResult */
2929
private $writeResult;
3030

31-
/** @var mixed[] */
31+
/** @var array */
3232
private $insertedIds;
3333

3434
/** @var boolean */
3535
private $isAcknowledged;
3636

37-
/**
38-
* @param WriteResult $writeResult
39-
* @param mixed[] $insertedIds
40-
*/
4137
public function __construct(WriteResult $writeResult, array $insertedIds)
4238
{
4339
$this->writeResult = $writeResult;
@@ -90,7 +86,7 @@ public function getInsertedCount()
9086
* field value. Any driver-generated ID will be a MongoDB\BSON\ObjectId
9187
* instance.
9288
*
93-
* @return mixed[]
89+
* @return array
9490
*/
9591
public function getInsertedIds()
9692
{
@@ -165,7 +161,7 @@ public function getUpsertedCount()
165161
* This method should only be called if the write was acknowledged.
166162
*
167163
* @see BulkWriteResult::isAcknowledged()
168-
* @return mixed[]
164+
* @return array
169165
* @throws BadMethodCallException is the write result is unacknowledged
170166
*/
171167
public function getUpsertedIds()

src/ChangeStream.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ class ChangeStream implements Iterator
9090

9191
/**
9292
* @internal
93-
* @param ChangeStreamIterator $iterator
94-
* @param callable $resumeCallable
9593
*/
9694
public function __construct(ChangeStreamIterator $iterator, callable $resumeCallable)
9795
{
@@ -194,10 +192,8 @@ public function valid()
194192
* Determines if an exception is a resumable error.
195193
*
196194
* @see https://github.com/mongodb/specifications/blob/master/source/change-streams/change-streams.rst#resumable-error
197-
* @param RuntimeException $exception
198-
* @return boolean
199195
*/
200-
private function isResumableError(RuntimeException $exception)
196+
private function isResumableError(RuntimeException $exception): bool
201197
{
202198
if ($exception instanceof ConnectionException) {
203199
return true;
@@ -224,7 +220,7 @@ private function isResumableError(RuntimeException $exception)
224220
* @param boolean $incrementKey Increment $key if there is a current result
225221
* @throws ResumeTokenException
226222
*/
227-
private function onIteration($incrementKey)
223+
private function onIteration(bool $incrementKey): void
228224
{
229225
/* If the cursorId is 0, the server has invalidated the cursor and we
230226
* will never perform another getMore nor need to resume since any
@@ -251,10 +247,8 @@ private function onIteration($incrementKey)
251247

252248
/**
253249
* Recreates the ChangeStreamIterator after a resumable server error.
254-
*
255-
* @return void
256250
*/
257-
private function resume()
251+
private function resume(): void
258252
{
259253
$this->iterator = call_user_func($this->resumeCallable, $this->getResumeToken(), $this->hasAdvanced);
260254
$this->iterator->rewind();
@@ -265,10 +259,9 @@ private function resume()
265259
/**
266260
* Either resumes after a resumable error or re-throws the exception.
267261
*
268-
* @param RuntimeException $exception
269262
* @throws RuntimeException
270263
*/
271-
private function resumeOrThrow(RuntimeException $exception)
264+
private function resumeOrThrow(RuntimeException $exception): void
272265
{
273266
if ($this->isResumableError($exception)) {
274267
$this->resume();

src/Client.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Client
9898
* @throws DriverInvalidArgumentException for parameter/option parsing errors in the driver
9999
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
100100
*/
101-
public function __construct($uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])
101+
public function __construct(string $uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])
102102
{
103103
$driverOptions += ['typeMap' => self::$defaultTypeMap];
104104

@@ -116,7 +116,7 @@ public function __construct($uri = 'mongodb://127.0.0.1/', array $uriOptions = [
116116

117117
$driverOptions['driver'] = $this->mergeDriverInfo($driverOptions['driver'] ?? []);
118118

119-
$this->uri = (string) $uri;
119+
$this->uri = $uri;
120120
$this->typeMap = $driverOptions['typeMap'] ?? null;
121121

122122
unset($driverOptions['typeMap']);
@@ -155,7 +155,7 @@ public function __debugInfo()
155155
* @param string $databaseName Name of the database to select
156156
* @return Database
157157
*/
158-
public function __get($databaseName)
158+
public function __get(string $databaseName)
159159
{
160160
return $this->selectDatabase($databaseName);
161161
}
@@ -201,7 +201,7 @@ public function createClientEncryption(array $options)
201201
* @throws InvalidArgumentException for parameter/option parsing errors
202202
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
203203
*/
204-
public function dropDatabase($databaseName, array $options = [])
204+
public function dropDatabase(string $databaseName, array $options = [])
205205
{
206206
if (! isset($options['typeMap'])) {
207207
$options['typeMap'] = $this->typeMap;
@@ -290,7 +290,6 @@ public function listDatabaseNames(array $options = []): Iterator
290290
* List databases.
291291
*
292292
* @see ListDatabases::__construct() for supported options
293-
* @param array $options
294293
* @return DatabaseInfoIterator
295294
* @throws UnexpectedValueException if the command response was malformed
296295
* @throws InvalidArgumentException for parameter/option parsing errors
@@ -314,7 +313,7 @@ public function listDatabases(array $options = [])
314313
* @return Collection
315314
* @throws InvalidArgumentException for parameter/option parsing errors
316315
*/
317-
public function selectCollection($databaseName, $collectionName, array $options = [])
316+
public function selectCollection(string $databaseName, string $collectionName, array $options = [])
318317
{
319318
$options += ['typeMap' => $this->typeMap];
320319

@@ -330,7 +329,7 @@ public function selectCollection($databaseName, $collectionName, array $options
330329
* @return Database
331330
* @throws InvalidArgumentException for parameter/option parsing errors
332331
*/
333-
public function selectDatabase($databaseName, array $options = [])
332+
public function selectDatabase(string $databaseName, array $options = [])
334333
{
335334
$options += ['typeMap' => $this->typeMap];
336335

src/Collection.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ class Collection
126126
* @param array $options Collection options
127127
* @throws InvalidArgumentException for parameter/option parsing errors
128128
*/
129-
public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [])
129+
public function __construct(Manager $manager, string $databaseName, string $collectionName, array $options = [])
130130
{
131-
if (strlen((string) $databaseName) < 1) {
131+
if (strlen($databaseName) < 1) {
132132
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
133133
}
134134

135-
if (strlen((string) $collectionName) < 1) {
135+
if (strlen($collectionName) < 1) {
136136
throw new InvalidArgumentException('$collectionName is invalid: ' . $collectionName);
137137
}
138138

@@ -153,8 +153,8 @@ public function __construct(Manager $manager, $databaseName, $collectionName, ar
153153
}
154154

155155
$this->manager = $manager;
156-
$this->databaseName = (string) $databaseName;
157-
$this->collectionName = (string) $collectionName;
156+
$this->databaseName = $databaseName;
157+
$this->collectionName = $collectionName;
158158
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
159159
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();
160160
$this->typeMap = $options['typeMap'] ?? self::$defaultTypeMap;
@@ -446,13 +446,13 @@ public function deleteOne($filter, array $options = [])
446446
* @param string $fieldName Field for which to return distinct values
447447
* @param array|object $filter Query by which to filter documents
448448
* @param array $options Command options
449-
* @return mixed[]
449+
* @return array
450450
* @throws UnexpectedValueException if the command response was malformed
451451
* @throws UnsupportedException if options are not supported by the selected server
452452
* @throws InvalidArgumentException for parameter/option parsing errors
453453
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
454454
*/
455-
public function distinct($fieldName, $filter = [], array $options = [])
455+
public function distinct(string $fieldName, $filter = [], array $options = [])
456456
{
457457
if (! isset($options['readPreference']) && ! is_in_transaction($options)) {
458458
$options['readPreference'] = $this->readPreference;
@@ -938,7 +938,6 @@ public function insertOne($document, array $options = [])
938938
* Returns information for all indexes for the collection.
939939
*
940940
* @see ListIndexes::__construct() for supported options
941-
* @param array $options
942941
* @return IndexInfoIterator
943942
* @throws InvalidArgumentException for parameter/option parsing errors
944943
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
@@ -1007,9 +1006,9 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
10071006
* Renames the collection.
10081007
*
10091008
* @see RenameCollection::__construct() for supported options
1010-
* @param string $toCollectionName New name of the collection
1011-
* @param ?string $toDatabaseName New database name of the collection. Defaults to the original database.
1012-
* @param array $options Additional options
1009+
* @param string $toCollectionName New name of the collection
1010+
* @param string|null $toDatabaseName New database name of the collection. Defaults to the original database.
1011+
* @param array $options Additional options
10131012
* @return array|object Command result document
10141013
* @throws UnsupportedException if options are not supported by the selected server
10151014
* @throws InvalidArgumentException for parameter/option parsing errors

src/Command/ListCollections.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ListCollections implements Executable
7373
* @param array $options Command options
7474
* @throws InvalidArgumentException for parameter/option parsing errors
7575
*/
76-
public function __construct($databaseName, array $options = [])
76+
public function __construct(string $databaseName, array $options = [])
7777
{
7878
if (isset($options['authorizedCollections']) && ! is_bool($options['authorizedCollections'])) {
7979
throw InvalidArgumentException::invalidType('"authorizedCollections" option', $options['authorizedCollections'], 'boolean');
@@ -95,19 +95,17 @@ public function __construct($databaseName, array $options = [])
9595
throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
9696
}
9797

98-
$this->databaseName = (string) $databaseName;
98+
$this->databaseName = $databaseName;
9999
$this->options = $options;
100100
}
101101

102102
/**
103103
* Execute the operation.
104104
*
105105
* @see Executable::execute()
106-
* @param Server $server
107-
* @return CachingIterator
108106
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
109107
*/
110-
public function execute(Server $server)
108+
public function execute(Server $server): CachingIterator
111109
{
112110
$cursor = $server->executeReadCommand($this->databaseName, $this->createCommand(), $this->createOptions());
113111
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
@@ -117,10 +115,8 @@ public function execute(Server $server)
117115

118116
/**
119117
* Create the listCollections command.
120-
*
121-
* @return Command
122118
*/
123-
private function createCommand()
119+
private function createCommand(): Command
124120
{
125121
$cmd = ['listCollections' => 1];
126122

@@ -144,9 +140,8 @@ private function createCommand()
144140
* the command be executed on the primary.
145141
*
146142
* @see https://php.net/manual/en/mongodb-driver-server.executecommand.php
147-
* @return array
148143
*/
149-
private function createOptions()
144+
private function createOptions(): array
150145
{
151146
$options = [];
152147

src/Command/ListDatabases.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ public function __construct(array $options = [])
9999
* Execute the operation.
100100
*
101101
* @see Executable::execute()
102-
* @param Server $server
103102
* @return array An array of database info structures
104103
* @throws UnexpectedValueException if the command response was malformed
105104
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
106105
*/
107-
public function execute(Server $server)
106+
public function execute(Server $server): array
108107
{
109108
$cursor = $server->executeReadCommand('admin', $this->createCommand(), $this->createOptions());
110109
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
@@ -119,10 +118,8 @@ public function execute(Server $server)
119118

120119
/**
121120
* Create the listDatabases command.
122-
*
123-
* @return Command
124121
*/
125-
private function createCommand()
122+
private function createCommand(): Command
126123
{
127124
$cmd = ['listDatabases' => 1];
128125

@@ -146,9 +143,8 @@ private function createCommand()
146143
* the command be executed on the primary.
147144
*
148145
* @see https://php.net/manual/en/mongodb-driver-server.executecommand.php
149-
* @return array
150146
*/
151-
private function createOptions()
147+
private function createOptions(): array
152148
{
153149
$options = [];
154150

0 commit comments

Comments
 (0)