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

More precise array_keys return type #3590

Open
wants to merge 2 commits into
base: 2.0.x
Choose a base branch
from
Open

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Oct 28, 2024

Comment on lines 49 to 54
if (!$keysArray->isConstantArray()->no()) {
$newArrayType = new ArrayType(
$keysArray->getIterableKeyType()->generalize(GeneralizePrecision::lessSpecific()),
$keysArray->getIterableValueType()->generalize(GeneralizePrecision::lessSpecific()),
);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we could make this more sophisticated and support more filtering on constant array, but I think this is good enough for now.

until a few days ago I didn't even know array_keys can filter by values - it feels pretty rare

@staabm staabm marked this pull request as ready for review October 28, 2024 17:34
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@herndlm
Copy link
Contributor

herndlm commented Oct 29, 2024

Just wondering if any of this could also be done inside Type::getKeysArray() as well? to e.g. avoid having to deal with lists outside of it again.

See also #2117 where I played around with adding filter support 😊

@herndlm
Copy link
Contributor

herndlm commented Oct 29, 2024

Nice, sorry, noticed too late and should have mentioned it earlier. Later on, e.g. in #3514 I used TrinaryLogic instead of bool in the new Type function. Do you think you can use that too?

@staabm
Copy link
Contributor Author

staabm commented Oct 29, 2024

I used $strict as bool, because other methods in Type also use it.

Do you see a use-case where Trinary would come in handy?

@herndlm
Copy link
Contributor

herndlm commented Oct 29, 2024

Not necessarily. If others use that too, then consistency makes sense IMO.

@staabm staabm force-pushed the b11928 branch 2 times, most recently from 9dbb287 to 21fc500 Compare November 14, 2024 10:35
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.

array_keys with > 1 argument returns too-broad type
3 participants