Add internal IniUtil helper class to convert ini size settings to bytes#276
Conversation
|
Since this function is going to be used for parsing user input, there should probably at least some basic guards against invalid input. Maybe we should throw a |
|
@jsor good point, added tests for that 👍 |
src/Util.php
Outdated
| $strippedSize = substr($size, 0, -1); | ||
|
|
||
| if (!is_numeric($strippedSize)) { | ||
| throw new \InvalidArgumentException('"' . $strippedSize . '" is not a valid ini size'); |
There was a problem hiding this comment.
The exception message should probably use the passed $size.
There was a problem hiding this comment.
Also, that fails for single-digits.
There was a problem hiding this comment.
And also addressed the single digit issue 👍
clue
left a comment
There was a problem hiding this comment.
Motivation for this PR is a bit unclear on its own, does it make sense to update the related classes as part of this PR?
src/Util.php
Outdated
| { | ||
| if (is_numeric($size) && $size <= 0) { | ||
| throw new \InvalidArgumentException('Expect "' . $size . '" to be higher isn\'t zero or lower'); | ||
| } |
There was a problem hiding this comment.
What is the motivation for this check? memory_limit can be set to -1 to allow unlimited memory (http://php.net/manual/en/ini.core.php#ini.memory-limit), while other ini settings do not allow this.
Should this be covered by this method?
There was a problem hiding this comment.
Good point, motivation for it was to catch invalid values but it escaped my mind that -1 is a valid value
src/Util.php
Outdated
| /** | ||
| * @internal | ||
| */ | ||
| final class Util |
There was a problem hiding this comment.
Not a big fan of a "utility" class, but given this is @internal only, I won't block this and would rather clean this up in a follow-up PR 👍
Does it make sense to move this to Io namespace so this is less prominent?
There was a problem hiding this comment.
Neither am I, another option would be to create a functions file. Moved it under Io
|
@clue The initial idea was to add this method first and then file follow up PR for updating uses. But as suggested I've updated the related classes within this PR now 👍 |
clue
left a comment
There was a problem hiding this comment.
Thanks for the quick update! Can you squash this to a reasonable number of commits, changes LGTM otherwise
👍
a87e3eb to
2d951f0
Compare
|
Cheers, rebased and squashed into one commit 👍 . Ping @jsor |
src/Io/MultipartParser.php
Outdated
| } | ||
|
|
||
| $this->uploadMaxFilesize = $uploadMaxFilesize === null ? $this->iniUploadMaxFilesize() : (int)$uploadMaxFilesize; | ||
| $this->uploadMaxFilesize = $uploadMaxFilesize === null ? Util::iniSizeToBytes(ini_get('upload_max_filesize')) : Util::iniSizeToBytes($uploadMaxFilesize); |
There was a problem hiding this comment.
This can be simplified by moving this out of the ternary. Also, needs tests?
There was a problem hiding this comment.
Updated a test to test this change
| $sizeLimit = Util::iniSizeToBytes(ini_get('post_max_size')); | ||
| } | ||
|
|
||
| $this->sizeLimit = $sizeLimit; |
There was a problem hiding this comment.
Note that explicitly giving a size here does not allow size suffixes. Also needs tests?
There was a problem hiding this comment.
Yeah seems that slipped through, fixed it here 👍
|
@clue Updated the PR, let me know if this is good and I'll squash again |
clue
left a comment
There was a problem hiding this comment.
Thanks for the quick update and sorry for the confusion! Can you squash this, otherwise LGTM? ![]()
|
Renamed |
133d629 to
d6592cc
Compare
d6592cc to
1f54423
Compare
1f54423 to
1439879
Compare
clue
left a comment
There was a problem hiding this comment.
Changes LGTM, thanks for the keeping up with this!
👍
Created Util::iniSizeToBytes() helper so we don't have to copy the same internal method everywhere. A follow up PR can take care of #275. This will also be used in #271.