Summary
DummyData::filled() (and by extension isNotFilled(), notFilled(), anyFilled()) throws a TypeError when the attribute being checked holds an object that does not implement __toString — for example a DateTime, DateTimeImmutable, or a Carbon CarbonInterface returned from Faker\Generator::dateTimeBetween().
Steps to reproduce
use DirectoryTree\Dummy\DummyData;
$data = new DummyData([
'dateOfBirth' => new DateTime,
]);
$data->filled('dateOfBirth');
Expected behaviour
filled('dateOfBirth') returns true — the attribute holds a real, non-empty value.
Actual behaviour
A fatal error is thrown:
Error: Object of class DateTime could not be converted to string
at vendor/directorytree/dummy/src/DummyData.php:317
at vendor/directorytree/dummy/src/DummyData.php:157
Root cause
DummyData::isEmptyString() guards against bool and array values before casting to string, but does not guard against objects:
return ! is_bool($value)
&& ! is_array($value)
&& trim((string) $value) === '';
Any object without a __toString() method (e.g. DateTime) triggers the cast and crashes.
Impact
Affects any consumer using HasDummyFactory whose toDummyInstance() calls $attributes->filled($key) on a key produced by Faker's dateTimeBetween(), dateTime(), or any factory-defined value that is an object.
Fix
Also exclude objects from the string-coercion check in isEmptyString(); treat objects as filled.
Environment
- Package:
directorytree/dummy
- PHP: 8.x
Summary
DummyData::filled()(and by extensionisNotFilled(),notFilled(),anyFilled()) throws aTypeErrorwhen the attribute being checked holds an object that does not implement__toString— for example aDateTime,DateTimeImmutable, or a CarbonCarbonInterfacereturned fromFaker\Generator::dateTimeBetween().Steps to reproduce
Expected behaviour
filled('dateOfBirth')returnstrue— the attribute holds a real, non-empty value.Actual behaviour
A fatal error is thrown:
Root cause
DummyData::isEmptyString()guards againstboolandarrayvalues before casting to string, but does not guard against objects:Any object without a
__toString()method (e.g.DateTime) triggers the cast and crashes.Impact
Affects any consumer using
HasDummyFactorywhosetoDummyInstance()calls$attributes->filled($key)on a key produced by Faker'sdateTimeBetween(),dateTime(), or any factory-defined value that is an object.Fix
Also exclude objects from the string-coercion check in
isEmptyString(); treat objects as filled.Environment
directorytree/dummy