Closed (fixed)
Project:
Drupal core
Version:
8.7.x-dev
Component:
base system
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
21 Sep 2018 at 07:18 UTC
Updated:
3 Dec 2018 at 09:59 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
mondrakeTest only patch. Actually it'd better if this test had a data provider instead, but that's yet another issue.
Comment #3
mondrakeComment #5
longwaveI think a negative size is OK. What if you had a diff system that as well as showing the changes in lines it showed the change in filesize? That could be a negative value.
Comment #6
mondrake#5 good thinking!
It looks like Bytes::toInt is converting negative $size to positive... a bit messy here.
Comment #7
mondrakeComment #8
mondrakeAlso, -1 is considered plural by formatPlural, so we get '-1 bytes' in place of '-1 byte'. But that's too far away from here.
Here a first step towards fix.
Comment #9
longwaveWe could do something like
but not sure if this is an improvement, maybe just wait for #1131926: format_plural returns a plural value for a negative singular
Comment #12
mondrakeDid not know about that, thanks!
Let's keep the workaround here FTTB. This one should be close now.
Comment #14
mondrakeZettabytes and Yottabytes are kinda stretching PHP's type casting features... :)
Comment #15
mondrakeComment #16
mondrakeActually better.
Comment #18
dawehnerWhat is the reason for putting
$expected == $resultin here? PHPUnit provides this information out of the box.To be honest I think we should improve the readability of this test and assign
$stringoutside.Comment #19
mondrake@dawehner I'd do that in a separate issue. This test class deserves some love, rejuvenating it to proper PHPUnit concepts - first and foremost introducing @dataproviders for making tests more atomic.
Comment #20
dawehnerFair :)
Comment #21
mondrakeFiled #3001846: Refactor KernelTests/Core/Common/SizeTest.
Comment #22
alexpottI'm not entirely convinced by #5. I'd rather deprecate support for negative amounts and leave it up to the caller on how to handle negative bytes. So we don't change behaviour in Drupal 8 but in Drupal 9 we through an exception of something. I think it would also be useful to have a look at other PHP libraries and see how they handle the negative case.
Comment #23
mondrakeagree, keep it simple. Will reroll then.
Comment #24
mondrakeAdded draft CR https://www.drupal.org/node/3004611
Comment #25
mondrakeHere it is. Updated IS with the deprecation decision.
Comment #26
neclimdul+ if ($size < 0) {
+ @trigger_error('Using format_size with negative $size argument is deprecated in Drupal 8.7.0 and will throw an exception in Drupal 9.0.0. Instead, handle the case of negative quantity beforehand and always pass a positive value. See https://www.drupal.org/node/3004611', E_USER_DEPRECATED);
+ }
If I had a quota of 2Gb of space and used 2.1Gb would I not have -0.1Gb of space? I think the assertion that you can't have negative sizes is incorrect.
Comment #27
neclimdulIn fact, I think that should definitely be removed. There may a features that can't allow negative sizes but we absolutely have to be able to format a string with a negative size. The alternative is that a developer basically has to write:
And that just doesn't make sense.
Comment #28
mondrakeLet's have more reviews before jumping into patching... there are two opposite views ATM.
Comment #29
mondrakeBTW the behaviour expected in #26 i.e. "-0.1 GB" is not what happens currently... negative value is just broken.
Comment #30
longwaveAgree with @neclimdul that there will be use cases where negative values make sense. I don't see how it can harm anyone by supporting negative values here, and if we start throwing exceptions (in D9) then there will be edge cases where somehow someone has ended up with e.g. a negative quota, and we would error when displaying when we don't strictly need to.
The only contentious point I feel is "-1 byte" vs "-1 bytes" but that already has its own issue.
Comment #31
neclimdullongwave described my intended point better than I did. The expected output wasn't as important as the fact that it wouldn't error unexpectedly. Correct output would be nice though ;)
Comment #32
alexpottIt's not broken as in producing an error - it just always outputs bytes...
I'm okay with fix the thing to do
-9.54 MBbut we should leave the -1 byte/s thing well alone. And fix/discuss that in the other issue.Whilst working on this I found a bug in really really large sizes > 1024 YB fixed that as well.
I've not included any change to \Drupal\Component\Utility\Bytes::toInt since I think that should have it's own issue. The change in #15 for example will result in breakages of 15 P-bytes whereas before that'd work!?!?!
Comment #33
krzysztof domańskiSmall refactoring: I did not change the logic of code. I only removed
elsein the following code and IMO unnecessaryswitchstatement. The code is a slightly cleaner now.In addition, there were no s at the end of kilobyte
Comment #34
alexpottChanging this removes the ability to translate the units KB/MB/GB etc... which must be why that is done like this.
Removing the else makes sense.
Comment #35
krzysztof domańskiIgnore patch #33. Removing else and correction of the comment.
Comment #38
mondrake@alexpott re #32
I do not think we'll fix that in the other issue, see #1131926-31: format_plural returns a plural value for a negative singular. Do we need another separate issue?
Comment #39
mondrake#36 and #37 are unrelated failures. I’d RTBC but cannot due to earlier work on the patch.
Comment #40
goodboy commentedComment #41
volegerRerolled.
Addressed #40
Fixed CS errors: Reformatted the switch/case construction.
Should the
case 'YB':value be replaced withdefault:to avoid "Missing return statement" warning in IDE?Comment #42
mondrakeLet's get this in so we can work on the conversion to OO. Retracted the CR that was drafted in #24.
I don't think so, the block of code covers all possibilities without needing a default - which is not mandatory anyway for PHP.
Comment #43
alexpottCommitted and pushed d29f046424 to 8.7.x and 1d78e9d5b6 to 8.6.x. Thanks!