Skip to content

Commit b0f110f

Browse files
committed
PHPLIB-600: Add type definitions for parameters
1 parent 5d48a1e commit b0f110f

Some content is hidden

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

60 files changed

+128
-122
lines changed

phpcs.xml.dist

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,9 @@
162162
</rule>
163163

164164

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>
165+
<!-- *********************************************************** -->
166+
<!-- Require native type hints for all code without a BC promise -->
167+
<!-- *********************************************************** -->
171168
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint">
172169
<exclude-pattern>src</exclude-pattern>
173170
</rule>

src/ChangeStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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($incrementKey)
227+
private function onIteration(bool $incrementKey)
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

src/Client.php

Lines changed: 5 additions & 5 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

@@ -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;
@@ -314,7 +314,7 @@ public function listDatabases(array $options = [])
314314
* @return Collection
315315
* @throws InvalidArgumentException for parameter/option parsing errors
316316
*/
317-
public function selectCollection($databaseName, $collectionName, array $options = [])
317+
public function selectCollection(string $databaseName, string $collectionName, array $options = [])
318318
{
319319
$options += ['typeMap' => $this->typeMap];
320320

@@ -330,7 +330,7 @@ public function selectCollection($databaseName, $collectionName, array $options
330330
* @return Database
331331
* @throws InvalidArgumentException for parameter/option parsing errors
332332
*/
333-
public function selectDatabase($databaseName, array $options = [])
333+
public function selectDatabase(string $databaseName, array $options = [])
334334
{
335335
$options += ['typeMap' => $this->typeMap];
336336

src/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ 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
{
131131
if (strlen((string) $databaseName) < 1) {
132132
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
@@ -452,7 +452,7 @@ public function deleteOne($filter, array $options = [])
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;

src/Command/ListCollections.php

Lines changed: 1 addition & 1 deletion
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');

src/Database.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Database
104104
* @param array $options Database options
105105
* @throws InvalidArgumentException for parameter/option parsing errors
106106
*/
107-
public function __construct(Manager $manager, $databaseName, array $options = [])
107+
public function __construct(Manager $manager, string $databaseName, array $options = [])
108108
{
109109
if (strlen((string) $databaseName) < 1) {
110110
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
@@ -164,7 +164,7 @@ public function __debugInfo()
164164
* @param string $collectionName Name of the collection to select
165165
* @return Collection
166166
*/
167-
public function __get($collectionName)
167+
public function __get(string $collectionName)
168168
{
169169
return $this->selectCollection($collectionName);
170170
}
@@ -264,7 +264,7 @@ public function command($command, array $options = [])
264264
* @throws InvalidArgumentException for parameter/option parsing errors
265265
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
266266
*/
267-
public function createCollection($collectionName, array $options = [])
267+
public function createCollection(string $collectionName, array $options = [])
268268
{
269269
if (! isset($options['typeMap'])) {
270270
$options['typeMap'] = $this->typeMap;
@@ -340,7 +340,7 @@ public function drop(array $options = [])
340340
* @throws InvalidArgumentException for parameter/option parsing errors
341341
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
342342
*/
343-
public function dropCollection($collectionName, array $options = [])
343+
public function dropCollection(string $collectionName, array $options = [])
344344
{
345345
if (! isset($options['typeMap'])) {
346346
$options['typeMap'] = $this->typeMap;
@@ -477,7 +477,7 @@ public function listCollections(array $options = [])
477477
* @throws InvalidArgumentException for parameter/option parsing errors
478478
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
479479
*/
480-
public function modifyCollection($collectionName, array $collectionOptions, array $options = [])
480+
public function modifyCollection(string $collectionName, array $collectionOptions, array $options = [])
481481
{
482482
if (! isset($options['typeMap'])) {
483483
$options['typeMap'] = $this->typeMap;
@@ -537,7 +537,7 @@ public function renameCollection(string $fromCollectionName, string $toCollectio
537537
* @return Collection
538538
* @throws InvalidArgumentException for parameter/option parsing errors
539539
*/
540-
public function selectCollection($collectionName, array $options = [])
540+
public function selectCollection(string $collectionName, array $options = [])
541541
{
542542
$options += [
543543
'readConcern' => $this->readConcern,

src/Exception/BadMethodCallException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class BadMethodCallException extends BaseBadMethodCallException implements Excep
2929
* @param string $class Class name
3030
* @return self
3131
*/
32-
public static function classIsImmutable($class)
32+
public static function classIsImmutable(string $class)
3333
{
3434
return new static(sprintf('%s is immutable', $class));
3535
}
@@ -40,7 +40,7 @@ public static function classIsImmutable($class)
4040
* @param string $method Method name
4141
* @return self
4242
*/
43-
public static function unacknowledgedWriteResultAccess($method)
43+
public static function unacknowledgedWriteResultAccess(string $method)
4444
{
4545
return new static(sprintf('%s should not be called for an unacknowledged write result', $method));
4646
}

src/Exception/InvalidArgumentException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class InvalidArgumentException extends DriverInvalidArgumentException implements
3636
* @param string|string[] $expectedType Expected type
3737
* @return self
3838
*/
39-
public static function invalidType($name, $value, $expectedType)
39+
public static function invalidType(string $name, $value, $expectedType)
4040
{
4141
if (is_array($expectedType)) {
4242
switch (count($expectedType)) {

src/GridFS/Bucket.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class Bucket
137137
* @param array $options Bucket options
138138
* @throws InvalidArgumentException for parameter/option parsing errors
139139
*/
140-
public function __construct(Manager $manager, $databaseName, array $options = [])
140+
public function __construct(Manager $manager, string $databaseName, array $options = [])
141141
{
142142
$options += [
143143
'bucketName' => self::$defaultBucketName,
@@ -282,7 +282,7 @@ public function downloadToStream($id, $destination)
282282
* @throws StreamException if the file could not be uploaded
283283
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
284284
*/
285-
public function downloadToStreamByName($filename, $destination, array $options = [])
285+
public function downloadToStreamByName(string $filename, $destination, array $options = [])
286286
{
287287
if (! is_resource($destination) || get_resource_type($destination) != "stream") {
288288
throw InvalidArgumentException::invalidType('$destination', $destination, 'resource');
@@ -517,7 +517,7 @@ public function openDownloadStream($id)
517517
* @throws FileNotFoundException if no file could be selected
518518
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
519519
*/
520-
public function openDownloadStreamByName($filename, array $options = [])
520+
public function openDownloadStreamByName(string $filename, array $options = [])
521521
{
522522
$options += ['revision' => -1];
523523

@@ -550,7 +550,7 @@ public function openDownloadStreamByName($filename, array $options = [])
550550
* @param array $options Upload options
551551
* @return resource
552552
*/
553-
public function openUploadStream($filename, array $options = [])
553+
public function openUploadStream(string $filename, array $options = [])
554554
{
555555
$options += ['chunkSizeBytes' => $this->chunkSizeBytes];
556556

@@ -574,7 +574,7 @@ public function openUploadStream($filename, array $options = [])
574574
* @throws FileNotFoundException if no file could be selected
575575
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
576576
*/
577-
public function rename($id, $newFilename)
577+
public function rename($id, string $newFilename)
578578
{
579579
$updateResult = $this->collectionWrapper->updateFilenameForId($id, $newFilename);
580580

@@ -620,7 +620,7 @@ public function rename($id, $newFilename)
620620
* @throws StreamException if the file could not be uploaded
621621
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
622622
*/
623-
public function uploadFromStream($filename, $source, array $options = [])
623+
public function uploadFromStream(string $filename, $source, array $options = [])
624624
{
625625
if (! is_resource($source) || get_resource_type($source) != "stream") {
626626
throw InvalidArgumentException::invalidType('$source', $source, 'resource');

src/GridFS/CollectionWrapper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CollectionWrapper
6464
* @param array $collectionOptions Collection options
6565
* @throws InvalidArgumentException
6666
*/
67-
public function __construct(Manager $manager, $databaseName, $bucketName, array $collectionOptions = [])
67+
public function __construct(Manager $manager, string $databaseName, string $bucketName, array $collectionOptions = [])
6868
{
6969
$this->databaseName = (string) $databaseName;
7070
$this->bucketName = (string) $bucketName;
@@ -110,7 +110,7 @@ public function dropCollections()
110110
* @param integer $fromChunk Starting chunk (inclusive)
111111
* @return Cursor
112112
*/
113-
public function findChunksByFileId($id, $fromChunk = 0)
113+
public function findChunksByFileId($id, int $fromChunk = 0)
114114
{
115115
return $this->chunksCollection->find(
116116
[
@@ -142,7 +142,7 @@ public function findChunksByFileId($id, $fromChunk = 0)
142142
* @param integer $revision
143143
* @return stdClass|null
144144
*/
145-
public function findFileByFilenameAndRevision($filename, $revision)
145+
public function findFileByFilenameAndRevision(string $filename, int $revision)
146146
{
147147
$filename = (string) $filename;
148148
$revision = (integer) $revision;
@@ -281,7 +281,7 @@ public function insertFile($file)
281281
* @param string $filename
282282
* @return UpdateResult
283283
*/
284-
public function updateFilenameForId($id, $filename)
284+
public function updateFilenameForId($id, string $filename)
285285
{
286286
return $this->filesCollection->updateOne(
287287
['_id' => $id],

src/GridFS/Exception/CorruptFileException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CorruptFileException extends RuntimeException
2929
* @param integer $expectedIndex Expected index number
3030
* @return self
3131
*/
32-
public static function missingChunk($expectedIndex)
32+
public static function missingChunk(int $expectedIndex)
3333
{
3434
return new static(sprintf('Chunk not found for index "%d"', $expectedIndex));
3535
}
@@ -41,7 +41,7 @@ public static function missingChunk($expectedIndex)
4141
* @param integer $expectedIndex Expected index number
4242
* @return self
4343
*/
44-
public static function unexpectedIndex($index, $expectedIndex)
44+
public static function unexpectedIndex(int $index, int $expectedIndex)
4545
{
4646
return new static(sprintf('Expected chunk to have index "%d" but found "%d"', $expectedIndex, $index));
4747
}
@@ -53,7 +53,7 @@ public static function unexpectedIndex($index, $expectedIndex)
5353
* @param integer $expectedSize Expected size
5454
* @return self
5555
*/
56-
public static function unexpectedSize($size, $expectedSize)
56+
public static function unexpectedSize(int $size, int $expectedSize)
5757
{
5858
return new static(sprintf('Expected chunk to have size "%d" but found "%d"', $expectedSize, $size));
5959
}

src/GridFS/Exception/FileNotFoundException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class FileNotFoundException extends RuntimeException
3333
* @param string $namespace Namespace for the files collection
3434
* @return self
3535
*/
36-
public static function byFilenameAndRevision($filename, $revision, $namespace)
36+
public static function byFilenameAndRevision(string $filename, int $revision, string $namespace)
3737
{
3838
return new static(sprintf('File with name "%s" and revision "%d" not found in "%s"', $filename, $revision, $namespace));
3939
}
@@ -45,7 +45,7 @@ public static function byFilenameAndRevision($filename, $revision, $namespace)
4545
* @param string $namespace Namespace for the files collection
4646
* @return self
4747
*/
48-
public static function byId($id, $namespace)
48+
public static function byId($id, string $namespace)
4949
{
5050
$json = toJSON(fromPHP(['_id' => $id]));
5151

src/GridFS/ReadableStream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function isEOF()
164164
* @return string
165165
* @throws InvalidArgumentException if $length is negative
166166
*/
167-
public function readBytes($length)
167+
public function readBytes(int $length)
168168
{
169169
if ($length < 0) {
170170
throw new InvalidArgumentException(sprintf('$length must be >= 0; given: %d', $length));
@@ -199,7 +199,7 @@ public function readBytes($length)
199199
* @param integer $offset
200200
* @throws InvalidArgumentException if $offset is out of range
201201
*/
202-
public function seek($offset)
202+
public function seek(int $offset)
203203
{
204204
if ($offset < 0 || $offset > $this->file->length) {
205205
throw new InvalidArgumentException(sprintf('$offset must be >= 0 and <= %d; given: %d', $this->file->length, $offset));

src/GridFS/StreamWrapper.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function getFile()
7777
*
7878
* @param string $protocol Protocol to use for stream_wrapper_register()
7979
*/
80-
public static function register($protocol = 'gridfs')
80+
public static function register(string $protocol = 'gridfs')
8181
{
8282
if (in_array($protocol, stream_get_wrappers())) {
8383
stream_wrapper_unregister($protocol);
@@ -119,13 +119,13 @@ public function stream_eof()
119119
* Opens the stream.
120120
*
121121
* @see https://php.net/manual/en/streamwrapper.stream-open.php
122-
* @param string $path Path to the file resource
123-
* @param string $mode Mode used to open the file (only "r" and "w" are supported)
124-
* @param integer $options Additional flags set by the streams API
125-
* @param string $openedPath Not used
122+
* @param string $path Path to the file resource
123+
* @param string $mode Mode used to open the file (only "r" and "w" are supported)
124+
* @param integer $options Additional flags set by the streams API
125+
* @param string|null $openedPath Not used
126126
* @return boolean
127127
*/
128-
public function stream_open($path, $mode, $options, &$openedPath)
128+
public function stream_open(string $path, string $mode, int $options, ?string &$openedPath)
129129
{
130130
$this->initProtocol($path);
131131
$this->mode = $mode;
@@ -151,7 +151,7 @@ public function stream_open($path, $mode, $options, &$openedPath)
151151
* @param integer $length Number of bytes to read
152152
* @return string
153153
*/
154-
public function stream_read($length)
154+
public function stream_read(int $length)
155155
{
156156
if (! $this->stream instanceof ReadableStream) {
157157
return '';
@@ -168,7 +168,7 @@ public function stream_read($length)
168168
* @param integer $whence One of SEEK_SET, SEEK_CUR, or SEEK_END
169169
* @return boolean True if the position was updated and false otherwise
170170
*/
171-
public function stream_seek($offset, $whence = SEEK_SET)
171+
public function stream_seek(int $offset, int $whence = SEEK_SET)
172172
{
173173
$size = $this->stream->getSize();
174174

@@ -242,7 +242,7 @@ public function stream_tell()
242242
* @param string $data Data to write
243243
* @return integer The number of bytes written
244244
*/
245-
public function stream_write($data)
245+
public function stream_write(string $data)
246246
{
247247
if (! $this->stream instanceof WritableStream) {
248248
return 0;
@@ -283,7 +283,7 @@ private function getStatTemplate()
283283
* @see StreamWrapper::stream_open()
284284
* @param string $path
285285
*/
286-
private function initProtocol($path)
286+
private function initProtocol(string $path)
287287
{
288288
$parts = explode('://', $path, 2);
289289
$this->protocol = $parts[0] ?: 'gridfs';

0 commit comments

Comments
 (0)