sebastian/object-enumerator
Traverses array structures and object graphs to enumerate all referenced objects, helping you inspect, analyze, or collect objects reachable from complex data structures. Install via Composer for production or as a dev dependency for testing and tooling.
Start by installing the package via Composer (typically as a dev dependency, since this is primarily used in testing and debugging tooling):
composer require --dev sebastian/object-enumerator
The core use case is integrating it into test assertions or debugging tools where you need to inspect all objects referenced by a variable (including nested objects and circular references). For example, when writing PHPUnit tests that validate deep object graph state—like in Doctrine ORM or Symfony services—it’s essential to ensure no unexpected side objects are retained.
First usage example:
use SebastianBergmann\ObjectEnumerator\Enumerator;
$enumerator = new Enumerator();
$objects = $enumerator->enumerate($someComplexObject);
foreach ($objects as $object) {
echo get_class($object) . "\n";
}
Check src/Enumerator.php for the class definition and tests/ for real-world usage (the package is heavily used by PHPUnit internally).
enumerate() to verify that a fixture isn’t leaking references (e.g., to avoid memory bloat or accidental mutation).spl_object_hash() to deduplicate or track object identity across complex structures.Common pattern:
$enumerator = new Enumerator();
foreach ($enumerator->enumerate($payload) as $object) {
if (is_object($object) && method_exists($object, 'getId')) {
$ids[] = $object->getId();
}
}
It’s especially valuable in test suites where assertions like assertObjectNotCloneable() or assertGraphContainsOnly() are implemented.
array_filter($objects, fn($o) => in_array(...))).dd()/dump() (Symfony VarDumper) for interactive debugging—this package is for programmatic enumeration, not human-readable output.Enumerator classes, alias carefully:
use SebastianBergmann\ObjectEnumerator\Enumerator as ObjectEnumerator;
How can I help you explore Laravel packages today?