Skip to content

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
merged 7 commits into from
Jan 18, 2022
Merged

Added CacheInterface stub #236

merged 7 commits into from
Jan 18, 2022

Conversation

YspMark
Copy link
Contributor

@YspMark YspMark commented Jan 17, 2022

Closes #235

@lookyman
Copy link
Collaborator

lookyman commented Jan 17, 2022

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.

@ondrejmirtes
Copy link
Member

Can you please write some tests in https://github.com/phpstan/phpstan-symfony/blob/master/tests/Type/Symfony/ExtensionTest.php to verify this signature works as expected? More info here: https://phpstan.org/developing-extensions/testing#type-inference

return $bar;
};

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

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
@ondrejmirtes
Copy link
Member

Thank you.

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.

CacheInterface->get returns mixed
3 participants