Review 13.4 Merged

91239[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
  • Verified V +2 Done
    • Stefan Bürk +2
    • core-ci +1 CI

Issues 2

Files 1

Review log 34

  1. Stefan Bürk commented on patch set 1 10/24/25, 12:14

    Cherry Picked from branch main.
  2. core-ci voted on patch set 1 V +1 10/24/25, 12:22 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87492

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

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