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: Breaking: Differentiate between kilobyte/kibibyte and megabyte/mebibyte #9277

Open
wants to merge 18 commits into
base: 4.6
Choose a base branch
from

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

@kenjis
Copy link
Member

kenjis commented Nov 19, 2024

For the next PR, could you use a specific branch name (other than 4.6)?

4.6 is the branch name in this repository, so I cannot get your PR with gh pr checkout command.

$ gh pr checkout 9277
remote: Enumerating objects: 97, done.
remote: Counting objects: 100% (97/97), done.
remote: Compressing objects: 100% (65/65), done.
remote: Total 97 (delta 57), reused 70 (delta 32), pack-reused 0 (from 0)
Unpacking objects: 100% (97/97), 27.88 KiB | 68.00 KiB/s, done.
From github.com:codeigniter4/CodeIgniter4
 * branch                  refs/pull/9277/head -> FETCH_HEAD
hint: Diverging branches can't be fast-forwarded, you need to either:
hint:
hint: 	git merge --no-ff
hint:
hint: or:
hint:
hint: 	git rebase
hint:
hint: Disable this message with "git config advice.diverging false"
fatal: Not possible to fast-forward, aborting.
failed to run git: exit status 128

@kenjis kenjis removed the breaking change Pull requests that may break existing functionalities label Nov 19, 2024
@ThomasMeschke
Copy link
Contributor Author

For the next PR, could you use a specific branch name (other than 4.6)?

4.6 is the branch name in this repository, so I cannot get your PR with gh pr checkout command.

$ gh pr checkout 9277
remote: Enumerating objects: 97, done.
remote: Counting objects: 100% (97/97), done.
remote: Compressing objects: 100% (65/65), done.
remote: Total 97 (delta 57), reused 70 (delta 32), pack-reused 0 (from 0)
Unpacking objects: 100% (97/97), 27.88 KiB | 68.00 KiB/s, done.
From github.com:codeigniter4/CodeIgniter4
 * branch                  refs/pull/9277/head -> FETCH_HEAD
hint: Diverging branches can't be fast-forwarded, you need to either:
hint:
hint: 	git merge --no-ff
hint:
hint: or:
hint:
hint: 	git rebase
hint:
hint: Disable this message with "git config advice.diverging false"
fatal: Not possible to fast-forward, aborting.
failed to run git: exit status 128

Sure, will do, sorry for the inconvenience 😇

system/Files/FileSizeUnit.php Show resolved Hide resolved
system/Files/File.php Outdated Show resolved Hide resolved
system/Files/File.php Outdated Show resolved Hide resolved
* reorder cases and function
* add DocBlock for return and throws
@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

.. literalinclude:: files/005.php
:lines: 2-

A ``RuntimeException`` will be thrown if the file does not exist or an error occurs.

getSizeByBinaryUnit()
=====================

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.. versionadded:: 4.6.0


getSizeByMetricUnit()
=====================

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.. versionadded:: 4.6.0

@@ -277,6 +277,9 @@ Deprecations
- The properties ``$arguments`` and ``$argumentsClass`` of ``Filters`` have
been deprecated. No longer used.
- The ``Filters::getArguments()`` method has been deprecated. No longer used.
- **File:**
- The function ``getSizeByUnit()`` of ``File`` has been deprecated.
Use either ``getSizeByBinaryUnit()`` or ``getSizeByMetricUnit()`` instead.
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do. Two separate bullet point, or both in one?

Copy link
Member

Choose a reason for hiding this comment

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

One line would be sufficient.

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