Skip to content

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

Merged
jasonvarga merged 2 commits into
statamic:6.xfrom
VerburgtJimmy:fix/modify-tostring-non-string-scalar
Jun 9, 2026
Merged

[6.x] Fix Modify::__toString() throwing TypeError for non-string scalar values#14674
jasonvarga merged 2 commits into
statamic:6.xfrom
VerburgtJimmy:fix/modify-tostring-non-string-scalar

Conversation

@VerburgtJimmy

Copy link
Copy Markdown
Contributor

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:

@jasonvarga

Copy link
Copy Markdown
Member

Also fixed the same get_class()get_debug_type() issue in getIterator(), which had the same bug — the error-detection logic was correct but get_class() would throw a TypeError before ModifierException could be raised.

@jasonvarga jasonvarga enabled auto-merge (squash) June 9, 2026 19:42
@jasonvarga jasonvarga merged commit 7b93712 into statamic:6.x Jun 9, 2026
18 checks passed
@VerburgtJimmy

Copy link
Copy Markdown
Contributor Author

Just wanted to thank you for the feedback. First commit officially merged so kinda a milestone :)

@jasonvarga

Copy link
Copy Markdown
Member

Hey cool, congrats! 🎉

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

2 participants