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

Add TypeCombinator::removeFalsey() #2003

Merged
merged 1 commit into from Dec 16, 2022
Merged

Add TypeCombinator::removeFalsey() #2003

merged 1 commit into from Dec 16, 2022

Conversation

axlon
Copy link
Contributor

@axlon axlon commented Nov 15, 2022

This PR adds the ability for the type combinator to remove falsey types from a given type. I am in need of this functionality within an extension, but the static type factory is currently not part of the public API. Rather than changing that, this seemed like a better way to expose the functionality.

PHPStan also contains a few places where it removes falsey types, so I replaced those occurrences.

@ondrejmirtes
Copy link
Member

Hi, what's your extension and why do you have the need to remove all falsey types in it? :)

@axlon
Copy link
Contributor Author

axlon commented Dec 16, 2022

Hi, what's your extension and why do you have the need to remove all falsey types in it? :)

Our extension is currently still privated, because its in very early stages of development. But we're working on an extension that aims to make Laravel strictly typed, which could be used alongside or separate of Larastan.

Currently what I'm trying to tackle is Laravel's Collection::filter() which works the same as array_filter(); when the method is called without any parameters it will remove any falsey values from the collection.

/** @var \Illuminate\Support\Collection<int, string|null> $foo */
$bar = $foo->filter(); // \Illuminate\Support\Collection<int, non-falsey-string>

My thinking was that since this is the way the array_filter extension does it, this is probably the best way to remove falsey types from a type.

@ondrejmirtes ondrejmirtes merged commit c9a546f into phpstan:1.9.x Dec 16, 2022
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants