-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Add generic hints to Cache #45325
Add generic hints to Cache #45325
Conversation
Can't this be sent to 9.x instead? |
921b538
to
651f5f9
Compare
I was told it coudn't |
Who did? @nunomaduro just said that we can't add these in general: It's been over a year so maybe we can reconsider but it's up to @taylorotwell to decide. |
Ok, I'm not privy to what the reasoning is. But I'm happy to have it go in 9.x if possible.
They where added for other components so it felt specific for the cache component? P.s. I'm pretty sure it's still 2022, or I forgot to show up for xmas which would be pretty bad :D |
These seem wrong to me, just because we pass a default doesn't mean that the value in the cache is of that type as well:
This will return a string but according to this PR it will return an int. |
Thanks for sending this in. I'm going to mark it as draft for now while people discuss it and also while we wait for @nunomaduro to return from vacation. |
I guess this is true, thought this feels like somewhat dubious usage. The same issue could technically also occur with cache()->remember('foo', 60, function (): string { return 'bar'});
cache()->remember('foo', 60, function (): int { return 123}); For such cases I think it would be fair to still warn the user that they are likely doing something wrong. |
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.
I've made some comments that must be addressed before merging. Also, can you rebase this on v9.x
please?
src/Illuminate/Cache/Repository.php
Outdated
@@ -175,9 +177,11 @@ protected function handleManyResult($keys, $key, $value) | |||
/** | |||
* Retrieve an item from the cache and delete it. | |||
* | |||
* @template TCached of mixed |
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.
Please use TCacheValue
everywhere, and remove of mixed
.
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.
Oh neat I wasn't aware of that short hand
src/Illuminate/Cache/Repository.php
Outdated
* @param mixed $default | ||
* @return mixed | ||
* @param TCached $default | ||
* @return null|TCached |
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.
No need for null
here.
src/Illuminate/Cache/Repository.php
Outdated
* @param string $key | ||
* @param mixed $default | ||
* @return mixed | ||
* @param TCached $default |
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.
This should consideration that the $default
value may be a closure that returns TCacheValue
. And this comment can applied to other arguments annotations in this pull request.
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.
Wasn't aware of this possibility in Laravel, hopefully this will help more discover it as well :)
@@ -0,0 +1,28 @@ | |||
<?php |
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.
You will need to add tests for the cases when a closure is given for the $default arguments.
Thanks! I'll look in to it 👍 |
651f5f9
to
5fbc585
Compare
@nunomaduro the issues should now have been addressed and the PR has been rebased and targeted towards |
@AJenbo can you maybe also fix the failing docblock check? |
@driesvints could you give some input on how it should be resolved? My thinking is that the tool should allow for additional lines, at least in the bloc preceding the generated part. This would also allow for adding descriptions to Facades. /**
* This facade adds a convenient way to interact with the application cache
*
* @template TCacheValue
*
* ... generated methods
*/ There also is this line in the tool that I'm not sure if maybe is relevant: We could also have it pickup any remaining generics and add them automatically like I have done manually in this PR? |
Just copy paste the output of the build. |
Well I have, and then added the following bit to make it actually valid:
@driesvints are you saying that we should go with the non functional generated one for now? P.s. I think it would also be good to have the tool generate a diff to make the discrepancies more clear. But that is a bit out of scope here. |
I'll fix up the generics Facade docblocks stuff once we merge this one. Don't let it be a blocker. |
5fbc585
to
067e02b
Compare
067e02b
to
b9c7356
Compare
Currently working on something else. But I will look at this week at this. |
Made a few adjustments, and added as you as co-author: #45467. |
@nunomaduro Since this update, doing the following will give the error public function doStuff(): string
{
return Cache::rememberForever('key', function (): string {
return 'example';
});
} In other words, it sees the return type |
This provides a way for IDEs and other analyzer tools to reason about the value returned by the cache and there by not cause lose of context when ever it is used. See #38257 for a general discussions about this topic.
This PR is opened against master as generic annotations on the cache component is meant to come after 9.x.
P.s. @nunomaduro could you please share some more information regarding the plans for introducing generic annotations on the framework as it would make it easier to work on :)