Skip to content

PHP 8.1 | NewReturnTypeDeclarations: support never and verify standalone types#1458

Merged
wimg merged 2 commits intodevelopfrom
php-8.1/newreturntypes-support-never-and-verify-standalone-types
Jan 3, 2023
Merged

PHP 8.1 | NewReturnTypeDeclarations: support never and verify standalone types#1458
wimg merged 2 commits intodevelopfrom
php-8.1/newreturntypes-support-never-and-verify-standalone-types

Conversation

@jrfnl
Copy link
Copy Markdown
Member

@jrfnl jrfnl commented Dec 30, 2022

PHP 8.1 | NewReturnTypeDeclarations: handle new never return type

Never type

A new return-only type never has been added. This indicates that a function either exit(), throws an exception, or doesn't terminate.

Includes unit tests.

Refs:

NewReturnTypeDeclarations: new check for non-standalone standalone types

The void, mixed and the new never types are "stand-alone" types, i.e types which are not allowed to be combined with other types, so they cannot be part of a type union, nor can they be nullable.

This is a cross-version compatibility issue as the name could have been used, prior to the introduction of the type, as a class/interface name and could have been nullable or part of a union as the class/interface name.

In the NewReturnTypeDeclarations sniff, this was so far only addressed by detecting the use of the nullable operator in combination with the mixed type.

That particular check has now been refactored to be based on a list of types in the $standAloneTypes property and to check for both the nullability operator, as well as the use of the type in a type union.

This includes a minor BC-break in that the PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.NullableMixed error code has now been changed to PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.NonStandaloneMixed .

Includes unit tests and minor adjustments to the existing unit tests to accommodate.

> **Never type**
>
> A new`return only type `never` has been added. This indicates that a function either `exit()`, throws an exception, or doesn't terminate.

Includes unit tests.

Refs:
* https://wiki.php.net/rfc/noreturn_type
* https://www.php.net/manual/en/migration81.new-features.php#migration81.new-features.core.never-type
* php/php-src#6761
* php/php-src@6cd0b48
The `void`, `mixed` and the new `never` types are "stand-alone" types, i.e types which are not allowed to be combined with other types, so they cannot be part of a type union, nor can they be nullable.

This is a cross-version compatibility issue as the name _could_ have been used, prior to the introduction of the type, as a class/interface name and could have been nullable or part of a union as the class/interface name.

In the `NewReturnTypeDeclarations` sniff, this was so far only addressed by detecting the use of the nullable operator in combination with the `mixed` type.

That particular check has now been refactored to be based on a list of types in the `$standAloneTypes` property and to check for both the nullability operator, as well as the use of the type in a type union.

This includes a minor BC-break in that the `PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.NullableMixed` error code has now been changed to `PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.NonStandaloneMixed` .

Includes unit tests and minor adjustments to the existing unit tests to accommodate.
@jrfnl jrfnl added this to the 10.0.0 milestone Dec 30, 2022
@jrfnl jrfnl requested a review from wimg December 30, 2022 15:50
@jrfnl jrfnl added the PR: quick merge PR only contains relatively simple changes label Dec 30, 2022
@wimg wimg merged commit 2d98eee into develop Jan 3, 2023
@wimg wimg deleted the php-8.1/newreturntypes-support-never-and-verify-standalone-types branch January 3, 2023 22:57
@github-actions github-actions bot removed PR: ready for review PR: quick merge PR only contains relatively simple changes labels Jan 3, 2023
@jrfnl jrfnl added the PHP: 8.1 label Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants