Review main Merged

91225[BUGFIX] Harden `MathUtility::canBeInterpretedAsInteger()` for `PHP8.5`

Stefan Bürk CR +2 V +2 Easy +21 −7 updated 10/24/25, 12:27

Commit message

Working on the support for upcoming PHP 8.5.0 unit and functional tests
has been evaluated to detect issues for new native deprecations, errors
and warnings and a bunch of them has been already tackled.

`MathUtility::canBeInterpretedAsInteger()` was the last candidate with
two issues for PHP 8.5:

 The float 9.223372036854776E+18 is not representable as an int,
 cast occurred

and

  The float NAN is not representable as an int, cast occurred

The second one has been revealed after added test for float NAN value
with #107800 and executing it with PHP8.5 and requires now additional
care to tackle the NAN issue.

Some developer raised performance concerns regarding #107800 and the
used `preg_match()` approach and performance benchmark attempts has
been done afterwards with a phpbenchmark approach and a simple plain
PHP script using `microtime()`, both revealing a slightly performance
decrease in the range of a fraction of a millisecond (0.0x ms) with
10000000 (ten million) method calls and is considerable okay gaining
warnings resolved and ending in a more robust implementation.

For the now additional popped up warning more effort has been taken
place to find a even better solution with and without feared method
`preg_match()` and executed against **both** benchmarking approached
and unit/functional tests of TYPO3.

Statistic overview (phpbenchmark) [1]:
======================================

+-----------------------------------------------------------+-----+-----------+--------+
| subject                                                   | set | mode      | rstdev |
+-----------------------------------------------------------+-----+-----------+--------+
| benchOriginal                                             | 0   | 759.290ms | ±1.97% |
| benchOriginal                                             | 1   | 12.662s   | ±1.09% |
| benchPregMatchGerritChange91188                           | 0   | 828.307ms | ±0.78% |
| benchPregMatchGerritChange91188                           | 1   | 13.333s   | ±1.07% |
| benchPregMatchChange91188AndNanProtection                 | 0   | 820.434ms | ±0.51% |
| benchPregMatchChange91188AndNanProtection                 | 1   | 13.483s   | ±0.51% |
| benchVariantTacklingAllWarningsAndTryingToKeepPerformance | 0   | 742.543ms | ±0.57% |
| benchVariantTacklingAllWarningsAndTryingToKeepPerformance | 1   | 12.580s   | ±0.67% |
+-----------------------------------------------------------+-----+-----------+--------+

Each method variant has been executed `10000000` times per set providing
range from `0` to `10000000 - 1` as value having `set 1` using the value
directly and `set 1` as string with a appended `a` character.

Each method has been executed five times against each set and averaged.

To be honest, 10 million method calls are quite unrealistic within one
request (frontend/backend) and and the difference for the non-integer
string value has a difference of ~700ms being already in the ~12 seconds
range with the original implementation compared to the `preg_match()`
approach.

However, trying to avoid extensive discussions in multiple places the
effort to test, verify and measure (benchmark) different approaches
has been done directly and documented along right away.

Simply adding a `nan` protection condition to the `preg_match()` code
increased the performance again still having a diff of `~700ms`, and
after testing out a lot of variants not passing the tests and having
even worse stats.

Ending up refactoring the whole method implementation using `gettype()`
and `match()` to execute optimized checks based on the type removing
the `preg_match()` call again still satisfied all unit and functional
tests restoring the performance to the original implementation with
a slightly but ignorable improvement.

Note here, this depends on additional factors like opcache used
or not, the used pcre version and so on along with used the PHP
version (8.2/8.3/8.4/8.5) and like always with +/- ranges usual
for benchmarking results.

[1] https://github.com/sbuerk/mathutility-preg-match-bench
[2] https://github.com/sbuerk/mathutility-preg-match-bench/blob/58d03f60e3ad3142d15e7ac3221527395ab9cd55/plain-benchmark.php

Review

  • Code review CR +2 Done
    • Stefan Bürk +2
    • Oliver Klee +1
    • Markus Klein +1
    • Benni Mack +1
  • Verified V +2 Done
    • Stefan Bürk +2
    • Oliver Klee +1
    • Markus Klein +1
    • Benni Mack +1
    • core-ci +1 CI

Issues 2

Files 1

Review log 2328

Show 23 earlier messages
  1. Stefan Bürk uploaded patch set 1 10/23/25, 12:46

  2. core-ci voted on patch set 1 V +1 10/23/25, 12:55 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87422

  3. Stefan Bürk uploaded patch set 2 V +1 10/23/25, 13:49

  1. Stefan Bürk updated the commit message in patch set 3 10/23/25, 13:49

  2. core-ci voted on patch set 3 V +1 10/23/25, 14:01 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87428

  3. Stefan Bürk marked it ready for review 10/23/25, 14:09

  4. Oliver Klee voted on patch set 3 CR +1 V +1 10/23/25, 14:26

  5. Oliver Klee updated the commit message in patch set 4 CR +1 V +1 10/23/25, 14:27

  6. Oliver Klee voted on patch set 4 CR +1 V +1 10/23/25, 14:27

  7. core-ci voted on patch set 4 V +1 10/23/25, 14:34 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87433

  8. Garvin Hicking commented on patch set 4 10/23/25, 14:39

    MathUtility.php:74

    ```suggestion // can look weird here but is required to avoid enforced casting issues ```

    MathUtility.php:79

    ```suggestion // can look weird here but is required to avoid enforced casting issues ```
  9. Oliver Hader commented on patch set 4 10/23/25, 14:58

    MathUtility.php:74 Reply

    Fix applied.

    MathUtility.php:79 Reply

    Fix applied.
  10. Oliver Hader commented on patch set 5 CR +1 V +1 10/23/25, 14:58

    Published edit on patch set 4.
  11. Oliver Hader voted on patch set 5 CR +1 V +1 10/23/25, 14:59

  12. Oliver Hader commented on patch set 5 10/23/25, 14:59

    This is much clearer now. Thanks!
  13. Oliver Klee voted on patch set 5 CR +1 V +1 10/23/25, 15:02

  14. core-ci voted on patch set 5 V +1 10/23/25, 15:05 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87434

  15. Stephan Großberndt voted on patch set 5 CR +1 10/23/25, 17:10

  16. Elias Häußler commented on patch set 5 10/23/25, 18:07

  17. Markus Klein voted on patch set 5 CR +1 V +1 10/23/25, 18:08

  18. Stefan Bürk updated the commit message in patch set 6 CR +1 V +1 10/23/25, 21:57

  19. Stefan Bürk commented on patch set 6 10/23/25, 22:01

    Reply

    Yeah thanks ... fixed that. It nagged me to loose the votes but having that wrong was not an option. Beside that, @markus.klein@typo3.org also mentioned the wrong named "scale" in the commit message ... it's `only million` instead of `billion`, but still impressive enough and way above real world usage so fixing that along the way.
  20. core-ci voted on patch set 6 V +1 10/23/25, 22:05 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87450

  21. Oliver Klee voted on patch set 6 CR +1 V +1 10/24/25, 07:22

  22. Markus Klein voted on patch set 6 CR +1 V +1 10/24/25, 07:23

  23. Benni Mack voted on patch set 6 CR +1 V +1 10/24/25, 11:28

  24. Stefan Bürk voted on patch set 6 CR +2 V +2 10/24/25, 12:14

  25. Stefan Bürk merged patch set 7 10/24/25, 12:27