Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #57630 [TypeInfo] Redesign Type methods and nullability (mtarld)
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