<commit_before><?php

namespace EventSource;

trait EventSourceTrait
{
    private $handlers = [];

    /**
     * @param string[] $eventNames
     */
    protected function initialize(array $eventNames)
    {
        if (!empty($this->handlers)) {
            throw new \BadMethodCallException('Event source is already initialized');
        }
        foreach ($eventNames as $eventName) {
            $this->handlers[ $eventName ] = [];
        }
    }

    /**
     * Add a handler to a specific event
     *
     * @param          $eventName
     * @param callable $handler
     */
    public function on($eventName, callable $handler)
    {
        $this->guardEvent($eventName);
        $this->handlers[ $eventName ][] = $handler;
    }

    /**
     * Remove an event handler
     *
     * @param          $eventName
     * @param callable $handler
     */
    public function remove($eventName, callable $handler)
    {
        if (isset($this->handlers[ $eventName ])) {
            $index = array_search($handler, $this->handlers[ $eventName ]);
            if ($index !== false) {
                unset($this->handlers[ $eventName ][ $index ]);
            }
        }
    }

    /**
     * Raise an event with an additional optional parameter
     *
     * @param          $eventName
     * @param mixed    $parameter
     */
    protected function raise($eventName, $parameter = null)
    {
        $this->guardEvent($eventName);
        foreach ($this->handlers[ $eventName ] as $handler) {
            $handler($parameter);
        }
    }

    /**
     * @param $eventName
     */
    private function guardEvent($eventName)
    {
        if (!isset($this->handlers[ $eventName ])) {
            throw new \InvalidArgumentException("Event '{$eventName}' is not defined");
        }
    }
}<commit_msg>Use call_user_func to allow some special callback definitions<commit_after><?php

namespace EventSource;

trait EventSourceTrait
{
    private $handlers = [];

    /**
     * @param string[] $eventNames
     */
    protected function initialize(array $eventNames)
    {
        if (!empty($this->handlers)) {
            throw new \BadMethodCallException('Event source is already initialized');
        }
        foreach ($eventNames as $eventName) {
            $this->handlers[ $eventName ] = [];
        }
    }

    /**
     * Add a handler to a specific event
     *
     * @param          $eventName
     * @param callable $handler
     */
    public function on($eventName, callable $handler)
    {
        $this->guardEvent($eventName);
        $this->handlers[ $eventName ][] = $handler;
    }

    /**
     * Remove an event handler
     *
     * @param          $eventName
     * @param callable $handler
     */
    public function remove($eventName, callable $handler)
    {
        if (isset($this->handlers[ $eventName ])) {
            $index = array_search($handler, $this->handlers[ $eventName ]);
            if ($index !== false) {
                unset($this->handlers[ $eventName ][ $index ]);
            }
        }
    }

    /**
     * Raise an event with an additional optional parameter
     *
     * @param          $eventName
     * @param mixed    $parameter
     */
    protected function raise($eventName, $parameter = null)
    {
        $this->guardEvent($eventName);
        foreach ($this->handlers[ $eventName ] as $handler) {
            call_user_func($handler, $parameter);
        }
    }

    /**
     * @param $eventName
     */
    private function guardEvent($eventName)
    {
        if (!isset($this->handlers[ $eventName ])) {
            throw new \InvalidArgumentException("Event '{$eventName}' is not defined");
        }
    }
}
{'fraction_non_alphanumeric': 0.13223363980530017, 'fraction_numerical': 0.0, 'mean_word_length': 1.9521149241819633, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '1746821', 'n_tokens_mistral': 1040, 'n_tokens_neox': 1030, 'n_words': 242}