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

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Aug 24, 2022

This PR handles PHPLIB-600 and PHPLIB-602, adding parameter types and return types where applicable.

Rules for phpcs have been updated to require argument types; no changes were made to return types as most of the classes are non-final (something we may want to address for 2.0).

@alcaeus alcaeus requested review from jmikola and levon80999 August 24, 2022 10:35
@alcaeus alcaeus self-assigned this Aug 24, 2022
@alcaeus alcaeus force-pushed the feature/phplib-599-typing branch from a2e2ee7 to 68d68e3 Compare August 24, 2022 10:38
@@ -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.

// TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
new Collection($this->manager, $this->getDatabaseName(), $collectionName);
}

public function provideInvalidDatabaseAndCollectionNames()
{
return [
[null],
[''],
[null, TypeError::class],
Copy link
Member Author

Choose a reason for hiding this comment

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

Adding the expected exception type to the dataset was necessary as null is now caught by PHP itself, while the empty string is caught by our logic.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

Were most of these code changes manually done, or is this something phpcbf helped with after updating phpcs.xml.dist?

@alcaeus
Copy link
Member Author

alcaeus commented Aug 25, 2022

Were most of these code changes manually done, or is this something phpcbf helped with after updating phpcs.xml.dist?

It was a mix of phpcbf and manual changes. Return types were added through manual labour as phpcs can't be configured to only enforce return types on private members or final classes.

@alcaeus alcaeus force-pushed the feature/phplib-599-typing branch from 0548d90 to 00a8eec Compare August 25, 2022 06:59
@alcaeus alcaeus force-pushed the feature/phplib-599-typing branch from 00a8eec to 1dec7b2 Compare August 25, 2022 06:59
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

getCommandDocument and execute() methods in Operations can probably get their doc blocks stripped down and return types added in some places. I think covariance on the interface allows for that.

@param annotations with single types (e.g. array, mixed) that don't have a description (or a meaningful description) of the parameter can probably be removed. The single type is probably already in the method signature. Likewise for |null stuff.

Likewise for @return annotations.

Lastly, I understand if you'd like to avoid adding type hints to the Executable and Explainable interfaces, even though they're documented as @internal. Perhaps we should have a ticket to add type hints and schedule it for 2.0, though.

@@ -99,7 +99,6 @@ public function __construct(array $options = [])
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return array An array of database info structures
Copy link
Member

Choose a reason for hiding this comment

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

Any reason not to use an array return type hint on execute() here? Correct me if I'm wrong, but I think that'd be permitted by covariance.

If this is out of scope for this PR, feel free to ignore.

Copy link
Member Author

Choose a reason for hiding this comment

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

Covariance would indeed allow us to add an array type here. However, backward compatibility promise won't allow us to do so, as anyone extending ListDatabases would be encountering fatal errors. Granted, it doesn't make much sense extending these classes, but they are all marked as part of the API and non-final, so we can't add return types to any public or protected methods.

Copy link
Member

Choose a reason for hiding this comment

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

Oh good point about extension. I didn't consider that.

Shame on us for not making these things final by default. Somewhere, @Ocramius is silently shaking his head in disappointment.

@alcaeus
Copy link
Member Author

alcaeus commented Aug 26, 2022

@jmikola you are correct - it looks like tooling did indeed leave most (if not all) @return array and @param array type annotations in place. I've removed them manually where applicable.

As for @param mixed and @return mixed, these are currently required to let the tooling know that the parameter expects any type (as opposed to "we don't know what it accepts) and returns a mixed value (as opposed to "we don't know what it returns") respectively. As such, removing them will cause complaints from the tooling. On the other hand, leaving them in place now will also help us when we drop PHP < 8 and can add the mixed pseudo type as an actual type.

Last but not least, most of our classes are neither final nor @internal, so the number of return types we can add is severely limited. In addition to that, the concept of tentative return types added in PHP 8.1 is only available to built-in and extension classes, but not to user land code. This means that we don't have a nice mechanism of telling people that the return type of a method will change in a future version. Making these classes final is something we should re-evaluate for 2.0.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

Thanks for all the detailed work on this. I'll leave you to create some 2.0 tickets on your own time.

@alcaeus alcaeus merged commit 1b29a6d into mongodb:master Sep 13, 2022
@alcaeus alcaeus deleted the feature/phplib-599-typing branch September 13, 2022 12:41
levon80999 pushed a commit to levon80999/mongo-php-library that referenced this pull request Sep 28, 2022
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants