Skip to content

PHPLIB-599: Typing improvements #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@
<!-- **************************************************************************** -->
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
<properties>
<!-- Requires PHP 7.4 -->
<property name="enableObjectTypeHint" value="false" />
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment was wrong - the object type was introduced in PHP 7.2.

<!-- Requires PHP 8.0 -->
<property name="enableMixedTypeHint" value="false" />
<!-- Requires PHP 8.0 -->
Expand All @@ -133,8 +131,6 @@

<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
<properties>
<!-- Requires PHP 7.2 -->
<property name="enableObjectTypeHint" value="false" />
<!-- Requires PHP 8.0 -->
<property name="enableStaticTypeHint" value="false" />
<!-- Requires PHP 8.0 -->
Expand Down Expand Up @@ -162,12 +158,9 @@
</rule>


<!-- *********************************************************************************** -->
<!-- Require native type hints for all parameters, properties, and return types in tests -->
<!-- *********************************************************************************** -->
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint">
<exclude-pattern>src</exclude-pattern>
</rule>
<!-- *********************************************************** -->
<!-- Require native type hints for all code without a BC promise -->
<!-- *********************************************************** -->
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint">
<exclude-pattern>src</exclude-pattern>
</rule>
Expand Down
8 changes: 4 additions & 4 deletions src/ChangeStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function valid()
* @param RuntimeException $exception
* @return boolean
*/
private function isResumableError(RuntimeException $exception)
private function isResumableError(RuntimeException $exception): bool
{
if ($exception instanceof ConnectionException) {
return true;
Expand All @@ -224,7 +224,7 @@ private function isResumableError(RuntimeException $exception)
* @param boolean $incrementKey Increment $key if there is a current result
* @throws ResumeTokenException
*/
private function onIteration($incrementKey)
private function onIteration(bool $incrementKey): void
{
/* If the cursorId is 0, the server has invalidated the cursor and we
* will never perform another getMore nor need to resume since any
Expand Down Expand Up @@ -254,7 +254,7 @@ private function onIteration($incrementKey)
*
* @return void
*/
private function resume()
private function resume(): void
{
$this->iterator = call_user_func($this->resumeCallable, $this->getResumeToken(), $this->hasAdvanced);
$this->iterator->rewind();
Expand All @@ -268,7 +268,7 @@ private function resume()
* @param RuntimeException $exception
* @throws RuntimeException
*/
private function resumeOrThrow(RuntimeException $exception)
private function resumeOrThrow(RuntimeException $exception): void
{
if ($this->isResumableError($exception)) {
$this->resume();
Expand Down
10 changes: 5 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Client
* @throws DriverInvalidArgumentException for parameter/option parsing errors in the driver
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function __construct($uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])
public function __construct(string $uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])
{
$driverOptions += ['typeMap' => self::$defaultTypeMap];

Expand Down Expand Up @@ -155,7 +155,7 @@ public function __debugInfo()
* @param string $databaseName Name of the database to select
* @return Database
*/
public function __get($databaseName)
public function __get(string $databaseName)
{
return $this->selectDatabase($databaseName);
}
Expand Down Expand Up @@ -201,7 +201,7 @@ public function createClientEncryption(array $options)
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function dropDatabase($databaseName, array $options = [])
public function dropDatabase(string $databaseName, array $options = [])
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
Expand Down Expand Up @@ -314,7 +314,7 @@ public function listDatabases(array $options = [])
* @return Collection
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function selectCollection($databaseName, $collectionName, array $options = [])
public function selectCollection(string $databaseName, string $collectionName, array $options = [])
{
$options += ['typeMap' => $this->typeMap];

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

Expand Down
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Collection
* @param array $options Collection options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [])
public function __construct(Manager $manager, string $databaseName, string $collectionName, array $options = [])
{
if (strlen((string) $databaseName) < 1) {
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
Expand Down Expand Up @@ -452,7 +452,7 @@ public function deleteOne($filter, array $options = [])
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function distinct($fieldName, $filter = [], array $options = [])
public function distinct(string $fieldName, $filter = [], array $options = [])
{
if (! isset($options['readPreference']) && ! is_in_transaction($options)) {
$options['readPreference'] = $this->readPreference;
Expand Down
6 changes: 3 additions & 3 deletions src/Command/ListCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ListCollections implements Executable
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function __construct($databaseName, array $options = [])
public function __construct(string $databaseName, array $options = [])
{
if (isset($options['authorizedCollections']) && ! is_bool($options['authorizedCollections'])) {
throw InvalidArgumentException::invalidType('"authorizedCollections" option', $options['authorizedCollections'], 'boolean');
Expand Down Expand Up @@ -120,7 +120,7 @@ public function execute(Server $server)
*
* @return Command
*/
private function createCommand()
private function createCommand(): Command
{
$cmd = ['listCollections' => 1];

Expand All @@ -146,7 +146,7 @@ private function createCommand()
* @see https://php.net/manual/en/mongodb-driver-server.executecommand.php
* @return array
*/
private function createOptions()
private function createOptions(): array
{
$options = [];

Expand Down
4 changes: 2 additions & 2 deletions src/Command/ListDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function execute(Server $server)
*
* @return Command
*/
private function createCommand()
private function createCommand(): Command
{
$cmd = ['listDatabases' => 1];

Expand All @@ -148,7 +148,7 @@ private function createCommand()
* @see https://php.net/manual/en/mongodb-driver-server.executecommand.php
* @return array
*/
private function createOptions()
private function createOptions(): array
{
$options = [];

Expand Down
12 changes: 6 additions & 6 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Database
* @param array $options Database options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function __construct(Manager $manager, $databaseName, array $options = [])
public function __construct(Manager $manager, string $databaseName, array $options = [])
{
if (strlen((string) $databaseName) < 1) {
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
Expand Down Expand Up @@ -164,7 +164,7 @@ public function __debugInfo()
* @param string $collectionName Name of the collection to select
* @return Collection
*/
public function __get($collectionName)
public function __get(string $collectionName)
{
return $this->selectCollection($collectionName);
}
Expand Down Expand Up @@ -264,7 +264,7 @@ public function command($command, array $options = [])
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function createCollection($collectionName, array $options = [])
public function createCollection(string $collectionName, array $options = [])
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
Expand Down Expand Up @@ -340,7 +340,7 @@ public function drop(array $options = [])
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function dropCollection($collectionName, array $options = [])
public function dropCollection(string $collectionName, array $options = [])
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
Expand Down Expand Up @@ -477,7 +477,7 @@ public function listCollections(array $options = [])
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function modifyCollection($collectionName, array $collectionOptions, array $options = [])
public function modifyCollection(string $collectionName, array $collectionOptions, array $options = [])
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
Expand Down Expand Up @@ -537,7 +537,7 @@ public function renameCollection(string $fromCollectionName, string $toCollectio
* @return Collection
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function selectCollection($collectionName, array $options = [])
public function selectCollection(string $collectionName, array $options = [])
{
$options += [
'readConcern' => $this->readConcern,
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BadMethodCallException extends BaseBadMethodCallException implements Excep
* @param string $class Class name
* @return self
*/
public static function classIsImmutable($class)
public static function classIsImmutable(string $class)
{
return new static(sprintf('%s is immutable', $class));
}
Expand All @@ -40,7 +40,7 @@ public static function classIsImmutable($class)
* @param string $method Method name
* @return self
*/
public static function unacknowledgedWriteResultAccess($method)
public static function unacknowledgedWriteResultAccess(string $method)
{
return new static(sprintf('%s should not be called for an unacknowledged write result', $method));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class InvalidArgumentException extends DriverInvalidArgumentException implements
* @param string|string[] $expectedType Expected type
* @return self
*/
public static function invalidType($name, $value, $expectedType)
public static function invalidType(string $name, $value, $expectedType)
{
if (is_array($expectedType)) {
switch (count($expectedType)) {
Expand Down
22 changes: 11 additions & 11 deletions src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Bucket
* @param array $options Bucket options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function __construct(Manager $manager, $databaseName, array $options = [])
public function __construct(Manager $manager, string $databaseName, array $options = [])
{
$options += [
'bucketName' => self::$defaultBucketName,
Expand Down Expand Up @@ -282,7 +282,7 @@ public function downloadToStream($id, $destination)
* @throws StreamException if the file could not be uploaded
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function downloadToStreamByName($filename, $destination, array $options = [])
public function downloadToStreamByName(string $filename, $destination, array $options = [])
{
if (! is_resource($destination) || get_resource_type($destination) != "stream") {
throw InvalidArgumentException::invalidType('$destination', $destination, 'resource');
Expand Down Expand Up @@ -517,7 +517,7 @@ public function openDownloadStream($id)
* @throws FileNotFoundException if no file could be selected
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function openDownloadStreamByName($filename, array $options = [])
public function openDownloadStreamByName(string $filename, array $options = [])
{
$options += ['revision' => -1];

Expand Down Expand Up @@ -550,7 +550,7 @@ public function openDownloadStreamByName($filename, array $options = [])
* @param array $options Upload options
* @return resource
*/
public function openUploadStream($filename, array $options = [])
public function openUploadStream(string $filename, array $options = [])
{
$options += ['chunkSizeBytes' => $this->chunkSizeBytes];

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

Expand Down Expand Up @@ -620,7 +620,7 @@ public function rename($id, $newFilename)
* @throws StreamException if the file could not be uploaded
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function uploadFromStream($filename, $source, array $options = [])
public function uploadFromStream(string $filename, $source, array $options = [])
{
if (! is_resource($source) || get_resource_type($source) != "stream") {
throw InvalidArgumentException::invalidType('$source', $source, 'resource');
Expand All @@ -643,7 +643,7 @@ public function uploadFromStream($filename, $source, array $options = [])
* @param stdClass $file GridFS file document
* @return string
*/
private function createPathForFile(stdClass $file)
private function createPathForFile(stdClass $file): string
{
if (! is_object($file->_id) || method_exists($file->_id, '__toString')) {
$id = (string) $file->_id;
Expand All @@ -665,7 +665,7 @@ private function createPathForFile(stdClass $file)
*
* @return string
*/
private function createPathForUpload()
private function createPathForUpload(): string
{
return sprintf(
'%s://%s/%s.files',
Expand All @@ -680,7 +680,7 @@ private function createPathForUpload()
*
* @return string
*/
private function getFilesNamespace()
private function getFilesNamespace(): string
{
return sprintf('%s.%s.files', $this->databaseName, $this->bucketName);
}
Expand All @@ -695,7 +695,7 @@ private function getFilesNamespace()
* @return stdClass
* @throws InvalidArgumentException
*/
private function getRawFileDocumentForStream($stream)
private function getRawFileDocumentForStream($stream): stdClass
{
if (! is_resource($stream) || get_resource_type($stream) != "stream") {
throw InvalidArgumentException::invalidType('$stream', $stream, 'resource');
Expand Down Expand Up @@ -732,7 +732,7 @@ private function openDownloadStreamByFile(stdClass $file)
/**
* Registers the GridFS stream wrapper if it is not already registered.
*/
private function registerStreamWrapper()
private function registerStreamWrapper(): void
{
if (in_array(self::$streamWrapperProtocol, stream_get_wrappers())) {
return;
Expand Down
Loading