<commit_before>const CompilerPassInterface = Jymfony.Component.DependencyInjection.Compiler.CompilerPassInterface;
const Reference = Jymfony.Component.DependencyInjection.Reference;
const EventSubscriberInterface = Jymfony.Component.EventDispatcher.EventSubscriberInterface;
const EventDispatcher = Jymfony.Component.EventDispatcher.EventDispatcher;
const ServiceClosureArgument = Jymfony.Component.DependencyInjection.Argument.ServiceClosureArgument;
const NotStaticMethodException = Jymfony.Component.EventDispatcher.Exception.NotStaticMethodException;

/**
 * @memberOf Jymfony.Component.EventDispatcher.DependencyInjection.Compiler
 */
class RegisterListenerPass extends implementationOf(CompilerPassInterface) {
    __construct(dispatcherService = 'event_dispatcher', listenerTag = 'kernel.event_listener', subscriberTag = 'kernel.event_subscriber') {
        this.dispatcherService = dispatcherService;
        this.listenerTag = listenerTag;
        this.subscriberTag = subscriberTag;
    }

    /**
     * @param {Jymfony.Component.DependencyInjection.ContainerBuilder} container
     */
    process(container) {
        if (! container.hasDefinition(this.dispatcherService) && !container.hasAlias(this.dispatcherService)) {
            return;
        }

        let definition = container.findDefinition(this.dispatcherService);

        for (let [ id, events ] of __jymfony.getEntries(container.findTaggedServiceIds(this.listenerTag))) {
            for (let event of events) {
                let priority = event.priority !== undefined ? event.priority : 0;

                if (event.event === undefined) {
                    throw new InvalidArgumentException(__jymfony.sprintf('Service "%s" must define the "event" attribute on "%s" tags.', id, this.listenerTag));
                }

                if (event.method === undefined) {
                    throw new InvalidArgumentException(__jymfony.sprintf('Service "%s" must define the "method" attribute on "%s" tags.', id, this.listenerTag));
                }

                definition.addMethodCall('addListenerService', [
                    event.event,
                    [
                        id,
                        event.method,
                        priority,
                    ],
                ]);
            }
        }

        let extractingDispatcher = new ExtractingEventDispatcher();

        for (let [ id, attributes ] of __jymfony.getEntries(container.findTaggedServiceIds(this.subscriberTag))) {
            let def = container.getDefinition(id);

            let myclass = container.parameterBag.resolveValue(def.getClass());
            let myReflectionClass = new ReflectionClass(myclass);

            if (! myReflectionClass.isSubclassOf(EventSubscriberInterface)) {
                throw new InvalidArgumentException(__jymfony.sprintf('Service "%s" must implement interface "Jymfony.Component.EventDispatcher.EventSubscriberInterface".', id));
            }

            container.addObjectResource(myclass);
            extractingDispatcher.subscriber = myclass;

            try {
                for (let args of extractingDispatcher.listeners) {
                    args[1] = [ new ServiceClosureArgument(new Reference(id)), args[1] ];
                    definition.addMethodCall('addListener', args);
                }
            } catch (e) {
                if (e instanceof NotStaticMethodException) {
                    definition.addMethodCall('addSubscriber', [ new Reference(id) ]);
                }

                throw e;
            }
        }
    }
}

class ExtractingEventDispatcher extends mix(EventDispatcher, EventSubscriberInterface) {
    constructor() {
        super();

        this.__construct();
        this._listeners = [];
    }

    set subscriber(subscriber) {
        this._listeners = [];

        this._subscriber = subscriber;
        this.addSubscriber(this);
    }

    get listeners() {
        return this._listeners;
    }

    addListener(eventName, listener, priority = 0) {
        this._listeners.push([ eventName, listener[1], priority ]);
    }

    getSubscribedEvents() {
        let reflClass = new ReflectionClass(this._subscriber);
        let class_ = reflClass.getConstructor();

        if (! isFunction(class_.getSubscribedEvents)) {
            throw new NotStaticMethodException();
        }

        return class_.getSubscribedEvents();
    }
}

