<commit_before><?php

namespace CodeClimate\PHPMD;

use PHPMD\PHPMD;
use PHPMD\RuleSetFactory;
use PHPMD\Writer\StreamWriter;
use PHPMD\Renderer\JSONRenderer;

class Runner
{
    const RULESETS = 'cleancode,codesize,controversial,design,naming,unusedcode';

    private $config;
    private $server;

    public function __construct($config, $server)
    {
        $this->config = $config;
        $this->server = $server;
    }

    public function queueDirectory($dir, $prefix = '')
    {
        if (isset($this->config['include_paths'])) {
            $this->queueWithIncludePaths();
        } else {
            $this->queuePaths($dir, $prefix, $this->config['exclude_paths']);
        }

        $this->server->process_work(false);
    }

    public function queueWithIncludePaths()
    {
        foreach ($this->config['include_paths'] as $f) {
            if ($f !== '.' and $f !== '..') {
                if (is_dir("/code$f")) {
                    $this->queuePaths("/code$f", "$f/");
                    continue;
                }
                $this->server->addwork(array("/code/$f"));
            }
        }
    }

    public function queuePaths($dir, $prefix = '', $exclusions = [])
    {
        $dir = rtrim($dir, '\\/');

        foreach (scandir($dir) as $f) {
            if (in_array("$prefix$f", $exclusions)) {
                continue;
            }

            if ($f !== '.' and $f !== '..') {
                if (is_dir("$dir/$f")) {
                    $this->queuePaths("$dir/$f", "$prefix$f/", $exclusions);
                    continue;
                }

                $prefix = ltrim($prefix, "\\/");
                $this->server->addwork(array("/code/$prefix$f"));
            }
        }
    }

    public function prefixCodeDirectory($configRulesets)
    {
        $officialPhpRulesets = explode(',', Runner::RULESETS);
        $configRulesets = explode(',', $configRulesets);

        foreach ($configRulesets as &$r) {
            if (!in_array($r, $officialPhpRulesets)) {
                $r  = "/code/$r";
            }
        }

        return implode(',', $configRulesets);
    }

    public function run($files)
    {
        $resultFile = tempnam(sys_get_temp_dir(), 'phpmd');

        $renderer = new JSONRenderer();
        $renderer->setWriter(new StreamWriter($resultFile));

        $ruleSetFactory = new RuleSetFactory();
<commit_msg>Don't prefix /code/ for absolute paths<commit_after><?php

namespace CodeClimate\PHPMD;

use PHPMD\PHPMD;
use PHPMD\RuleSetFactory;
use PHPMD\Writer\StreamWriter;
use PHPMD\Renderer\JSONRenderer;

class Runner
{
    const RULESETS = 'cleancode,codesize,controversial,design,naming,unusedcode';

    private $config;
    private $server;

    public function __construct($config, $server)
    {
        $this->config = $config;
        $this->server = $server;
    }

    public function queueDirectory($dir, $prefix = '')
    {
        if (isset($this->config['include_paths'])) {
            $this->queueWithIncludePaths();
        } else {
            $this->queuePaths($dir, $prefix, $this->config['exclude_paths']);
        }

        $this->server->process_work(false);
    }

    public function queueWithIncludePaths()
    {
        foreach ($this->config['include_paths'] as $f) {
            if ($f !== '.' and $f !== '..') {
                if (is_dir("/code$f")) {
                    $this->queuePaths("/code$f", "$f/");
                    continue;
                }
                $this->server->addwork(array("/code/$f"));
            }
        }
    }

    public function queuePaths($dir, $prefix = '', $exclusions = [])
    {
        $dir = rtrim($dir, '\\/');

        foreach (scandir($dir) as $f) {
            if (in_array("$prefix$f", $exclusions)) {
                continue;
            }

            if ($f !== '.' and $f !== '..') {
                if (is_dir("$dir/$f")) {
                    $this->queuePaths("$dir/$f", "$prefix$f/", $exclusions);
                    continue;
                }

                $prefix = ltrim($prefix, "\\/");
                $this->server->addwork(array("/code/$prefix$f"));
            }
        }
    }

    public function prefixCodeDirectory($configRulesets)
    {
        $officialPhpRulesets = explode(',', Runner::RULESETS);
        $configRulesets = explode(',', $configRulesets);

        foreach ($configRulesets as &$r) {
            if (!in_array($r, $officialPhpRulesets) and $r[0] != "/") {
                $r  = "/code/$r";
            }
        }

        return implode(',', $configRulesets);
    }

    public function run($files)
    {
        $resultFile = tempnam(sys_get_temp_dir(), 'phpmd');

        $renderer = new JSONRenderer();
        $renderer->setWriter(new StreamWriter($resultFile));

        $ruleSetFactory = new RuleSetFactory();

{'fraction_non_alphanumeric': 0.16790890269151137, 'fraction_numerical': 0.00020703933747412008, 'mean_word_length': 1.884179104477612, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '850456', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1417, 'n_words': 247}