<commit_before><?php

/*
 * This file is part of Gush package.
 *
 * (c) 2013-2015 <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Gush;

use Gush\Adapter\Adapter;
use Gush\Adapter\IssueTracker;
use Gush\Command as Cmd;
use Gush\Exception\UserException;
use Gush\Factory\AdapterFactory;
use Gush\Helper as Helpers;
use Gush\Helper\OutputAwareInterface;
use Gush\Subscriber\CommandEndSubscriber;
use Gush\Subscriber\CoreInitSubscriber;
use Gush\Subscriber\GitRepoSubscriber;
use Gush\Subscriber\TableSubscriber;
use Gush\Subscriber\TemplateSubscriber;
use KevinGH\Amend\Command as UpdateCommand;
use KevinGH\Amend\Helper as UpdateHelper;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Input\InputAwareInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;

class Application extends BaseApplication
{
    const MANIFESTO_FILE_URL = 'http://gushphp.org/manifest.json';

    const GUSH_LOGO = <<<LOGO
   _____ _    _  _____ _    _
  / ____| |  | |/ ____| |  | |
 | |  __| |  | | (___ | |__| |
 | | |_ | |  | |\___ \|  __  |
 | |__| | |__| |____) | |  | |
  \_____|\____/|_____/|_|  |_|

LOGO;

    /**
     * @var Config $config The configuration file
     */
    protected $config;

    /**
     * @var null|Adapter
     */
    protected $adapter;

    /**
     * @var null|IssueTracker
     */
    protected $issueTracker;

    /**
     * @var EventDispatcher
     */
    protected $dispatcher;

    /**
     * @var AdapterFactory
     */
    protected $adapterFactory;

    /**
     * Constructor.
     *
     * @param AdapterFactory $adapterFactory
     * @param Config         $config
     * @param string         $version
     */
    public function __construct(AdapterFactory $adapterFactory, Config $config, $version = '@package_version@')
    {
        if ('@'.'package_version@' !== $version) {
            $version = ltrim($version, 'v');
        }

        parent::__construct('Gush', $version);

        $this->adapterFactory = $adapterFactory;
        $this->config = $config;

        // The parent dispatcher is private and has
        // no accessor, so we set it here to make it accessible.
        $this->dispatcher = new EventDispatcher();

        $this->registerSubscribers();
        $this->addCommands($this->getCommands());
    }

    protected function registerSubscribers()
    {
        $helperSet = $this->getHelperSet();

        $this->dispatcher->addSubscriber(new TableSubscriber());
        $this->dispatcher->addSubscriber(
            new GitRepoSubscriber(
                $this,
                $helperSet->get('git'),
                $helperSet->get('git_config'),
                $helperSet->get('gush_style')
            )
        );

        $this->dispatcher->addSubscriber(
            new CoreInitSubscriber(
                $this,
                $helperSet->get('git'),
                $helperSet->get('git_config'),
                $helperSet->get('gush_style')
            )
        );

        $this->dispatcher->addSubscriber(new TemplateSubscriber($helperSet->get('template')));
        $this->dispatcher->addSubscriber(
            new CommandEndSubscriber($helperSet->get('filesystem'), $helperSet->get('git'))
        );
    }

    /**
     * {@inheritdoc}
     */
    protected function getDefaultHelperSet()
    {
        $helperSet = parent::getDefaultHelperSet();
        $helperSet->set(new Helpers\FilesystemHelper());
        $helperSet->set(new Helpers\TextHelper());
        $helperSet->set(new Helpers\GushQuestionHelper());
        $helperSet->set(new Helpers\StyleHelper($helperSet->get('gush_question')));
        $helperSet->set(new Helpers\TableHelper());
        $helperSet->set(new Helpers\ProcessHelper());
        $helperSet->set(new Helpers\EditorHelper());
        $helperSet->set(new Helpers\GitConfigHelper($helperSet->get('process'), $this));
        $helperSet->set(
            new Helpers\GitHelper(
                $helperSet->get('process'),
                $helperSet->get('git_config'),
                $helperSet->get('filesystem')
            )
        );
        $helperSet->set(new Helpers\TemplateHelper($helperSet->get('gush_style'), $this));
        $helperSet->set(new Helpers\MetaHelper($this->getSupportedMetaFiles()));
        $helperSet->set(new Helpers\AutocompleteHelper());
        $helperSet->set(new UpdateHelper());

        return $helperSet;
    }

    /**
     * @return string
     */
    public function getHelp()
    {
        return self::GUSH_LOGO.PHP_EOL.parent::getHelp();
    }

    /**
     * Get the Repository adapter.
     *
     * @return Adapter
     *
     * @throws \RuntimeException
     */
    public function getAdapter()
    {
        if (null === $this->adapter) {
            throw new \RuntimeException(
                'No repo-adapter set, make sure the current command implements "Gush\Feature\GitRepoFeature".'
            );
        }

        return $this->adapter;
    }

    /**
     * Get the IssueTracker adapter.
     *
     * @return IssueTracker
     *
     * @throws \RuntimeException
     */
    public function getIssueTracker()
    {
        if (null === $this->issueTracker) {
            throw new \RuntimeException(
                'No issue-adapter set, make sure the current command implements "Gush\Feature\IssueTrackerRepoFeature".'
            );
        }

        return $this->issueTracker;
    }

    /**
     * Set the Repository Adapter.
     *
     * @param Adapter $adapter
     */
    public function setAdapter(Adapter $adapter)
    {
        $this->adapter = $adapter;
    }

    /**
     * Set the IssueTracker Adapter.
     *
     * @param IssueTracker $issueTracker
     */
    public function setIssueTracker(IssueTracker $issueTracker)
    {
        $this->issueTracker = $issueTracker;
    }

    /**
     * Get the application configuration.
     *
     * @return Config
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * @return EventDispatcher
     */
    public function getDispatcher()
    {
        return $this->dispatcher;
    }

    /**
     * @return AdapterFactory
     */
    public function getAdapterFactory()
    {
        return $this->adapterFactory;
    }

    /**
     * {@inheritdoc}
     */
    protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
    {
        $helperSet = $command->getHelperSet();

        foreach ($helperSet as $helper) {
            if ($helper instanceof OutputAwareInterface) {
                $helper->setOutput($output);
            }

            if ($helper instanceof InputAwareInterface) {
                $helper->setInput($input);
            }
        }

        $event = new ConsoleCommandEvent($command, $input, $output);
        $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);

        $exitCode = 0;

        try {
            $exitCode = $command->run($input, $output);
        } catch (\Exception $e) {
            $exitCode = $e->getCode();

            $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
            $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);

            if ($e instanceof UserException) {
                $this->getHelperSet()->get('gush_style')->error($e->getMessages());

                if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
                    throw $e;
                }
            } else {
                throw $event->getException();
            }
        } finally {
            $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
            $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
        }

        return $exitCode;
    }

    /**
     * @return \Symfony\Component\Console\Command\Command[]
     */
    public function getCommands()
    {
        $updateCommand = new UpdateCommand('core:update');
        $updateCommand->setManifestUri(self::MANIFESTO_FILE_URL);

        return [<commit_msg>fix Application missing exit-code in doRunCommand<commit_after><?php

/*
 * This file is part of Gush package.
 *
 * (c) 2013-2015 <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Gush;

use Gush\Adapter\Adapter;
use Gush\Adapter\IssueTracker;
use Gush\Command as Cmd;
use Gush\Exception\UserException;
use Gush\Factory\AdapterFactory;
use Gush\Helper as Helpers;
use Gush\Helper\OutputAwareInterface;
use Gush\Subscriber\CommandEndSubscriber;
use Gush\Subscriber\CoreInitSubscriber;
use Gush\Subscriber\GitRepoSubscriber;
use Gush\Subscriber\TableSubscriber;
use Gush\Subscriber\TemplateSubscriber;
use KevinGH\Amend\Command as UpdateCommand;
use KevinGH\Amend\Helper as UpdateHelper;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Input\InputAwareInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;

class Application extends BaseApplication
{
    const MANIFESTO_FILE_URL = 'http://gushphp.org/manifest.json';

    const GUSH_LOGO = <<<LOGO
   _____ _    _  _____ _    _
  / ____| |  | |/ ____| |  | |
 | |  __| |  | | (___ | |__| |
 | | |_ | |  | |\___ \|  __  |
 | |__| | |__| |____) | |  | |
  \_____|\____/|_____/|_|  |_|

LOGO;

    /**
     * @var Config $config The configuration file
     */
    protected $config;

    /**
     * @var null|Adapter
     */
    protected $adapter;

    /**
     * @var null|IssueTracker
     */
    protected $issueTracker;

    /**
     * @var EventDispatcher
     */
    protected $dispatcher;

    /**
     * @var AdapterFactory
     */
    protected $adapterFactory;

    /**
     * Constructor.
     *
     * @param AdapterFactory $adapterFactory
     * @param Config         $config
     * @param string         $version
     */
    public function __construct(AdapterFactory $adapterFactory, Config $config, $version = '@package_version@')
    {
        if ('@'.'package_version@' !== $version) {
            $version = ltrim($version, 'v');
        }

        parent::__construct('Gush', $version);

        $this->adapterFactory = $adapterFactory;
        $this->config = $config;

        // The parent dispatcher is private and has
        // no accessor, so we set it here to make it accessible.
        $this->dispatcher = new EventDispatcher();

        $this->registerSubscribers();
        $this->addCommands($this->getCommands());
    }

    protected function registerSubscribers()
    {
        $helperSet = $this->getHelperSet();

        $this->dispatcher->addSubscriber(new TableSubscriber());
        $this->dispatcher->addSubscriber(
            new GitRepoSubscriber(
                $this,
                $helperSet->get('git'),
                $helperSet->get('git_config'),
                $helperSet->get('gush_style')
            )
        );

        $this->dispatcher->addSubscriber(
            new CoreInitSubscriber(
                $this,
                $helperSet->get('git'),
                $helperSet->get('git_config'),
                $helperSet->get('gush_style')
            )
        );

        $this->dispatcher->addSubscriber(new TemplateSubscriber($helperSet->get('template')));
        $this->dispatcher->addSubscriber(
            new CommandEndSubscriber($helperSet->get('filesystem'), $helperSet->get('git'))
        );
    }

    /**
     * {@inheritdoc}
     */
    protected function getDefaultHelperSet()
    {
        $helperSet = parent::getDefaultHelperSet();
        $helperSet->set(new Helpers\FilesystemHelper());
        $helperSet->set(new Helpers\TextHelper());
        $helperSet->set(new Helpers\GushQuestionHelper());
        $helperSet->set(new Helpers\StyleHelper($helperSet->get('gush_question')));
        $helperSet->set(new Helpers\TableHelper());
        $helperSet->set(new Helpers\ProcessHelper());
        $helperSet->set(new Helpers\EditorHelper());
        $helperSet->set(new Helpers\GitConfigHelper($helperSet->get('process'), $this));
        $helperSet->set(
            new Helpers\GitHelper(
                $helperSet->get('process'),
                $helperSet->get('git_config'),
                $helperSet->get('filesystem')
            )
        );
        $helperSet->set(new Helpers\TemplateHelper($helperSet->get('gush_style'), $this));
        $helperSet->set(new Helpers\MetaHelper($this->getSupportedMetaFiles()));
        $helperSet->set(new Helpers\AutocompleteHelper());
        $helperSet->set(new UpdateHelper());

        return $helperSet;
    }

    /**
     * @return string
     */
    public function getHelp()
    {
        return self::GUSH_LOGO.PHP_EOL.parent::getHelp();
    }

    /**
     * Get the Repository adapter.
     *
     * @return Adapter
     *
     * @throws \RuntimeException
     */
    public function getAdapter()
    {
        if (null === $this->adapter) {
            throw new \RuntimeException(
                'No repo-adapter set, make sure the current command implements "Gush\Feature\GitRepoFeature".'
            );
        }

        return $this->adapter;
    }

    /**
     * Get the IssueTracker adapter.
     *
     * @return IssueTracker
     *
     * @throws \RuntimeException
     */
    public function getIssueTracker()
    {
        if (null === $this->issueTracker) {
            throw new \RuntimeException(
                'No issue-adapter set, make sure the current command implements "Gush\Feature\IssueTrackerRepoFeature".'
            );
        }

        return $this->issueTracker;
    }

    /**
     * Set the Repository Adapter.
     *
     * @param Adapter $adapter
     */
    public function setAdapter(Adapter $adapter)
    {
        $this->adapter = $adapter;
    }

    /**
     * Set the IssueTracker Adapter.
     *
     * @param IssueTracker $issueTracker
     */
    public function setIssueTracker(IssueTracker $issueTracker)
    {
        $this->issueTracker = $issueTracker;
    }

    /**
     * Get the application configuration.
     *
     * @return Config
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * @return EventDispatcher
     */
    public function getDispatcher()
    {
        return $this->dispatcher;
    }

    /**
     * @return AdapterFactory
     */
    public function getAdapterFactory()
    {
        return $this->adapterFactory;
    }

    /**
     * {@inheritdoc}
     */
    protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
    {
        $helperSet = $command->getHelperSet();

        foreach ($helperSet as $helper) {
            if ($helper instanceof OutputAwareInterface) {
                $helper->setOutput($output);
            }

            if ($helper instanceof InputAwareInterface) {
                $helper->setInput($input);
            }
        }

        $event = new ConsoleCommandEvent($command, $input, $output);
        $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);

        try {
            $exitCode = $command->run($input, $output);
        } catch (\Exception $e) {
            $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
            $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);

            $event = new ConsoleExceptionEvent($command, $input, $output, $e, $event->getExitCode());
            $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);

            if ($e instanceof UserException) {
                $this->getHelperSet()->get('gush_style')->error($e->getMessages());

                if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
                    throw $e;
                }

                return $event->getExitCode();
            } else {
                throw $event->getException();
            }
        }

        $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
        $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);

        return $event->getExitCode();
    }

    /**
     * @return \Symfony\Component\Console\Command\Command[]
     */
    public function getCommands()
    {
        $updateCommand = new UpdateCommand('core:update');
        $updateCommand->setManifestUri(self::MANIFESTO_FILE_URL);

        return [
{'fraction_non_alphanumeric': 0.13095514757146176, 'fraction_numerical': 0.0009876830118521961, 'mean_word_length': 2.2962466487935655, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 161, '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': '7419111', 'n_tokens_mistral': 4973, 'n_tokens_neox': 4808, 'n_words': 908}