Skip to content

[6.x] Fix Modify::__toString() throwing TypeError for non-string scalar values#14674

Open
VerburgtJimmy wants to merge 1 commit into
statamic:6.xfrom
VerburgtJimmy:fix/modify-tostring-non-string-scalar
Open

[6.x] Fix Modify::__toString() throwing TypeError for non-string scalar values#14674
VerburgtJimmy wants to merge 1 commit into
statamic:6.xfrom
VerburgtJimmy:fix/modify-tostring-non-string-scalar

Conversation

@VerburgtJimmy
Copy link
Copy Markdown

Closes #14671.

Statamic\Modifiers\Modify::__toString() was calling method_exists($this->value, '__toString') without first checking that $this->value is an object or string. PHP 8+ made method_exists() strictly typed, so it throws a TypeError on 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's formatMoney modifier does for a zero price.

Fix

  • Cast scalars (int/float/bool/string) and null to string directly.
  • Only call method_exists() when the value is actually an object.
  • The fallback ModifierException now uses get_debug_type() instead of get_class(), so the error path doesn't itself throw when the value is an array or scalar.

Tests

Added to tests/Modifiers/FluentModifyTest.php:

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.

Statamic::modify() throws TypeError when value is integer 0

1 participant