Skip to content

Fix filled(...) throws exception when attribute holds a non-Stringable object#9

Merged
stevebauman merged 1 commit into
masterfrom
bug-8
May 25, 2026
Merged

Fix filled(...) throws exception when attribute holds a non-Stringable object#9
stevebauman merged 1 commit into
masterfrom
bug-8

Conversation

@stevebauman
Copy link
Copy Markdown
Member

@stevebauman stevebauman commented May 25, 2026

Summary

Fixes a TypeError thrown by DummyData::filled() (and isNotFilled(), notFilled(), anyFilled()) when an attribute holds an object that does not implement __toString — most commonly a DateTime returned from Faker\Generator::dateTimeBetween().

Closes #8

Problem

DummyData::isEmptyString() guarded against bool and array before casting the value to a string, but not against objects. Any object without __toString() triggered:

Error: Object of class DateTime could not be converted to string
  at src/DummyData.php:317
  at src/DummyData.php:157

This surfaced for consumers using HasDummyFactory whose toDummyInstance() called $attributes->filled($key) on a key produced by Faker's date-time helpers.

Change

Added ! is_object($value) to the guard in isEmptyString(). Objects are now treated as filled, which matches the semantic intent of filled() — an object is a real, non-empty value.

return ! is_bool($value)
    && ! is_array($value)
    && ! is_object($value)
    && trim((string) $value) === '';

@stevebauman stevebauman changed the title Fix filled() crash when attribute holds a non-Stringable object Fix filled(...) throws exception when attribute holds a non-Stringable object May 25, 2026
@stevebauman stevebauman merged commit 9648c02 into master May 25, 2026
1 check passed
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.

$data->filled() throws "Object could not be converted to string" when attribute holds a non-Stringable object

1 participant