Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid typehints for nullable properties with union type #72

Closed
petr-buchyn opened this issue Dec 7, 2020 · 4 comments
Closed

Invalid typehints for nullable properties with union type #72

petr-buchyn opened this issue Dec 7, 2020 · 4 comments

Comments

@petr-buchyn
Copy link

petr-buchyn commented Dec 7, 2020

Version: 3.5.1

Bug Description

For now for nullable properties this library generates typehint with ?, but this conflicts with PHP8 Union Types.
Generated typehint for union types now looks like ?OneType|OtherType, which is not valid PHP syntax.
Could you fix this for PHP8 please?

Possible Solution

Check PHP version and if it's larger than 8.0 - replace ? for |nullin typehint.

@dg
Copy link
Member

dg commented Dec 7, 2020

Show please your code

@petr-buchyn
Copy link
Author

Hi @dg !

Sample code to reproduce:

$class = new ClassType('MyClass');
$class
    ->addProperty('foo')
    ->setType('int|string|SomeClass')
    ->setNullable(true);

$printer = new PsrPrinter();
$printer->printClass($class);

@dg dg closed this as completed in 0ebcd5b Dec 8, 2020
@petr-buchyn
Copy link
Author

Thanks, @dg !
Not that it is an issue for me, but you also should check, whether the |null is already contained in union type - otherwise there may be a fatal error.

Anyway, thank you very much!

@dg
Copy link
Member

dg commented Dec 8, 2020

I think I'll leave it. So that no one sets setNullable together with setType('...|null')

dg added a commit that referenced this issue Feb 24, 2021
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

No branches or pull requests

2 participants