[6.x] Fix Modify::__toString() throwing TypeError for non-string scalar values#14674
Open
VerburgtJimmy wants to merge 1 commit into
Open
[6.x] Fix Modify::__toString() throwing TypeError for non-string scalar values#14674VerburgtJimmy wants to merge 1 commit into
VerburgtJimmy wants to merge 1 commit into
Conversation
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.
Closes #14671.
Statamic\Modifiers\Modify::__toString()was callingmethod_exists($this->value, '__toString')without first checking that$this->valueis an object or string. PHP 8+ mademethod_exists()strictly typed, so it throws aTypeErroron ints, floats, bools, null, and arrays. That means something like(string) Statamic::modify(0)->formatMoney()blows up any time the chain returns an int, which is exactly what Cargo'sformatMoneymodifier does for a zero price.Fix
int/float/bool/string) andnullto string directly.method_exists()when the value is actually an object.ModifierExceptionnow usesget_debug_type()instead ofget_class(), so the error path doesn't itself throw when the value is an array or scalar.Tests
Added to
tests/Modifiers/FluentModifyTest.php:0, positive/negative ints, a float,true/false,null, and plain strings.Modify::value(0)->add(5)→"5"(the example from Statamic::modify() throws TypeError when value is integer 0 #14671).ModifierExceptionwith the expected message.