Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Differentiate between kilobyte/kibibyte and megabyte/mebibyte #9277

Merged
merged 18 commits into from
Dec 4, 2024

Conversation

ThomasMeschke
Copy link
Contributor

@ThomasMeschke ThomasMeschke commented Nov 13, 2024

Notice
This originated from another PR: #9271

Description
Noticed that the file size calculation mixes up the SI unit prefixes with the NIST/IEC unit prefixes, by taking "kb" and "mb" but calculating "kib" and "mib".
Fixed by providing both possibilities.

This therefore will break existing code!

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Copy link
Contributor

@neznaika0 neznaika0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Find all places with method.
user_guide_src/source/libraries/files.rst
user_guide_src/source/libraries/uploaded_files.rst
user_guide_src/source/libraries/files/005.php
user_guide_src/source/libraries/files/013.php

add changelog to user_guide_src/source/changelogs/v4.6.0.rst

@ThomasMeschke
Copy link
Contributor Author

Will do, will add.

@kenjis kenjis added breaking change Pull requests that may break existing functionalities enhancement PRs that improve existing functionalities labels Nov 14, 2024
@kenjis kenjis changed the title refactor: Breaking: Differentiate between kilobyte/kibibyte and megabyte/mebibyte feat: Breaking: Differentiate between kilobyte/kibibyte and megabyte/mebibyte Nov 14, 2024
@kenjis
Copy link
Member

kenjis commented Nov 14, 2024

Why do you need this enhancement?
Frankly, many devs do not seem to need this.
It seems in this framework "kb" means 1024 bytes.

@ThomasMeschke
Copy link
Contributor Author

Why do you need this enhancement? Frankly, many devs do not seem to need this. It seems in this framework "kb" means 1024 bytes.

#9271 (comment)
Stumbled across this due to a mismatch with another component in use.
"Need" is not how I would see it. More like a "really want to have it right".

@kenjis
Copy link
Member

kenjis commented Nov 14, 2024

I got your intention.
But we do not prefer breaking changes.
Strictly speaking, we do not allow any breaking changes in minor/patch updates.
But if we think it is a bug, we can fix it in minor updates.

I don't think it is worth adding this breaking change. Because many devs do not need this feature.
What do you think to add a new method or new method parameter for kib/mib?

@kenjis
Copy link
Member

kenjis commented Nov 14, 2024

How do we address number_to_size? #9271 (comment)

@ThomasMeschke
Copy link
Contributor Author

ThomasMeschke commented Nov 14, 2024

I got your intention. But we do not prefer breaking changes. Strictly speaking, we do not allow any breaking changes in minor/patch updates. But if we think it is a bug, we can fix it in minor updates.

I don't think it is worth adding this breaking change. Because many devs do not need this feature. What do you think to add a new method or new method parameter for kib/mib?

I might be getting you wrong, but new parameter is exactly what I did, didn't I?
Or do you mean leave "kb" and "mb" for 1024 an use new ones for 1000?
What would they be? I fear this then gets even more "wrong" or at least confusing than it already is 😕

Regarding number_to_size():
We could introduce two new methods: number_to_bytes_binary and number_to_bytes_metric, let number_to_size internally call the metric binary edition, so its behavior does not change, and mark it as deprecated.

@kenjis
Copy link
Member

kenjis commented Nov 14, 2024

If we merge this PR, existing apps may break. Because the behavior will change.

-$kilobytes = $file->getSizeByUnit('kb'); // 250.880
+$kilobytes = $file->getSizeByUnit('kb'); // 256.901

This is a breaking change, and we would like to avoid to break existing apps as possible.

Adding a new parameter means something like this:

$kilobytes = $file->getSizeByUnit('kb'); // 250.880
$kilobytes = $file->getSizeByUnit('kb', false); // 256.901

This does not break existing apps.

@ThomasMeschke
Copy link
Contributor Author

Ah, I see. Well, I personally think introducing breaking changes from time to time is unavoidable, but I can absolutely see the impact vs benefit side of this very instance.
Other option would be the same as for number_to_size, introduce a getSizeByUnitMetric and getSizeByUnitBinary, both allowing "kb" and "mb", calculating differently, and the let getSizeByUnit internally call the Binary version for now and mark it as deprecated.

@kenjis
Copy link
Member

kenjis commented Nov 15, 2024

Yes, adding new method(s) is an option.

@neznaika0
Copy link
Contributor

"Boolean" methods are usually better separated. This is clearly visible in the code

@ThomasMeschke
Copy link
Contributor Author

"Boolean" methods are usually better separated. This is clearly visible in the code

Yeah, also not a big fan of bool params to distinguish between behaviors. It would also violate the S in SOLID.
So, if you are fine, I would rework the change to the two-method approach?

Copy link
Contributor

@warcooft warcooft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a new parameter $precision to the this method? This would make it more flexible and allow for better control over the precision of the output.

system/Files/File.php Outdated Show resolved Hide resolved
system/Files/File.php Show resolved Hide resolved
system/Files/File.php Outdated Show resolved Hide resolved
system/Files/File.php Outdated Show resolved Hide resolved
system/Files/File.php Outdated Show resolved Hide resolved
@neznaika0
Copy link
Contributor

And @phpstan-param positive-int $precision

@ThomasMeschke
Copy link
Contributor Author

ThomasMeschke commented Nov 19, 2024

Somebody motivated to enrich the contribution guide about how to run all the code checks locally? 😇

Edit: Just took a look into the running checks and noticed the composer scripts. Didn't even know those were a thing 🤯

system/Files/File.php Outdated Show resolved Hide resolved
system/Files/File.php Outdated Show resolved Hide resolved
system/Files/File.php Outdated Show resolved Hide resolved
system/Files/File.php Outdated Show resolved Hide resolved
system/Files/FileSizeUnit.php Outdated Show resolved Hide resolved
system/Files/File.php Outdated Show resolved Hide resolved
@paulbalandan
Copy link
Member

@ThomasMeschke could you address the remaining review comments? and squash the commits? so we can merge this PR

@ThomasMeschke
Copy link
Contributor Author

Which comments do you mean? I though I got all of them...

@michalsn
Copy link
Member

michalsn commented Dec 2, 2024

@ThomasMeschke Well, that's strange - usually, GitHub would resolve a conversation automatically if the changes to the files were made, but here for some reason, it didn't happen. The unresolved conversations are an indicator for us that not all requested changes were implemented. In the future, please resolve the conversation manually if you see that your changes weren't taken into account.

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can squash on merge.

@michalsn michalsn changed the title feat: Breaking: Differentiate between kilobyte/kibibyte and megabyte/mebibyte feat: Differentiate between kilobyte/kibibyte and megabyte/mebibyte Dec 2, 2024
Copy link
Member

@paulbalandan paulbalandan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I didn't see it was already resolved albeit no indication it was resolved. 😅

@paulbalandan paulbalandan requested review from kenjis, warcooft, paulbalandan and neznaika0 and removed request for warcooft, paulbalandan and neznaika0 December 2, 2024 10:08
@paulbalandan paulbalandan merged commit 455a559 into codeigniter4:4.6 Dec 4, 2024
46 checks passed
@paulbalandan
Copy link
Member

Thank you, @ThomasMeschke

@ThomasMeschke ThomasMeschke deleted the 4.6 branch December 4, 2024 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.6 enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants