<commit_before><?php
/**
 *
 */

namespace Mvc5\Event;

use Iterator;
use Mvc5\Signal as _Signal;

trait Generator
{
    /**
     *
     */
    use _Signal;

    /**
     * @param array|callable|object|string $config
     * @return callable|null
     */
    protected abstract function callable($config) : callable;

    /**
     * @param callable|Event|string $event
     * @param callable $listener
     * @param array $args
     * @param callable $callback
     * @return mixed
     */
    protected function emit($event, callable $listener, array $args = [], callable $callback = null)
    {
        return $event instanceof Event ? $event($listener, $args, $callback) : $this->signal($listener, $args, $callback);
    }

    /**
     * @param array|string|Iterator $event
     * @param array $args
     * @param callable $callback
     * @return mixed|null
     */
    protected function generate($event, array $args = [], callable $callback = null)
    {
        return $this->traverse($event, $this->queue($event, $args), $args, $callback);
    }

    /**
     * @param $listener
     * @param $event
     * @param $queue
     * @param $args
     * @param $callback
     * @param null $result
     * @return null
     */
    protected function iterate($listener, $event, $queue, $args, $callback, $result = null)
    {
        return !$listener || ($event instanceof Event && $event->stopped()) ? $result : $this->iterate(
            $this->step($queue), $event, $queue, $args, $callback, $this->result($event, $listener, $args, $callback)
        );
    }

    /**
     * @param Event|object|string $event
     * @param array $args
     * @return array|Iterator|null
     */
    protected abstract function iterator($event, array $args = []);

    /**
     * @param array|Event|object|string|Iterator $event
     * @param array $args
     * @return array|Iterator
     */
    protected function queue($event, array $args = [])
    {
        return is_array($event) || $event instanceof Iterator ? $event : $this->iterator($event, $args);
    }

    /**
     * @param array|Event|object|Iterator $event
     * @param $listener
     * @param array $args
     * @param callable $callback
     * @return mixed
     */
    protected function result($event, $listener, array $args = [], callable $callback = null)
    {
        return $this->emit($event, $this->callable($listener), $args, $callback);
    }

    /**
     * @param array|Iterator $queue
     * @return mixed|null
     */
    protected function start($queue)
    {
        return is_array($queue) ? current($queue) : $queue->current();
    }

    /**
     * @param array|Iterator $queue
     * @return mixed|null
     */
    protected function step(&$queue)
    {
        if (is_array($queue)) {
            return next($queue);
        }

        $queue->next();

        return $queue->current();
    }

    /**
     * @param $event<commit_msg>fix repeat iterator class events<commit_after><?php
/**
 *
 */

namespace Mvc5\Event;

use Iterator;
use Mvc5\Signal as _Signal;

trait Generator
{
    /**
     *
     */
    use _Signal;

    /**
     * @param array|callable|object|string $config
     * @return callable|null
     */
    protected abstract function callable($config) : callable;

    /**
     * @param callable|Event|string $event
     * @param callable $listener
     * @param array $args
     * @param callable $callback
     * @return mixed
     */
    protected function emit($event, callable $listener, array $args = [], callable $callback = null)
    {
        return $event instanceof Event ? $event($listener, $args, $callback) : $this->signal($listener, $args, $callback);
    }

    /**
     * @param array|string|Iterator $event
     * @param array $args
     * @param callable $callback
     * @return mixed|null
     */
    protected function generate($event, array $args = [], callable $callback = null)
    {
        return $this->traverse($event, $this->queue($event, $args), $args, $callback);
    }

    /**
     * @param $listener
     * @param $event
     * @param $queue
     * @param $args
     * @param $callback
     * @param null $result
     * @return null
     */
    protected function iterate($listener, $event, $queue, $args, $callback, $result = null)
    {
        return !$listener || ($event instanceof Event && $event->stopped()) ? $result : $this->iterate(
            $this->step($queue), $event, $queue, $args, $callback, $this->result($event, $listener, $args, $callback)
        );
    }

    /**
     * @param Event|object|string $event
     * @param array $args
     * @return array|Iterator|null
     */
    protected abstract function iterator($event, array $args = []);

    /**
     * @param array|Event|object|string|Iterator $event
     * @param array $args
     * @return array|Iterator
     */
    protected function queue($event, array $args = [])
    {
        return is_array($event) || $event instanceof Iterator ? $event : $this->iterator($event, $args);
    }

    /**
     * @param array|Event|object|Iterator $event
     * @param $listener
     * @param array $args
     * @param callable $callback
     * @return mixed
     */
    protected function result($event, $listener, array $args = [], callable $callback = null)
    {
        return $this->emit($event, $this->callable($listener), $args, $callback);
    }

    /**
     * @param array|Iterator $queue
     * @return mixed|null
     */
    protected function start(&$queue)
    {
        if (is_array($queue)) {
            return reset($queue);
        }

        $queue->rewind();

        return $queue->current();
    }

    /**
     * @param array|Iterator $queue
     * @return mixed|null
     */
    protected function step(&$queue)
    {
        if (is_array($queue)) {
            return next($queue);
        }

        $queue->next();

        return $queue->current();
    }

    /**
     * @param $event
{'fraction_non_alphanumeric': 0.15350803043110736, 'fraction_numerical': 0.000676246830092984, 'mean_word_length': 2.3941480206540446, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6402614', 'n_tokens_mistral': 1763, 'n_tokens_neox': 1736, 'n_words': 434}