Repository navigation
TPropertyValue: a value without a string form passes through coercion, and ensureString() throws for it (#1497) - #1509
Merged
Conversation
…ough unchanged (pradosoft#1497) An array given to a union without an array or iterable member, and an object without __toString() that no member accepts, reach the setter unchanged, so a typed setter raises PHP's TypeError.
… value without a string form (pradosoft#1497) An array or an object without __toString() throws propertyvalue_invalid_string_type, with warnings reported or masked. coerceToType() passes such a value through unchanged for string and ?string, so a typed setter raises PHP's TypeError.
12 of 69 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.
Part of #1497. Two commits, to be reviewed separately.
1. Unions: a value no member can hold passes through unchanged
TPropertyValue::coerceToType(['a'], int|string)raisedTPhpErrorException: Array to string conversion, and an object without__toString()raised PHP's\Error. Step 4 of_coerceUnionType()returned an array only for a union with anarraymember and otherwise fell through to a string cast.Such a value is now returned unchanged, the miss style
_coerceToClass()already documents, so a typed setter raises PHP's ownTypeError("must be of type string|int, array given") and the property stays unassigned.array|stringanditerable|stringkeep passing an array through, and nullable unions,ICoercible, enums,Stringableobjects, andTJavaScriptLiteralbehave as before. Two cases that threw now succeed because the setter accepts the value: an object toobject|int, and a callable array tocallable|string.2. Single
string:ensureString()throwsTInvalidDataTypeExceptionThe same defect existed for
stringand?stringthroughensureString(), whose(string)cast raised the warning, or returned'Array'when warnings were masked.coerceToType()tostringor?stringpasses an array or an object without__toString()through, as in commit 1.ensureString()called directly throwsTInvalidDataTypeExceptionwith the newpropertyvalue_invalid_string_type, "Value of type '{0}' cannot be converted to a string." Most setters are untyped and storeensureString($value), so pass-through there would store the array. It throws the same way with warnings reported or masked.ensureString()does is unchanged: bool,NAN, null, numbers,Stringableobjects,TJavaScriptLiteral.Behavior to review:
ensureString($array)returned'Array'; it now throws.TException::__construct()converts each message argument withensureString().new TFooException('code', $array)throwsTInvalidDataTypeException; with warnings reported it already threwTPhpErrorException. Rendering such an argument as its type is a possible follow-up.framework/passes an array or such an object toensureString()on purpose, and no existing test does. Sites where input can be an array, such as a POST fieldfield[]inTWebTemplate::loadPostData(), now get the clear exception where they got the warning exception.Regression tests for both commits; they pass on PHP 8.2 through 8.5.