Skip to content

non-empty gets lost when non-empty-array is cast with (array) #9208

@zonuexe

Description

@zonuexe

Bug report

non-empty-array should keep NonEmptyArrayType when cast with (array).

Code snippet that reproduces the problem

/**
 * @param int|non-empty-list<int> $id_or_ids
 * @return non-empty-list<int>
 */
function f(int|array $id_or_ids): array
{
	if (is_array($id_or_ids)) {
		\PHPStan\Testing\assertType('non-empty-list<int>', (array)$id_or_ids);
	} else {
		\PHPStan\Testing\assertType('array{int}', (array)$id_or_ids);
	}

	$ids = (array)$id_or_ids;
	\PHPStan\Testing\assertType('non-empty-list<int>', $ids);
	return $ids;
}

https://phpstan.org/r/71dc2791-8fc0-4b1a-8c05-504ab454832e

Expected output

No errors.

Did PHPStan help you today? Did it make you happy in any way?

I enjoy seeing myself grow in solving PHPStan bugs when I encounter them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions