<?php declare(strict_types=1);
/*
 * This file is part of PHPUnit.
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace PHPUnit\Event;

use ArrayIterator;
use Iterator;

final class Subscribers
{
    /**
     * @var array<string, array<int, Subscriber>>
     */
    private array $subscribers = [];

    public function add(Subscriber $subscriber): void
    {
        foreach ($subscriber->subscribesTo() as $type) {
            $this->subscribers[$type->asString()][] = $subscriber;
        }
    }

    /**
     * @return Iterator<Subscriber>
     */
    public function for(Type $type): Iterator
    {
        return new ArrayIterator($this->subscribers[$type->asString()]);
    }
}

<?php declare(strict_types=1);
/*
 * This file is part of PHPUnit.
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace PHPUnit\Event;

use function array_key_exists;
use ArrayIterator;
use EmptyIterator;
use Iterator;

final class Subscribers
{
    /**
     * @var array<string, array<int, Subscriber>>
     */
    private array $subscribers = [];

    public function add(Subscriber $subscriber): void
    {
        foreach ($subscriber->subscribesTo() as $type) {
            $this->subscribers[$type->asString()][] = $subscriber;
        }
    }

    /**
     * @return Iterator<Subscriber>
     */
    public function for(Type $type): Iterator
    {
        if (!array_key_exists($type->asString(), $this->subscribers)) {
            return new EmptyIterator();
        }

        return new ArrayIterator($this->subscribers[$type->asString()]);
    }
}

Return EmptyIterator when no subscribers have been added for type
Fix: Return EmptyIterator when no subscribers have been added for type

Co-authored-by: Andreas Möller <96e8155732e8324ae26f64d4516eb6fe696ac84f@localheinz.com>
Co-authored-by: Arne Blankerts <2d7739f42ebd62662a710577d3d9078342a69dee@Blankerts.de>
