<commit_before><?php
namespace Hostnet\Component\CodeSniffer;

use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\InstallerEvent;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;

/**
 * This small composer installer plugin hooks into the post-autoload-dump event and swaps out the phpcs and phpcbf
 * files for their hostnet counter parts.
 *
 * @author <NAME> <<EMAIL>>
 */
class Installer implements PluginInterface, EventSubscriberInterface
{
    /**
     * The configured directory of the vendor/bin.
     *
     * @var string
     */
    private $bin_dir;

    /**
     * @var IOInterface
     **/
    private $io;

    /**
     * Activate this plugin by storing the bin_dir.
     *
     * @see \Composer\Plugin\PluginInterface::activate()
     * @param Composer    $composer the composer instance to pull the config of the bin-dir from.
     * @param IOInterface $io the io interface to write / read output input.
     */
    public function activate(Composer $composer, IOInterface $io)
    {
        $this->bin_dir = $composer->getConfig()->get('bin-dir');
        $this->io      = $io;
    }

    /**
     * Ugly Composer static call to hook in to the post-autoload-dump event.
     *
     * @return array holding the post-autoload-dump hook and function to execute.
     */
    public static function getSubscribedEvents()
    {
        return [ 'post-autoload-dump' => 'execute' ];
    }

    /**
     * This function call is called from the phpcs main composer.json file as an 'pre-dependencies-solving' script.
     * This function makes sure the Download manager ALWAYS uses source instead of dist to ensure the 'test'-folder of
     * PHP Code Sniffer is there...
     *
     * @param InstallerEvent $event the event fired.
     */
    public static function ensureSource(InstallerEvent $event)
    {
        $download_manager = $event->getComposer()->getDownloadManager();
        /*@var $dm \Composer\Downloader\DownloadManager */
        $download_manager->setPreferDist(false);
        $download_manager->setPreferSource(true);
    }

    /**
     * Allow configuration from script, to use and test phpcs standalone
     * @param Event $event the event fired.
     */
    public static function configure(Event $event)
    {
        self::defaultConfig($event->getComposer()->getConfig()->get('bin-dir'), $event->getIo());
    }

    /**
     * Use executable of phpcs to configure prefereces
     * @param string $setting name of the setting
     * @param string $value value of the setting
     * @param string $bin_dir location of the phpcs executable
     * @param IOInterface $io composer io interface for verbose output
     */
    public static function phpcsConfig($setting, $value, $bin_dir, IOInterface $io)
    {

        $phpcs   = escapeshellarg($bin_dir . '/phpcs');
        $setting = escapeshellarg($setting);
        $value   = escapeshellarg($value);
        if ($io->isVerbose()) {
            $io->write(`2>&1 $phpcs --config-set $setting $value`);
        }
    }

    /**
     * Set the default config for phpcs to our desired settings
     * @param string $bin_dir
     * @param IOInterface $io
     */
    public static function defaultConfig($bin_dir, IOInterface $io)
    {
        self::phpcsConfig('default_standard', 'Hostnet', $bin_dir, $io);
        self::phpcsConfig('show_process', 1, $bin_dir, $io);<commit_msg>do not only in verbose mode :P<commit_after><?php
namespace Hostnet\Component\CodeSniffer;

use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\InstallerEvent;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;

/**
 * This small composer installer plugin hooks into the post-autoload-dump event and swaps out the phpcs and phpcbf
 * files for their hostnet counter parts.
 *
 * @author <NAME> <<EMAIL>>
 */
class Installer implements PluginInterface, EventSubscriberInterface
{
    /**
     * The configured directory of the vendor/bin.
     *
     * @var string
     */
    private $bin_dir;

    /**
     * @var IOInterface
     **/
    private $io;

    /**
     * Activate this plugin by storing the bin_dir.
     *
     * @see \Composer\Plugin\PluginInterface::activate()
     * @param Composer    $composer the composer instance to pull the config of the bin-dir from.
     * @param IOInterface $io the io interface to write / read output input.
     */
    public function activate(Composer $composer, IOInterface $io)
    {
        $this->bin_dir = $composer->getConfig()->get('bin-dir');
        $this->io      = $io;
    }

    /**
     * Ugly Composer static call to hook in to the post-autoload-dump event.
     *
     * @return array holding the post-autoload-dump hook and function to execute.
     */
    public static function getSubscribedEvents()
    {
        return [ 'post-autoload-dump' => 'execute' ];
    }

    /**
     * This function call is called from the phpcs main composer.json file as an 'pre-dependencies-solving' script.
     * This function makes sure the Download manager ALWAYS uses source instead of dist to ensure the 'test'-folder of
     * PHP Code Sniffer is there...
     *
     * @param InstallerEvent $event the event fired.
     */
    public static function ensureSource(InstallerEvent $event)
    {
        $download_manager = $event->getComposer()->getDownloadManager();
        /*@var $dm \Composer\Downloader\DownloadManager */
        $download_manager->setPreferDist(false);
        $download_manager->setPreferSource(true);
    }

    /**
     * Allow configuration from script, to use and test phpcs standalone
     * @param Event $event the event fired.
     */
    public static function configure(Event $event)
    {
        self::defaultConfig($event->getComposer()->getConfig()->get('bin-dir'), $event->getIo());
    }

    /**
     * Use executable of phpcs to configure prefereces
     * @param string $setting name of the setting
     * @param string $value value of the setting
     * @param string $bin_dir location of the phpcs executable
     * @param IOInterface $io composer io interface for verbose output
     */
    public static function phpcsConfig($setting, $value, $bin_dir, IOInterface $io)
    {

        $phpcs   = escapeshellarg($bin_dir . '/phpcs');
        $setting = escapeshellarg($setting);
        $value   = escapeshellarg($value);
        $output  = `2>&1 $phpcs --config-set $setting $value`;

        if ($io->isVerbose()) {
            $io->write($output);;
        }
    }

    /**
     * Set the default config for phpcs to our desired settings
     * @param string $bin_dir
     * @param IOInterface $io
     */
    public static function defaultConfig($bin_dir, IOInterface $io)
    {
        self::phpcsConfig('default_standard', 'Hostnet', $bin_dir, $io);
        self::phpcsConfig('show_process', 1, $bin_dir, $io);
{'fraction_non_alphanumeric': 0.11055276381909548, 'fraction_numerical': 0.0008614501076812635, 'mean_word_length': 2.8065573770491805, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3099647', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1977, 'n_words': 609}