Skip to content

Fix: Correct typehint on repository retrieval methods#53025

Merged
taylorotwell merged 3 commits intolaravel:11.xfrom
liamduckett:fix/repository-get-typehint
Oct 7, 2024
Merged

Fix: Correct typehint on repository retrieval methods#53025
taylorotwell merged 3 commits intolaravel:11.xfrom
liamduckett:fix/repository-get-typehint

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)
*/

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
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