Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added CacheInterface stub #236

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
ondrejmirtes merged 7 commits into phpstan:master from YspMark:master
Jan 18, 2022
Merged

Added CacheInterface stub #236

ondrejmirtes merged 7 commits into phpstan:master from YspMark:master
Jan 18, 2022

Conversation

@YspMark
Copy link
Contributor

@YspMark YspMark commented Jan 17, 2022
edited
Loading

Closes #235

BackEndTea reacted with heart emoji
Copy link
Collaborator

lookyman commented Jan 17, 2022
edited
Loading

The callable annotation has to have parentheses for parameters, event if there are none. Like this callable(): T.

However, in this case the callback actually optionally accepts Psr\Cache\CacheItemInterface as the first (and only) parameter.

Also, the parameter is annotated as callable|CallbackInterface in Symfony. So I think we need to add a second stub for the CallbackInterface, with templated __invoke method, and the one for CacheInterface's $callback needs to be annotated something like this: @param \Symfony\Contracts\Cache\CallbackInterface<T>|callable(\Psr\Cache\CacheItemInterface=): T $callback.

Finally, please add some tests.

/**
* @template T
*
* @param \Symfony\Contracts\Cache\CallbackInterface<T>|callable(\Psr\Cache\CacheItemInterface): T $callback
Copy link
Collaborator

Choose a reason for hiding this comment

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

\Psr\Cache\CacheItemInterface=, notice the = at the end. See https://phpstan.org/writing-php-code/phpdoc-types#callables.

For the Parameter $callback of method Symfony\Contracts\Cache\CacheInterface::get() has invalid type Psr\Cache\CacheItemInterface. error, we need to actually add the Psr\Cache\CacheItemInterface stub as well. It should be sufficient for it to be an empty class. We only need to know the class, we don't care about methods. See https://phpstan.org/user-guide/stub-files, Why is PHPStan complaining about "Class not found" in a stub file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback, I was not familiar with the = optional notation, actually thought it was a typo.

Copy link
Member

return $bar;
};

assertType('Bar', $cacheInterface->get('cacheTest', $callbackCallable));
Copy link
Member

@ondrejmirtes ondrejmirtes Jan 18, 2022

Choose a reason for hiding this comment

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

The case with \Symfony\Contracts\Cache\CallbackInterface<T> should also be tested.

@ondrejmirtes ondrejmirtes merged commit 96ee630 into phpstan:master Jan 18, 2022
Copy link
Member

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ondrejmirtes ondrejmirtes ondrejmirtes approved these changes

+1 more reviewer

@lookyman lookyman lookyman left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

CacheInterface->get returns mixed

AltStyle によって変換されたページ (->オリジナル) /