<commit_before><?php

namespace Trunkat\LibphutilComposerConvertor;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

class Convert extends Command
{
    const NEW_NAMESPACE = 'Facebook\Libphutil';
    const NEW_NAMESPACE_ESCAPED = '\\Facebook\\Libphutil\\';

    /** All the possible prefixes of classes and functions */
    private $prefixes = array(
        'class'    => array('(', '{', '[', ' ', ';', '.', '!', "'", '"', ')'),
        'function' => array('(', '{', '[', ' ', ';', '.', '!', "'", '"', ')'),
    );
    /** All the possible suffixes of classes and functions */
    private $suffixes = array(
        'class'    => array(' ', "\n", ';', '(', ')', '}', ']', ',', '::', "'", '"'),
        'function' => array('(', ' (', "'", '"'),
    );

    /**
     * Command configuration..
     */
    protected function configure()
    {
        $this
            ->setName('convert')
            ->setDescription('Make libphutil composer compatible.')
            ->addArgument(
                'source',
                InputArgument::REQUIRED,
                'Source directory containing libphutil library.'
            )
            ->addArgument(
                'target',
                InputArgument::REQUIRED,
                'Target directory for final composer version of libphutil library.'
            )
        ;
    }

    /**
     * Command execution.
     * 
     * @param InputInterface  $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $replace = $this->getReplacementForPhpInternals();
        $source  = $input->getArgument('source') . '/src';
        $target  = $input->getArgument('target') . '/src';

        $fs = new Filesystem();
        $fn = new Finder();

        // Clean old and recreate target directory.
        $fs->remove($target);
        $target .= '/' . str_replace('\\', '/', self::NEW_NAMESPACE);
        $fs->mkdir($target);
        $output->writeln('Directory cleaned.');

        // Explore files and copy them to new location.
        $classFiles    = array();
        $functionFiles = array();
        $count         = 0;
        $functionLoc   = '';
        foreach ($fn->files()->exclude('__tests__')->name('*.php')->in($source) as $file) {
            $pathName  = $file->getRelativePathname();
            $fileName  = $file->getFileName();
            $baseName  = basename($fileName, '.php');

            $contents  = $file->getContents();
            $classes   = $this->getPhpClasses($contents);
            $functions = $this->getPhpFunctions($contents);

            if ($classes) {
                $targetPath = "$target/$fileName";<commit_msg>Making final class normal so the can bemocked in unittests<commit_after><?php

namespace Trunkat\LibphutilComposerConvertor;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

class Convert extends Command
{
    const NEW_NAMESPACE = 'Facebook\Libphutil';
    const NEW_NAMESPACE_ESCAPED = '\\Facebook\\Libphutil\\';

    /** All the possible prefixes of classes and functions */
    private $prefixes = array(
        'class'    => array('(', '{', '[', ' ', ';', '.', '!', "'", '"', ')'),
        'function' => array('(', '{', '[', ' ', ';', '.', '!', "'", '"', ')'),
    );
    /** All the possible suffixes of classes and functions */
    private $suffixes = array(
        'class'    => array(' ', "\n", ';', '(', ')', '}', ']', ',', '::', "'", '"'),
        'function' => array('(', ' (', "'", '"'),
    );

    /**
     * Command configuration..
     */
    protected function configure()
    {
        $this
            ->setName('convert')
            ->setDescription('Make libphutil composer compatible.')
            ->addArgument(
                'source',
                InputArgument::REQUIRED,
                'Source directory containing libphutil library.'
            )
            ->addArgument(
                'target',
                InputArgument::REQUIRED,
                'Target directory for final composer version of libphutil library.'
            )
        ;
    }

    /**
     * Command execution.
     * 
     * @param InputInterface  $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $replace = $this->getReplacementForPhpInternals();
        $source  = $input->getArgument('source') . '/src';
        $target  = $input->getArgument('target') . '/src';

        $fs = new Filesystem();
        $fn = new Finder();

        // Replacements.
        $replace['final class'] = 'class';

        // Clean old and recreate target directory.
        $fs->remove($target);
        $target .= '/' . str_replace('\\', '/', self::NEW_NAMESPACE);
        $fs->mkdir($target);
        $output->writeln('Directory cleaned.');

        // Explore files and copy them to new location.
        $classFiles    = array();
        $functionFiles = array();
        $count         = 0;
        $functionLoc   = '';
        foreach ($fn->files()->exclude('__tests__')->name('*.php')->in($source) as $file) {
            $pathName  = $file->getRelativePathname();
            $fileName  = $file->getFileName();
            $baseName  = basename($fileName, '.php');

            $contents  = $file->getContents();
            $classes   = $this->getPhpClasses($contents);
            $functions = $this->getPhpFunctions($contents);

            if ($classes) {
                $targetPath = "$target/$fileName";
{'fraction_non_alphanumeric': 0.17153164972122006, 'fraction_numerical': 0.00032797638570022957, 'mean_word_length': 2.2338282078472957, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3220533', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1680, 'n_words': 344}