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

Change Index Criteria to Not Allow Floats. Add Tests. #25

Merged
merged 1 commit into from
Mar 14, 2024

Conversation

markrogoyski
Copy link
Contributor

I tightened up the logic for what a valid index is. The previous logic allowed floats, but it did not seem like float indexes made sense, as PHP cannot have floats as an array index, and the previous behavior was casting the float to an int and using that as the index value.

return $this->source[$this->convertIndex(\intval($offset))];

Consider if this is the behavior you want.

For reference, if you cast a float to an int:

php > echo intval(1.4);
1
php > echo intval(1.5);
1
php > echo intval(1.9);
1
php > echo intval(-1.9);
-1

Added some tests to show that floats and numeric floats as well as special floats no longer work.

For reference in Python:

>>> [1, 2, 3][1.5]
<stdin>:1: SyntaxWarning: list indices must be integers or slices, not float; perhaps you missed a comma?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not float

Also, it was not clear what the distinction between KeyError and IndexError is. The unit tests assert what is currently being thrown.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 8275852171

Details

  • 3 of 3 (100.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.4%) to 99.561%

Files with Coverage Reduction New Missed Lines %
src/Views/ArrayView.php 1 98.63%
Totals Coverage Status
Change from base Build 8269254992: -0.4%
Covered Lines: 227
Relevant Lines: 228

💛 - Coveralls

@Smoren Smoren merged commit 5d4ac50 into Smoren:master Mar 14, 2024
7 checks passed
@Smoren
Copy link
Owner

Smoren commented Mar 14, 2024

Thank you @markrogoyski for you PR! I merged it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants