-
Notifications
You must be signed in to change notification settings - Fork 679
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
Update CachingIterator::getCache
stub.
#8282
Conversation
I found these snippets: https://psalm.dev/r/15a83f948f<?php
declare(strict_types=1);
namespace App;
use Generator;
use CachingIterator;
$generator =
/**
* @return Generator<bool, bool>
*/
static function (): Generator {
yield true => true;
yield false => false;
};
$iterator = new CachingIterator($generator(), CachingIterator::FULL_CACHE);
/** @psalm-trace $cache */
$cache = $iterator->getCache();
|
CachingIterator::getCache
stub.CachingIterator::getCache
stub.
CachingIterator::getCache
stub.CachingIterator::getCache
stub.
CachingIterator::getCache
stub.CachingIterator::getCache
stub.
It'd be nice to show some error if
|
Tell me how and I'll proceed asap.
Isn't it !!! |
I think the conservative route is to add a The downside is that it will result a false positive for |
I found these snippets: https://psalm.dev/r/4ea22ebdea<?php
/**
* @template T1
* @template T2 of array-key
* @template TFlags of int-mask<1,2,4,8,16,256>
*/
class Foo
{
/**
* @param T1 $a
* @param T2 $b
* @param TFlags $flags
*/
public function __construct($a, $b, $flags) {}
/** @psalm-if-this-is self<mixed, mixed, 256> */
public function test(): void {}
}
$foo1 = new Foo(1, 2, 3);
$foo1->test();
$foo2 = new Foo(3, 4, 256);
$foo2->test();
|
Thanks, very clear. I will update the PR in a couple of hours. |
Unfortunately, templates are not very suited for handling flags. While it could work for simple cases, it creates a whole world of weirdness, as @weirdan explained here: #5035 (comment) I don't know how other languages handle that kind of thing. But again, other languages may not have such a funky internal API... |
I thought it might be ok in this case since it seems like it's probably fine to only care about the exact value and not the mask, would you be opposed to it? It would be nice to have a more general way to deal with bitmask templates eventually. |
I just updated the PR, feel free to give another review. TIL: |
Hey @ondrejmirtes, Do you have an opinion on adding templates to iterators (or possibly any class) to track flags in __construct in order to change return types in other methods? If we started to do it, would PHPStan do the same in order to keep parity? Right now, things like inheritance and covariance checks on templates makes me think it would not be such a great idea but I'd like to get more point of view |
When adding a new Apart from |
It shows an error for this issue because it requires |
I found these snippets: https://psalm.dev/r/4ea22ebdea<?php
/**
* @template T1
* @template T2 of array-key
* @template TFlags of int-mask<1,2,4,8,16,256>
*/
class Foo
{
/**
* @param T1 $a
* @param T2 $b
* @param TFlags $flags
*/
public function __construct($a, $b, $flags) {}
/** @psalm-if-this-is self<mixed, mixed, 256> */
public function test(): void {}
}
$foo1 = new Foo(1, 2, 3);
$foo1->test();
$foo2 = new Foo(3, 4, 256);
$foo2->test();
|
Thanks for your input Ondrej! Yeah, I don't think we should try to use templates for flags in iterator classes. They're really weird and will always have edge cases. I feel like every time we make a big change in those, it doesn't solve a lot of cases but it always bring back new unsolvable issues a few months later (users were used to the old wrong behaviour and the new one may not be better for their use case) The rest of the PR seems good though |
I updated the PR, removed the new |
Also update `RecursiveCachingIterator`, remove extended methods and constructor that are the same.
stubs/CoreGenericIterators.phpstub
Outdated
/** | ||
* @param TIterator $iterator | ||
* @param int-mask-of<self::*> $flags | ||
* @param int-mask<1,2,4,8,16,256> $flags |
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.
Why change this? The previous type should work the same way but is more flexible to future changes, no?
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.
But then we need to provide a template when creating the CachingIterator, and this is what we want to avoid based on what I read on previous comments, no ?
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.
unless I'm mistaken, int-mask-of<self::*>
should not need any template
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 don't know, could you confirm?
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.
Well, that's what it had before this PR, so if it works on psalm.dev right now it should be fine: https://psalm.dev/r/ecacca8230
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.
Fixed.
Thanks! |
cool, merci aussi ! |
This PR:
CachingIterator::getCache
stubExamples: https://3v4l.org/i5faS#v8.1.8
False positive in PSalm: https://psalm.dev/r/15a83f948f