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

Use PHP 8.0 union types #89

Open
marijnvanwezel opened this issue Feb 2, 2023 · 0 comments
Open

Use PHP 8.0 union types #89

marijnvanwezel opened this issue Feb 2, 2023 · 0 comments
Labels
platform/php-7.4 Issues related specifically to running php-cypher-dsl on PHP 7.4 subject/typing Issues related to the type system type/enhancement New feature or request type/refactoring A refactoring that should be applied to make the code easier to understand

Comments

@marijnvanwezel
Copy link
Collaborator

PHP 8.0 introduces the concept of union types, which allows us to explicitly type a function with multiple different types. php-cypher-dsl currently polyfills this feature through the ErrorTrait trait. However, there are a few problems with this polyfill approach:

  • It is not understood by IDE's, which makes refactoring hard;
  • It requires an additional function call;
  • Type-checking is only done at the deepest call;
  • It does not work for interfaces or abstract methods.

Therefore, I want to propose to drop support for PHP 7.4 and start using PHP 8.0 union types. Implementing this is relatively straightforward:

  1. Add union types where needed to return types;
  2. Add union types where needed to argument types;
  3. Add union types where needed to class parameter types;
  4. Remove any references to the ErrorTrait trait.

The ErrorTrait trait also has a method for checking whether the type of each value in an array is allowed. This is currently not supported through PHP's type system. There are two options to fix this:

  • Use PHPDoc to specify which types the array allows and use PHP's native array type in signatures, or
  • Keep supporting the type-checking of arrays.

I think the first option is best. Type checking of arrays is only used in a few places (five times throughout the entire library), and it adds complexity and overhead. Most usages can also be replaced by typed variadic parameters.

@marijnvanwezel marijnvanwezel added type/enhancement New feature or request subject/typing Issues related to the type system type/refactoring A refactoring that should be applied to make the code easier to understand platform/php-7.4 Issues related specifically to running php-cypher-dsl on PHP 7.4 labels Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform/php-7.4 Issues related specifically to running php-cypher-dsl on PHP 7.4 subject/typing Issues related to the type system type/enhancement New feature or request type/refactoring A refactoring that should be applied to make the code easier to understand
Projects
None yet
Development

No branches or pull requests

1 participant