<commit_before><?php

/*
 * This file is part of phpunit-smartrunner.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace iakio\phpunit\smartrunner\commands;

use DOMDocument;
use DOMAttr;
use iakio\phpunit\smartrunner\FileSystem;

class InitCommand
{
    public function __construct(FileSystem $fs)
    {
        $this->fs = $fs;
    }

    private function defaultPhpUnitConfig()
    {
        $config = <<<EOD
<?xml version="1.0"?>
<phpunit>
  <listeners>
    <listener class="iakio\phpunit\smartrunner\DependencyListener"></listener>
  </listeners>
</phpunit>
EOD;

        return $config;
    }

    private function phpunitConfig(array $argv)
    {
        if (count($argv) > 0) {
            $doc = new DOMDocument();
            $doc->loadXML(
                file_get_contents($argv[0])
            );
            $listeners_tags = $doc->getElementsByTagName('listeners');
            if ($listeners_tags->length === 0) {
                $listeners = $doc->firstChild->appendChild($doc->createElement('listeners'));
            } else {
                $listeners = $listeners_tags[0];
            }
            $listener = $doc->createElement('listener');
            $listener->setAttributeNode(
                new DomAttr('class', 'iakio\phpunit\smartrunner\DependencyListener'));
            $listeners->appendChild($listener);
            $phpunit_config = $doc->saveXML();
        } else {
            $phpunit_config = $this->defaultPhpUnitConfig();
        }

        return $phpunit_config;
    }

    public function run(array $argv = [])
    {
        if (file_exists($this->fs->config_file)) {
            echo $this->fs->relativePath($this->fs->config_file), " already exists.\n";
        } else {
            $this->fs->saveConfigFile($this->fs->loadConfig());
            echo $this->fs->relativePath($this->fs->config_file), " created.\n";
        }

        if (file_exists($this->fs->phpunit_config_file)) {
            echo $this->fs->relativePath($this->fs->phpunit_config_file), " already exists.\n";
        } else {
            $this->fs->savePhpUnitConfig($this->phpunitConfig($argv));<commit_msg>Fix (for PHP 5.5 or below).<commit_after><?php

/*
 * This file is part of phpunit-smartrunner.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace iakio\phpunit\smartrunner\commands;

use DOMDocument;
use DOMAttr;
use iakio\phpunit\smartrunner\FileSystem;

class InitCommand
{
    public function __construct(FileSystem $fs)
    {
        $this->fs = $fs;
    }

    private function defaultPhpUnitConfig()
    {
        $config = <<<EOD
<?xml version="1.0"?>
<phpunit>
  <listeners>
    <listener class="iakio\phpunit\smartrunner\DependencyListener"></listener>
  </listeners>
</phpunit>
EOD;

        return $config;
    }

    private function phpunitConfig(array $argv)
    {
        if (count($argv) > 0) {
            $doc = new DOMDocument();
            $doc->loadXML(
                file_get_contents($argv[0])
            );
            $listeners_tags = $doc->getElementsByTagName('listeners');
            if ($listeners_tags->length === 0) {
                $listeners = $doc->firstChild->appendChild($doc->createElement('listeners'));
            } else {
                $listeners = $listeners_tags->item(0);
            }
            $listener = $doc->createElement('listener');
            $listener->setAttributeNode(
                new DomAttr('class', 'iakio\phpunit\smartrunner\DependencyListener'));
            $listeners->appendChild($listener);
            $phpunit_config = $doc->saveXML();
        } else {
            $phpunit_config = $this->defaultPhpUnitConfig();
        }

        return $phpunit_config;
    }

    public function run(array $argv = [])
    {
        if (file_exists($this->fs->config_file)) {
            echo $this->fs->relativePath($this->fs->config_file), " already exists.\n";
        } else {
            $this->fs->saveConfigFile($this->fs->loadConfig());
            echo $this->fs->relativePath($this->fs->config_file), " created.\n";
        }

        if (file_exists($this->fs->phpunit_config_file)) {
            echo $this->fs->relativePath($this->fs->phpunit_config_file), " already exists.\n";
        } else {
            $this->fs->savePhpUnitConfig($this->phpunitConfig($argv));
{'fraction_non_alphanumeric': 0.14547896150402864, 'fraction_numerical': 0.003133393017009848, 'mean_word_length': 2.388172858225929, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 2, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4485017', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1289, 'n_words': 238}