Floats in locales that use decimal comma #909
Replies: 5 comments 4 replies
-
as there's a workaround available, i'm not sure we should pursue a fix we have to remove in a while... could you share example code to give a little insight to what you're suggested solution is? perhahps then it would be possible to come up with something which is less work or at the very least decide whether it's worth the effort. |
Beta Was this translation helpful? Give feedback.
-
@thomascorthals thanks for adding the poc! hmz, tough one. personally (but that's just me), knowing php core is about to fix this, i think the best option is to document this issue and workaround properly and leave it at that |
Beta Was this translation helpful? Give feedback.
-
I've opened PR #911 as a proof-of-concept and marked it as a draft. The solution is simple enough: However, the very next function I encountered already poses a problem. solarium/src/Core/Query/Helper.php Line 224 in 3340357 It says it takes a string, but without strict typing it also happily takes a float. In fact, that's how we unit test it and how we tell to use it in the docs. solarium/tests/Core/Query/HelperTest.php Lines 245 to 251 in 3340357 solarium/examples/2.1.6-helper-functions.php Lines 16 to 17 in 3340357 We'd have to check the type and only json_encode on actual floats lest we get "45.15" if it's passed as a string. Changing the typehint and declaring strict_types=1 would break BC for everyone who does pass it as a string.
I didn't try and find out how many functions are susceptible to this. Another major issue is with testing these changes. A correct test requires a locale that uses a decimal comma. If the $ locale -a
C
C.utf8
POSIX You'll also have to remember to set the locale back to You could do it in You could group all locale related tests in a separate |
Beta Was this translation helpful? Give feedback.
-
agreed. might be interesting for a 7.0 release to set
personally i don't think it should be part of the test itself. we might want to look into whether it's possible to use a github action which changes the php.ini for each test run, but i'm not sure testing all possible locales is the way to go this sounds like a lot of work for something with a workaround and a definite fix ahead so i'll stick to
|
Beta Was this translation helpful? Give feedback.
-
I was a bit thrown off because it always does this when I run tests locally:
Actually, the This doesn't change the fact that the Docker containers out there for testing don't have any locales preinstalled. $ locale -a
C
C.utf8
POSIX |
Beta Was this translation helpful? Give feedback.
-
See issue #262 that was opened 6.5 years ago. I was looking to finally fix this and it turns out it isn't all that hard, but would still be a lot of work. Changes aren't limited to the request builders, other functions that deal with floats (e.g.
rangeQuery()
) would also need to be updated. There also would have to be additional unit tests for each affected function.However, in PHP 8.0 all of this is no longer necessary: https://wiki.php.net/rfc/locale_independent_float_to_string. Floats are always cast to string with a decimal point. I've verified that all integration tests succeed for locales that use a decimal comma in PHP 8.0, whereas they fail spectacularly in PHP 7.4.
I have updated the
rangeQuery()
function locally to make it locale-independent and added a unit test as proof of concept. It succeeds in PHP 7.4 and 8.0. These updates won't cause trouble down the road (or with the workaround in #262), but will become unnecessary when we drop support for PHP 7.4 after 2022-11-28.That's almost two years out. And there will probably be users stuck on PHP 7.4 for some time after that. Do you think it's still worth the effort to fix this?
Beta Was this translation helpful? Give feedback.
All reactions