fix(parser): always return altitude as a float - #21
Merged
Conversation
A coordinate declaring an altitude produced a float, one omitting it produced the integer 0, so the type of the same key depended on the input. The array shape documented on parsePointCoordinates() claims float in both cases, and PHPStan believed it. Callers comparing strictly, or encoding to JSON and diffing the result, saw 0 where they had been told to expect 0.0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
7.7,45.8,12gives12.0, a float.7.7,45.8gives0, an integer. Same key, type depending on the input.The docblock right above it says otherwise:
So PHPStan was being told
floatand the code was returningint, which is exactly the kind of thing static analysis is supposed to catch and could not, because the cast is inside a ternary it has no reason to doubt.Found while documenting the return shapes for #20.
Effect on callers
0.0fails where the file omitted the altitude.json_encode()emits0rather than0.0, so a GeoJSON payload diffed against a fixture is unstable depending on whether the source coordinates carried a third component.Fix
: 0.0. Both occurrences,parsePointCoordinates()andparseLineStringCoordinates(), which had the same expression.Tests
tests/AltitudeTest.php:12.0, when the coordinate declares it[7.7, 45.8, 0.0]Suite 62 to 65. PHPStan and Pint clean.
Breaking change
Only in the sense that
0 === $altitudeno longer holds andjson_encode()now writes0.0where it wrote0. Both are the documented behaviour rather than a change to it, but a strict test fixture somewhere could notice, so it belongs in the changelog and in the next major rather than in a patch.