Skip to content

Commit

Permalink
feature #57630 [TypeInfo] Redesign Type methods and nullability (mtarld)
Browse files Browse the repository at this point in the history
This PR was merged into the 7.2 branch.

Discussion
----------

[TypeInfo] Redesign Type methods and nullability

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Issues        |
| License       | MIT

By using the component and trying to integrate it within several libs, I ended up finding some limitations due to the component design.

Basically, the issues were:
- Abstract classes (such as `Type`) knowing its concrete implementations (such as in `getBaseType` method).
- For API simplicity `CollectionType`, and `GenericType` are forwarding methods to their wrapped type using `__call`, but I think this must be avoided as much as possible.
- It is difficult to know if a type is "just" a nullable type (because we need to check the union content).
- Invalid types could be instantiated

To tackle these issues, I propose the following:
- Introduce `CompositeTypeInterface` and `WrappingTypeInterface` that describe whether a type is composed by several types or wrapping one.
- Add a `NullableType` which extends `UnionType` and is a `WrappingTypeInterface`, so that it is easy to recognize a nullable type and to get the non-nullable related type.
- Remove the `Type::asNonNullable` method as it's covered by `NullableType::getWrappedType`
- Get rid of the `__call` method as it is easy to know it a type is wrapping another one
- Rename `is` to `satisfy` and add the new `CompositeTypeInterface::composedTypesSatisfy` and `WrappingTypeInterface::wrappedTypeSatisfy` methods
- Rename `isA` to `isIdentifiedBy`

Plus, **thanks to `@skalpa`'s great work**, the following checks are added to prevent invalid type construction:
- `BackedEnumType`
    - Backed type can only be `int` or `string`
- `CollectionType`
    - Main type can only be `array` or `iterable` when it's a built-in type
- `GenericType`
    - Main type can only be `array` or `iterable` when it's a built-in type
- `IntersectionType`
    - Cannot contain other types than class types
- `UnionType`
    - Cannot contain `UnionType`
    - Cannot contain standalone types (`mixed`, `void`, and `never`)
    - Cannot contain either `true` and `false`
    - Cannot contain either `bool` and `true` or `false`
    - Cannot contain either `object` and a class type

Commits
-------

8c376c89c9b [TypeInfo] Redesign Type methods and nullability
  • Loading branch information
nicolas-grekas committed Nov 13, 2024
2 parents 7a46f7d + 81bf4e0 commit 2b2f30e
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"symfony/string": "^6.4|^7.0",
"symfony/translation": "^6.4|^7.0",
"symfony/twig-bundle": "^6.4|^7.0",
"symfony/type-info": "^7.1",
"symfony/type-info": "^7.2",
"symfony/validator": "^6.4|^7.0",
"symfony/workflow": "^6.4|^7.0",
"symfony/yaml": "^6.4|^7.0",
Expand Down

0 comments on commit 2b2f30e

Please sign in to comment.