module.exports = RegisterListenerPass;
<commit_msg>[EventDispatcher] fix register listener pass (reflection exception catch and missing else)<commit_after>const CompilerPassInterface = Jymfony.Component.DependencyInjection.Compiler.CompilerPassInterface;
const Reference = Jymfony.Component.DependencyInjection.Reference;
const EventSubscriberInterface = Jymfony.Component.EventDispatcher.EventSubscriberInterface;
const EventDispatcher = Jymfony.Component.EventDispatcher.EventDispatcher;
const ServiceClosureArgument = Jymfony.Component.DependencyInjection.Argument.ServiceClosureArgument;
const NotStaticMethodException = Jymfony.Component.EventDispatcher.Exception.NotStaticMethodException;

/**
 * @memberOf Jymfony.Component.EventDispatcher.DependencyInjection.Compiler
 */
class RegisterListenerPass extends implementationOf(CompilerPassInterface) {
    __construct(dispatcherService = 'event_dispatcher', listenerTag = 'kernel.event_listener', subscriberTag = 'kernel.event_subscriber') {
        this.dispatcherService = dispatcherService;
        this.listenerTag = listenerTag;
        this.subscriberTag = subscriberTag;
    }

    /**
     * @param {Jymfony.Component.DependencyInjection.ContainerBuilder} container
     */
    process(container) {
        if (! container.hasDefinition(this.dispatcherService) && !container.hasAlias(this.dispatcherService)) {
            return;
        }

        let definition = container.findDefinition(this.dispatcherService);

        for (let [ id, events ] of __jymfony.getEntries(container.findTaggedServiceIds(this.listenerTag))) {
            for (let event of events) {
                let priority = event.priority !== undefined ? event.priority : 0;

                if (event.event === undefined) {
                    throw new InvalidArgumentException(__jymfony.sprintf('Service "%s" must define the "event" attribute on "%s" tags.', id, this.listenerTag));
                }

                if (event.method === undefined) {
                    throw new InvalidArgumentException(__jymfony.sprintf('Service "%s" must define the "method" attribute on "%s" tags.', id, this.listenerTag));
                }

                definition.addMethodCall('addListenerService', [
                    event.event,
                    [
                        id,
                        event.method,
                        priority,
                    ],
                ]);
            }
        }

        let extractingDispatcher = new ExtractingEventDispatcher();

        for (let [ id, attributes ] of __jymfony.getEntries(container.findTaggedServiceIds(this.subscriberTag))) {
            let def = container.getDefinition(id);

            let myReflectionClass, myclass = container.parameterBag.resolveValue(def.getClass());
            try {
                myReflectionClass = new ReflectionClass(myclass);
            } catch (err) {
                if (err instanceof ReflectionException) {
                    throw new InvalidArgumentException(__jymfony.sprintf('Service "%s" requires a non-existent class "%s".', id, myclass));
                }

                throw err;
            }

            if (! myReflectionClass.isSubclassOf(EventSubscriberInterface)) {
                throw new InvalidArgumentException(__jymfony.sprintf('Service "%s" must implement interface "Jymfony.Component.EventDispatcher.EventSubscriberInterface".', id));
            }

            container.addObjectResource(myclass);
            extractingDispatcher.subscriber = myclass;

            try {
                for (let args of extractingDispatcher.listeners) {
                    args[1] = [ new ServiceClosureArgument(new Reference(id)), args[1] ];
                    definition.addMethodCall('addListener', args);
                }
            } catch (e) {
                if (e instanceof NotStaticMethodException) {
                    definition.addMethodCall('addSubscriber', [ new Reference(id) ]);
                } else {
                    throw e;
                }
            }
        }
    }
}

class ExtractingEventDispatcher extends mix(EventDispatcher, EventSubscriberInterface) {
    constructor() {
        super();

        this.__construct();
        this._listeners = [];
    }

    set subscriber(subscriber) {
        this._listeners = [];

        this._subscriber = subscriber;
        this.addSubscriber(this);
    }

    get listeners() {
        return this._listeners;
    }

    addListener(eventName, listener, priority = 0) {
        this._listeners.push([ eventName, listener[1], priority ]);
    }

    getSubscribedEvents() {
        let reflClass = new ReflectionClass(this._subscriber);
        let class_ = reflClass.getConstructor();

        if (! isFunction(class_.getSubscribedEvents)) {
            throw new NotStaticMethodException();
        }

        return class_.getSubscribedEvents();
    }
}

module.exports = RegisterListenerPass;

{'fraction_non_alphanumeric': 0.09462915601023018, 'fraction_numerical': 0.0010656436487638534, 'mean_word_length': 2.411486732097419, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6350224', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2211, 'n_words': 441}