8000 [PropertyInfo] Support multiple builtinType (union types) by Korbeil · Pull Request #38987 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyInfo] Support multiple builtinType (union types) #38987

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

Closed

Conversation

Korbeil
Copy link
Contributor
@Korbeil Korbeil commented Nov 4, 2020
Q A
Branch? 5.x
Bug fix? no
New feature? yes
Deprecations? no
Tickets #38093
License MIT
Doc PR N/A

First step for #38093, before parsing union types in phpDoc, we need to handle union types correctly in the Type class.

I changed the $builtinType type in the Type construct in order to handle multiple builtin types.

@Korbeil Korbeil force-pushed the feature/property-info-union-type branch 2 times, most recently from 786b60e to bbe2949 Compare November 4, 2020 15:54
@Korbeil Korbeil force-pushed the feature/property-info-union-type branch from bbe2949 to bf96c1a Compare November 4, 2020 16:00
@Korbeil Korbeil force-pushed the feature/property-info-union-type branch 3 times, most recently from ec3a8e5 to d7655cd Compare November 4, 2020 16:20
@stof
Copy link
Member
stof commented Nov 4, 2020

I don't think that's the right representation of a union type. Currently, an object type is represented as new Type('object', $nullable, MyClass::class)

So to support union types, I think we would need something dealing with an array of Type objects instead of having an array only in the buiiltinType argument (which does not support unions for object types).
your proposal supports string|int but not A|B

@Korbeil Korbeil force-pushed the feature/property-info-union-type branch from d7655cd to 585ef24 Compare November 4, 2020 16:20
@Korbeil Korbeil force-pushed the feature/property-info-union-type branch 2 times, most recently from 971cfcc to 00d3325 Compare November 4, 2020 16:25
@Korbeil
Copy link
Contributor Author
Korbeil commented Nov 4, 2020

I don't think that's the right representation of a union type. Currently, an object type is represented as new Type('object', $nullable, MyClass::class)

So to support union types, I think we would need something dealing with an array of Type objects instead of having an array only in the buiiltinType argument (which does not support unions for object types).
your proposal supports string|int but not A|B

It's actually true, but I don't think we should have multiple objects to describe that, it should be stored in a single object. It also highlight another issue that I would like to solve: use a separate parameter for the management of nullability when it should be included in builtin types from the beginning.

Can't we try to think on new object to manage this ? Something like class UnionType which will extends our actual Type ?

My ideal implementation would be something like this: https://gist.github.com/Korbeil/3cfaa05878657f4150bdb0ce0fb0b0e6

@stof
Copy link
Member
stof commented Nov 4, 2020

It's actually true, but I don't think we should have multiple objects to describe that, it should be stored in a single object. It also highlight another issue that I would like to solve: use a separate parameter for the management of nullability when it should be included in builtin types from the beginning.

To me, the actual issue is more distinguishing builtin types from class types actually. nullability as separate made sense in PHP 7 where any type would be prefixed by ? to make it nullable.

@jderusse jderusse added this to the 5.x milestone Nov 4, 2020
@Korbeil
Copy link
Contributor Author
Korbeil commented Nov 4, 2020

To me, the actual issue is more distinguishing builtin types from class types actually. nullability as separate made sense in PHP 7 where any type would be prefixed by ? to make it nullable.

Do you already have some ideas about how to do that ? I actually can't think of a new object representation without some needed compatibility layer to avoid BC.

@dunglas
Copy link
Member
dunglas commented Nov 4, 2020

I fear we'll not be able to prevent the compatibility layer. I like @stof proposal of returning an array of Type.

@stof
Copy link
Member
stof commented Nov 4, 2020

We should probably return some kind of object wrapping an array of types, rather than a raw array of types. Otherwise, we'll face an issue again if/when we need to add support for intersection types (which also deal with a list of types, but not the same than for union types)

@stof
Copy link
Member
stof commented Nov 4, 2020

If we want to fit that in the existing structure of the Type object, we might use union as the "builtin type" and have a getter returning the list of types, maybe

@Korbeil Korbeil force-pushed the feature/property-info-union-type branch from 00d3325 to bb4ccaa Compare November 5, 2020 12:12
@Korbeil
Copy link
Contributor Author
Korbeil commented Nov 5, 2020

I'll close this for now since the issue is not representating union types since PropertyTypeExtractorInterface already support it by returning an array.
The issue here is that subtypes (collection key & value) are an instance of Type which doesn't allow union types. For example: A|B|C is already supported, but array<A|B|C> isn't. I'll try to come with a better solution 😉 Thanks everyone for your comments and reviews.

@Korbeil Korbeil closed this Nov 5, 2020
@Korbeil Korbeil deleted the feature/property-info-union-type branch November 5, 2020 12:35
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.

7 participants
0