nette / php-generator

🐘 Generates neat PHP code for you. Supports new PHP 8.3 features.
https://doc.nette.org/php-generator
Other
2.11k stars 138 forks source link

Nullable union type #139

Closed GromNaN closed 1 year ago

GromNaN commented 1 year ago

From PHPWatch: PHP 8.0: Union Types

You can declare a type as nullable by prefixing a ? sign to the type. For example, if a certain property can be a string or a null, you can declare it as ?string. With PHP 8.0 Union Types, string|null will be functionally equivalent to ?string.

Also, ? is incompatible with union types. ?string|int is not accepted.

I'd like modify Type::nullable() to detect union type and add |null instead of ?.

Alternatively, Type::union() could accept a 2nd parameter: $nullable that adds the |null

I'll create a PR if that sounds good for you.

Note: intersection types are not nullable, RFC was declined.

dg commented 1 year ago

I agree, and I'll be glad for the PR