Skip to content

Conversation

VincentLanglet
Copy link
Contributor

Closes phpstan/phpstan#9734

I'll need your decision on the condition to chose in ArrayType::tryRemove @ondrejmirtes

We could have

if ($typeToRemove->isSuperTypeOf(new ConstantArrayType([], []))->yes()) {
     return TypeCombinator::intersect($this, new NonEmptyArrayType());
}

but it means that now a

@phpstan-assert-if-true string[] $array

will infer a non empty array for the false case.

It could make sens since in order to say that all the values are not string, it require to access to at least one value.

If preferred we could restrict such behavior to the list with an extra $typeToRemove->isList()->yes() check.

Or, to just solve array_is_list ; the check can be something like $typeToRemove->describe(...) === 'list'...

Copy link
Contributor

@staabm staabm left a comment

Choose a reason for hiding this comment

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

Do we have a test somewhere which asserts the types for array_is_list(mixed)?

@VincentLanglet
Copy link
Contributor Author

Do we have a test somewhere which asserts the types for array_is_list(mixed)?

Dunno, but what do you have in mind ?
It gives

/**
	 * @param mixed $a
	 * @return void
	 */
	public function doFoo4($a): void
	{
		if (array_is_list($a)) {
			assertType('list', $a);
		} else {
			assertType('mixed~list', $a);
		}
	}

I added a test.

@staabm
Copy link
Contributor

staabm commented Aug 9, 2025

Looks good - just making sure we have it covered

@ondrejmirtes ondrejmirtes merged commit 07a29ab into phpstan:2.1.x Aug 29, 2025
443 of 452 checks passed
@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

Development

Successfully merging this pull request may close these issues.

array is non-empty when array_is_list returns false

3 participants