Skip to content

Commit c2db99f

Browse files
nyamsprodnicolas-grekas
authored andcommitted
Align Time\Duration with the native implementation
Runs the phpt tests of php-src (php/php-src#23073) unmodified against the polyfill, and fixes the divergences that are left: * use the native ZPP wording for the multiplyBy()/divideBy() argument errors, and reject a negative divisor with ValueError instead of DivisionByZeroError * detect the nanoseconds overflow of multiplyBy(), which turned into "TypeError: ... must be of type int, float given" * reject "PT1HS" and "PTS", which the native parser does not accept * validate $nanoseconds before the range of $seconds in fromSeconds() * report the native range error in fromMinutes()/fromHours()/fromMicroseconds()/ fromMilliseconds() and when parsing an ISO-8601 duration * forbid dynamic properties, as the native readonly class does The range and the sign of zero durations are now enforced in a single place, so that no operation can return a Duration that breaks the class invariants. On the test side, the phpt files are byte-identical to php-src, except helper.inc which cannot use the syntax of PHP >= 8.5, and new.phpt which is covered by DurationTest since a userland class cannot reject ReflectionClass::newInstanceWithoutConstructor(). DurationTest now runs on PHP >= 8.6 too, so its expectations are checked against the native class. Also adds the subtree split, moves the classmap entry next to the other 8.6 polyfills and ignores the temporary files written by PhptTest.
1 parent 16ed7cf commit c2db99f

10 files changed

Lines changed: 402 additions & 418 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ phpunit.xml
66
.phpunit
77
vendor/
88
/tests/Io/Poll/phpt/*.php
9+
/tests/Time/phpt/*.php
10+
/tests/Time/phpt/methods/*.php
911
/tests/unicode

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ should **not** `require` the `symfony/polyfill` package, but the standalone ones
174174
- `symfony/polyfill-intl-normalizer` for using the intl normalizer,
175175
- `symfony/polyfill-io-poll` for using the `Io\Poll` API and `StreamPollHandle`,
176176
- `symfony/polyfill-mbstring` for using the mbstring functions,
177+
- `symfony/polyfill-time` for using the `Time` API,
177178
- `symfony/polyfill-util` for using the polyfill utility helpers.
178179
- `symfony/polyfill-uuid` for using the `uuid_*` functions,
179-
- `symfony/polyfill-time` for using the `Time` API,
180180

181181
Requiring `symfony/polyfill` directly would prevent Composer from sharing
182182
correctly polyfills in dependency graphs. As such, it would likely install

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@
7070
"src/Intl/MessageFormatter/Resources/stubs",
7171
"src/Intl/Normalizer/Resources/stubs",
7272
"src/Io/Poll/Resources/stubs",
73+
"src/Time/Resources/stubs",
7374
"src/Php86/Resources/stubs",
7475
"src/Php85/Resources/stubs",
7576
"src/Php84/Resources/stubs",
7677
"src/Php83/Resources/stubs",
7778
"src/Php82/Resources/stubs",
7879
"src/Php81/Resources/stubs",
7980
"src/Php80/Resources/stubs",
80-
"src/Php73/Resources/stubs",
81-
"src/Time/Resources/stubs"
81+
"src/Php73/Resources/stubs"
8282
]
8383
},
8484
"minimum-stability": "dev"

splitsh.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"polyfill-intl-messageformatter": "src/Intl/MessageFormatter",
2121
"polyfill-intl-normalizer": "src/Intl/Normalizer",
2222
"polyfill-mbstring": "src/Mbstring",
23+
"polyfill-time": "src/Time",
2324
"polyfill-util": "src/Util",
2425
"polyfill-uuid": "src/Uuid"
2526
},

src/Time/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
Symfony Polyfill / Time
2-
============================
2+
=======================
33

44
This component provides the [`Time` API](https://wiki.php.net/rfc/duration_class)
55
added to PHP 8.6 core, for PHP >= 8.1:
66

77
- `Time\TimeException`, `Time\Duration`
88

9+
The phpt tests of the native implementation, borrowed from php-src, run against
10+
the polyfill as part of the test suite. Two divergences remain:
11+
12+
- `Duration` is a plain final class with readonly properties instead of a
13+
`readonly` class, so `ReflectionClass::newInstanceWithoutConstructor()`
14+
succeeds where the native class rejects it; dynamic properties are rejected
15+
as they are natively.
16+
- On 32-bit platforms, operations whose intermediate values do not fit in an
17+
integer throw `Time\TimeException` instead of returning the in-range result
18+
the native implementation computes with 64-bit arithmetic.
19+
920
More information can be found in the
1021
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).
1122

0 commit comments

Comments
 (0)