Skip to content

Null coalesce operator can be used for type specifying shorthand #7501

@zonuexe

Description

@zonuexe

Feature request

The following two types of expressions are equivalent, but they don't work as expected with the null coalesce operator.

/**
 * @param array{key?: mixed} $a
 */
function f(array $a): void
{
	if (isset($a['key']) && is_int($a['key'])) {
		assertType('array{key: int}', $a);
	}

	if (is_int($a['key'] ?? null)) {
		assertType('array{key: int}', $a);
	}
}

https://phpstan.org/r/3bee56cc-e003-43e2-b4ad-f75e81c6652e

I found out that this commit doesn't handle this pattern. phpstan/phpstan-src@b98a1cf

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

This morning, it became a hot topic in Japan that the Japanese government's youth development program for IT innovation decided to support a new programming language project with the "Union Grand Unified Theory". https://www.ipa.go.jp/jinzai/mitou/2022/gaiyou_tk-1.html

Although details have not been clarified yet, the concept of the Cotton language seems to a statically typed language based on the union type instead of the sum type. We rely heavily on union types in PHP, so I'm looking forward to the results of this project.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions