Filtering a Doctrine collection that implements Selectable interface

Code
<?php

use PHPSnippets\Db\Entity\Test;
use 
Doctrine\ORM\Query;
use 
Doctrine\Common\Collections\Collection;
use 
Doctrine\Common\Collections\Selectable;
use 
Doctrine\Common\Collections\Criteria;
use 
Doctrine\Common\Collections\ArrayCollection;

// function for collection output
$print = function(Collection $result) {
    
$output = [];
    foreach (
$result as $entity) {
        
$output[] = $entity->name;
    }
    echo 
implode(', '$output) .
        
str_repeat(PHP_EOL2);
};

$repo $entityManager->getRepository(Test::CLASS);

// similar to findAll() but returns result as Collection
$collection $repo->matching(Criteria::create());
$print($collection);

if (
$collection instanceOf Selectable) {
    
// expression to filter collection
    
$expr Criteria::expr()->eq('name''foo');

    
$criteria Criteria::create();
    
$criteria->where($expr);

    
// This works directly with the collection
    
$collection2 $collection->matching($criteria);
    
$print($collection2);
}
Result
foo, test, bar

foo
Used Versions
PHP 8.2, Laminas MVC 3.2, Symfony 5.2, Laravel 8.28, PHPUnit 9.5, Doctrine ORM 2.8