-
Notifications
You must be signed in to change notification settings - Fork 76
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
Feature - Psalm annotations #93
Conversation
This is mainly to help validate the type definitions.
`\IteratorAggregate->getIterator()` does not return an `\Iterator`, it returns a `\Traversable`, which does not necessarily provide a `valid()` method. In such cases, further checks are needed to determine what has been returned.
High level comment: This uses a lot of
I'm not a fan of this bit. The fact that these are implemented using generators is an implementation detail, not something that should be exposed as an API-level guarantee. We should reserve the right to implement these using custom Iterators in the future. I also don't think that RewindableGenerator fundamentally requires a Generator. It should work fine with an Iterator as well. The only Generator-specific functionality it has is that it also forwards the send and throw methods. It was probably a bad idea to implement those, as they're not really relevant to this library. I think we should either suppress those undefined methods for the non-generator case, or split up RewindableGenerator into RewindableIterator and a RewindableGenerator extension for it, where the former only requires the Iterator interface, and the latter the Generator interface. |
Thanks for the feedback! For the I hear what you're saying about Generators / RewindableGenerator. The quick and dirty fix here would indeed be to simply suppress the type contradiction. That said, looking at RewindableGenerator I do see that On a side note, I can see that the CI actions for PHP 7.1 are failing on the composer-install step; do you happen to have any ideas on what's failing there? All I can see in the logs is that composer exited with an error code, but testing PHP 7.1 with composer v1 in a container locally seems to succeed for me. |
From a quick test, this seems to at least parse correctly, so I think that's fine. As long as it doesn't get worse treatment than plain
I'd probably go for the RewindableIterator -- it doesn't seem like this would be particularly hard to do.
No idea what's going on there. Maybe we could try whether updating to |
Right, I've managed to sort the For the RewindableIterator, I've gone ahead and implemented it. For backwards-compatibility purposes, I've left a RewindableGenerator which extends off of the RewindableIterator and marked it as deprecated. I've also updated the I've even managed to figure out the CI issue. Upgrading to Please have a look when you have a moment 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable to me.
<directory name="vendor" /> | ||
</ignoreFiles> | ||
</projectFiles> | ||
</psalm> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a phpstan config as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike psalm, phpstan does not require an explicit config. A config can be added to be explicit about things if so desired.
Should go a ways towards #83
I've been using this library in a few of my projects, and found myself wanting these annotations.
Some notes about this PR:
vimeo/psalm
as arequire-dev
dependency (^4.18
for PHP >= 7.1,^5.13
for PHP >= 7.4). This also adds a default Psalm config file (psalm.xml
).iter\
namespace to return\Generator
instead of\Iterator
. This is to satisfy the type requirements ofmakeRewindable
andcallRewindable
, and is actually what these functions return.isEmpty()
, as\IteratorAggregate->getIterator()
only returns\Traversable
, not\Iterator
.In the edge case that the returned
\Traversable
is not also\Iterator
, this could have caused an issue.This is fixed in this PR.
$levels = INF
parameter or similar, changedINF
toPHP_INT_MAX
asINF
is a float type and not int as declared. In these cases,$levels
can never exceed thePHP_INT_MAX
anyhow.docblock to explain this.
\iter\rewindable
namespace, as that would necessarily mean that any changes to the regular function would also need to be reflected in the docblocks for these methods. This does mean that these functions do not benefit from the Psalm type annotations currently.Please do reach out if there are any questions!