<commit_before><?php
declare(strict_types=1);

namespace MattyG\BBStatic\BBCode;

use Aura\Di\Container as DiContainer;
use Nbbc\BBCode;
use Nbbc\Debugger as BBCodeDebugger;
use Symfony\Component\Finder\Finder as SymfonyFinder;

/**
 * @param DiContainer $di
 * @param string $filename
 * @return array
 */
function MattyGBBStaticInitRequire(DiContainer $di, string $filename): array
{
    return require($filename);
}

final class Init
{
    /**
     * @var DiContainer
     */
    private $di;

    /**
     * @var string[]
     */
    private $rulesDirs = array();

    /**
     * @var string[]
     */
    private $templateOverridesDirs = array();

    /**
     * @var SymfonyFinder
     */
    private $finderProto;

    /**
     * @param string $rulesDir
     * @param string $handlersDir
     */
    public function __construct(DiContainer $di, string $rulesDir = __DIR__ . "/rules", string $templateOverridesDir = __DIR__ . "/template_overrides")
    {
        $this->di = $di;
        $this->addRulesDir($rulesDir);
        $this->addTemplateOverridesDir($templateOverridesDir);

        $this->finderProto = new SymfonyFinder();
        $this->finderProto->files()
            ->name("*.php")
            ->ignoreVCS(true)
            ->ignoreDotFiles(true)
            ->followLinks();
    }

    /**
     * @param string $rulesDir
     */
    public function addRulesDir(string $rulesDir)
    {
        $this->rulesDirs[] = $rulesDir;
    }

    /**
     * @param string $templateOverridesDir
     */
    public function addTemplateOverridesDir(string $templateOverridesDir)
    {
        $this->templateOverridesDirs[] = $templateOverridesDir;
    }

    /**
     * @return BBCode
     */
    public function init() : BBCode
    {
        $bbcode = new BBCode;
        $this->initRules($bbcode);
        $this->initTemplateOverrides($bbcode);

        if (getenv("BBCODE_DEBUG") == 1) {
            BBCodeDebugger::$level = 0;
        }

        return $bbcode;
    }

    /**
     * @param BBCode $bbcode
     */
    protected function initRules(BBCode $bbcode)
    {
        foreach ($this->rulesDirs as $rulesDir) {
            $globber = clone $this->finderProto;
            $globber->in($rulesDir);
            foreach ($globber as $ruleFile) {
                $ruleFilename = $ruleFile->getRelativePathname();
                $ruleName = pathinfo($ruleFilename, PATHINFO_FILENAME);
                $rule = MattyGBBStaticInitRequire($this->di, $rulesDir . "/" . $ruleFilename);
                $bbcode->addRule($ruleName, $rule);
            }
        }
    }

    /**
     * @param BBCode $bbcode
     */
    protected function initTemplateOverrides(BBCode $bbcode)
    {
        foreach ($this->templateOverridesDirs as $templateOverridesDir) {
            $globber = clone $this->finderProto;
            $globber->in($templateOverridesDir);
            foreach ($globber as $templateOverrideFile) {<commit_msg>Small code cleanup<commit_after><?php
declare(strict_types=1);

namespace MattyG\BBStatic\BBCode;

use Aura\Di\Container as DiContainer;
use Nbbc\BBCode;
use Nbbc\Debugger as BBCodeDebugger;
use Symfony\Component\Finder\Finder as SymfonyFinder;

/**
 * @param DiContainer $di
 * @param string $filename
 * @return array
 */
function MattyGBBStaticInitRequire(DiContainer $di, string $filename): array
{
    return require($filename);
}

final class Init
{
    /**
     * @var DiContainer
     */
    private $di;

    /**
     * @var string[]
     */
    private $rulesDirs = array();

    /**
     * @var string[]
     */
    private $templateOverridesDirs = array();

    /**
     * @var SymfonyFinder
     */
    private $finderProto;

    /**
     * @param string $rulesDir
     * @param string $handlersDir
     */
    public function __construct(DiContainer $di, string $rulesDir = __DIR__ . "/rules", string $templateOverridesDir = __DIR__ . "/template_overrides")
    {
        $this->di = $di;
        $this->addRulesDir($rulesDir);
        $this->addTemplateOverridesDir($templateOverridesDir);

        $this->finderProto = new SymfonyFinder();
        $this->finderProto->files()
            ->name("*.php")
            ->ignoreVCS(true)
            ->ignoreDotFiles(true)
            ->followLinks();
    }

    /**
     * @param string $rulesDir
     */
    public function addRulesDir(string $rulesDir)
    {
        $this->rulesDirs[] = $rulesDir;
    }

    /**
     * @param string $templateOverridesDir
     */
    public function addTemplateOverridesDir(string $templateOverridesDir)
    {
        $this->templateOverridesDirs[] = $templateOverridesDir;
    }

    /**
     * @return BBCode
     */
    public function init() : BBCode
    {
        $bbcode = new BBCode;
        $this->initRules($bbcode);
        $this->initTemplateOverrides($bbcode);

        if (getenv("BBCODE_DEBUG") == 1) {
            BBCodeDebugger::$level = 0;
        }

        return $bbcode;
    }

    /**
     * @param BBCode $bbcode
     */
    private function initRules(BBCode $bbcode)
    {
        foreach ($this->rulesDirs as $rulesDir) {
            $globber = clone $this->finderProto;
            $globber->in($rulesDir);
            foreach ($globber as $ruleFile) {
                $ruleFilename = $ruleFile->getRelativePathname();
                $ruleName = pathinfo($ruleFilename, PATHINFO_FILENAME);
                $rule = MattyGBBStaticInitRequire($this->di, $rulesDir . "/" . $ruleFilename);
                $bbcode->addRule($ruleName, $rule);
            }
        }
    }

    /**
     * @param BBCode $bbcode
     */
    private function initTemplateOverrides(BBCode $bbcode)
    {
        foreach ($this->templateOverridesDirs as $templateOverridesDir) {
            $globber = clone $this->finderProto;
            $globber->in($templateOverridesDir);
            foreach ($globber as $templateOverrideFile) {
{'fraction_non_alphanumeric': 0.13124254303732744, 'fraction_numerical': 0.0010226691665246293, 'mean_word_length': 2.2545757071547423, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1636059', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1774, 'n_words': 342}