Repository navigation
Coerce scalar properties without loss - #37
Merged
Merged
Conversation
Hydrators compile with strict_types; when the constructor rejects an argument, plain int, float, string and bool fields are converted only if lossless and the call is retried, so input of the declared type costs nothing extra. Lossy input throws DataHydrationException naming the field. Closes #24.
5 tasks
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.
A property typed
int,float,stringorboolwithout a cast now accepts only values that convert without loss:"3"and3.0intoint, a numeric string intofloat, int/float/Stringableintostring, and"false","no","0",""intoboolasfalse. Anything else throwsDataHydrationExceptionnaming the field ("Invalid value for field 'age': Cannot convert string to int without loss.").tryFrom()returnsnull, andfromResult()andwith()apply the same rules. In 2.x PHP's weak coercion applied, so"1.9"became1and"false"becametrue.How it works
from()hydrator is compiled withstrict_types, both throughevaland in the.meta.phpcache file, so values of the declared type go straight to the constructor with no extra checks.TypeError,ScalarCoercion::input()converts the plain scalar fields that need it and the call is retried once.TypeErroris rethrown (redacted as before), so aTypeErrorfrom the constructor body never runs the body twice.fromResult(),fromLazy(),with()and property assignments check the type inline.int|string) are checked strictly and not converted.ParameterMetagets ascalarTypefield with a__set_statedefault. The cache format is bumped tosdo-cache-5.Benchmark
Medians of 9 rounds against the base. Everything is within ±2%, except cold "Hydration: flat", where the base itself swings between 3.1M and 4.1M ops/s from run to run.
A first version checked the type inline in every field and cost 6–18% on nested data and collections; this design replaced it.
Mutation testing
The only escaped mutants are equivalent ones: whitespace or ordering in the generated
catch, and the int-range bound that guards the float→int cast against PHP 8.5's out-of-range warning.Docs:
hydration.md("Scalar Types"), UPGRADE-3.0, CHANGELOG, the upgrade skill.Closes #24.