Review main Merged

81325[BUGFIX] Mitigate locale `C` in `DateFormatter` wrapping class

Stefan Bürk CR +2 V +2 Normal +51 −8 updated 10/05/23, 14:56

Commit message

Since PHP 8.3.0RC3 [1] the `\IntlDateFormatter` throws a
`\IntlException` exception with the message:

    datefmt_create: invalid locale: U_ILLEGAL_ARGUMENT_ERROR

if an invalid locale is passed to the constructor. The value `C`
is considered as a non-valid locale, albeit it may be set.

This change introduces a fallback to `en-US` for the two methods
of the `\TYPO3\CMS\Core\Localization\DateFormatter` wrapping class,
if `C` is passed as locale.

`en-US` has been chosen as fallback because the TYPO3 locale class
`\TYPO3\CMS\Core\Localization\Locale` uses the same as fallback if
`C` is provided and additional tests are added to cover the same
behaviour.

Note: Other illegal locales are not handled and will allow the
      exception to bubble up with PHP8.3+.

[1] https://github.com/php/php-src/commit/a80db7b52aa134bdc8b9841

Review

  • Code review CR +2 Done
    • Stefan Bürk +2
    • Oli Bartsch +1
    • Oliver Klee +1
    • Markus Klein +1
    • Simon Schaufelberger +1
  • Verified V +2 Done
    • Stefan Bürk +2
    • Oli Bartsch +1
    • Oliver Klee +1
    • Markus Klein +1
    • Simon Schaufelberger +1
    • core-ci +1 CI

Issues 1

Files 2

Review log 1720

  1. Stefan Bürk uploaded patch set 1 10/05/23, 11:04

  2. core-ci voted on patch set 1 V -1 10/05/23, 11:12 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/47436

  3. Oliver Klee commented on patch set 1 10/05/23, 11:40

    DateFormatter.php:107

    Would it make sense to move this comment to inside the `if` so that it's directly above the line it refers to?

    DateFormatter.php:108

    I propose we make this more explicit (mostly for the benefit of the human reader): ```php if (is_string($locale)) { ```
  4. Stefan Bürk uploaded patch set 2 V -1 10/05/23, 12:06

  5. Stefan Bürk commented on patch set 2 10/05/23, 12:07

    DateFormatter.php:107 Reply

    irrelevant - if condition has been removed again.

    DateFormatter.php:108 Reply

    irrelevant - if condition has been removed again.
  6. Stefan Bürk commented on patch set 2 10/05/23, 12:07

    DateFormatter.php:107 Reply

    Done
  7. Oliver Klee commented on patch set 2 10/05/23, 12:11

    DateFormatter.php:89

    According to https://www.php.net/manual/de/function.setlocale.php, `setlocale` will return `false` if there is a problem. In this case (`false`, which gets cast to an empty string), I suggest we also switch to the `en-US` fallback locale.

    DateFormatterTest.php:94

    I propose we also have a test with a nonsensical locale (i.e., that is certain to not exist), e.g., `inexistent-locale` (which would this very explicit to the reader).
  8. core-ci voted on patch set 2 V +1 10/05/23, 12:13 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/47439

  9. Stefan Bürk commented on patch set 2 10/05/23, 12:21

    DateFormatter.php:89 Reply

    empty locale would let the IntlFormatter fallback to the default php ini setting for intl.locale ... therefore, the empty is okay here. `C` is adopted to fallback like it is handled in the Locale() class implementation already. Mainly, because `ext:intl` does not take C as a valid locale. A general handling of this and other cases needs to be addressed in a bigger picture. This change is about getting a first working state so we can update the PHP8.3.0 image to RC3 for now. I would not switch to a overall default of 'en-US' for all cases.

    DateFormatterTest.php:94 Reply

    We should taggle that in a dedicated patch. The locale handling needs additional work, and the setlocale() needs to vanish in either cases. The goal of this change is to get a compatible state for PHP8.3 right now. Handling of absolutly not existing/invalid locales needs to be addressed in a bigger picture.
  10. Oliver Klee commented on patch set 2 V +1 CR +1 10/05/23, 12:34

    DateFormatterTest.php:94 Reply

    Ack
  11. Markus Klein updated the commit message in patch set 3 CR +1 V +1 10/05/23, 12:55

  12. Markus Klein voted on patch set 3 V +1 CR +1 10/05/23, 12:55

  13. core-ci voted on patch set 3 V +1 10/05/23, 13:02 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/47440

  14. Stefan Bürk voted on patch set 3 V +2 CR +2 10/05/23, 13:21

  15. Stefan Bürk voted on patch set 3 V 0 CR 0 10/05/23, 13:21

  16. Oliver Klee voted on patch set 3 V +1 CR +1 10/05/23, 13:51

  17. Simon Schaufelberger voted on patch set 3 V +1 CR +1 10/05/23, 14:23

  18. Oli Bartsch voted on patch set 3 V +1 CR +1 10/05/23, 14:46

  19. Stefan Bürk voted on patch set 3 V +2 CR +2 10/05/23, 14:47

  20. Stefan Bürk merged patch set 4 10/05/23, 14:56