Skip to content
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

Fix: Correct typehint on repository retrieval methods #53025

Merged
merged 3 commits into from
Oct 7, 2024

Conversation

liamduckett
Copy link
Contributor

Hey. I believe the Typehints on these methods are incorrect.

The return type currently reads as

If a non null default is specified, then the return type will be the default. Otherwise, the return type will be null

This doesn't take into account whether a value for this key is present. See a (very) minimal reproduction in PHPStan.

My understanding is that the dumped type here should not be 5, as this expression would return 'bar'. As such, a typehint more than mixed cannot be offered here.

It may be worth noting that this was not an issue interrupting any actual code - I noticed it whilst investigating an issue I was having with Laravel Idea.

For reference - the Cache facade only offers a return type of mixed

/**
* @method static mixed get(array|string $key, mixed|\Closure $default = null)
*/

@liamduckett liamduckett force-pushed the fix/repository-get-typehint branch from c82cb30 to 4ee5462 Compare October 3, 2024 18:30
assertType('int', $cache->get('cache', 27));
assertType('int', $cache->get('cache', function (): int {
assertType('mixed', $cache->get('cache', 27));
assertType('mixed', $cache->get('cache', function (): int {
return 26;
}));

assertType('mixed', $cache->pull('key'));
Copy link
Member

Choose a reason for hiding this comment

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

@liamduckett wouldn't this assertion assert that the existing behavior is correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @crynobone. I don't think the current assertion exhibits the correct behaviour.

How can we be sure that $cache->get('cache', 27) is an int? What if the default isn't used (because the cache has a value under the requested key)?

@taylorotwell taylorotwell merged commit 6df8b61 into laravel:11.x Oct 7, 2024
31 checks passed
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.

3 participants