<commit_before><?php
namespace PharIo\Phive;

class PhiveXmlConfig {

    /**
     * @var XmlFile
     */
    private $configFile;

    /**
     * @var VersionConstraintParser
     */
    private $versionConstraintParser;

    /**
     * @param XmlFile $configFile
     * @param VersionConstraintParser $versionConstraintParser
     */
    public function __construct(XmlFile $configFile, VersionConstraintParser $versionConstraintParser) {
        $this->configFile = $configFile;
        $this->versionConstraintParser = $versionConstraintParser;
    }

    /**
     * @param RequestedPhar $requestedPhar
     * @param Phar $installedPhar
     * @param Directory $location
     */
    public function addPhar(RequestedPhar $requestedPhar, Phar $installedPhar, Directory $location) {
        $name = (string)$requestedPhar->getAlias();
        if ($this->hasPharNode($name)) {
            $pharNode = $this->getPharNode($name);
        } else {
            $pharNode = $this->configFile->createElement('phar');
            $pharNode->setAttribute('name', $name);
            $this->configFile->addElement($pharNode);
        }

        $xmlFileDirectory = $this->configFile->getDirectory();

        $pharNode->setAttribute('version', $requestedPhar->getAlias()->getVersionConstraint()->asString());
        $pharNode->setAttribute('installed', $installedPhar->getVersion()->getVersionString());
        $pharNode->setAttribute('location', $location->getRelativePathTo($xmlFileDirectory));
        $this->configFile->save();
    }

    /**
     * @param string $name
     *
     * @return bool
     */
    public function hasPharLocation($name) {
        return $this->hasPharNode($name);
    }

    /**
     * @param string $name
     *
     * @return Filename
     */
    public function getPharLocation($name) {
        $node = $this->getPharNode($name);
        return (new Directory($node->getAttribute('location')))->file($name);
    }

    /**
     * @param string $name
     */
    public function removePhar($name) {
        if (!$this->hasPharNode($name)) {
            return;
        }
        $pharNode = $this->getPharNode($name);
        $pharNode->parentNode->removeChild($pharNode);
        $this->configFile->save();
    }

    /**
     * @param string $name
     *
     * @return bool
     */
    private function hasPharNode($name) {
        return $this->getPharNode($name) !== null;
    }

    /**
     * @param string $name
     *
     * @return \DOMElement
     */
    private function getPharNode($name) {
        return $this->configFile->query(sprintf('//phive:phar[@name="%s"]', mb_strtolower($name)))->item(0);
    }

    /**
     * @return ConfiguredPhar[]
     */
    public function getPhars() {
        $phars = [];
        /** @var \DOMElement $pharNode */
        foreach ($this->configFile->query('//phive:phar') as $pharNode) {
            if ($pharNode->hasAttribute('url')) {
                $url = new PharUrl($pharNode->getAttribute('url'));
                $pharName = (string)$url;
                $versionConstraint = $url->getPharVersion()->getVersionString();
            } else {
                $pharName = $pharNode->getAttribute('name');
                $versionConstraint = $pharNode->getAttribute('version');
            }
            $pharVersion = null;
            if ($pharNode->hasAttribute('installed') && !empty($pharNode->getAttribute('installed'))) {
                $pharVersion = new Version($pharNode->getAttribute('installed'));
            }
            $phars[] = new ConfiguredPhar(
                $pharName,
                $this->versionConstraintParser->parse($versionConstraint),
                $pharVersion
            );
        }

        return $phars;
    }

    /**
     * @param \DOMElement $element
     *
     * @return PharAlias
     */
    private function getPharAliasFromNode(\DOMElement $element) {
        $parser = new VersionConstraintParser();
        if ($element->hasAttribute('version')) {
            $version = $parser->parse($element->getAttribute('version'));
        } else {
            $version = new AnyVersionConstraint();
        }
        return new PharAlias($element->getAttribute('name'), $version);
    }

    /**
     * @return bool
     */
    public function hasTargetDirectory() {
        return $this->getTargetDirectoryNode() !== null;
    }

    /**
     * @return Directory
     * @throws ConfigException
     */<commit_msg>remove unused code<commit_after><?php
namespace PharIo\Phive;

class PhiveXmlConfig {

    /**
     * @var XmlFile
     */
    private $configFile;

    /**
     * @var VersionConstraintParser
     */
    private $versionConstraintParser;

    /**
     * @param XmlFile $configFile
     * @param VersionConstraintParser $versionConstraintParser
     */
    public function __construct(XmlFile $configFile, VersionConstraintParser $versionConstraintParser) {
        $this->configFile = $configFile;
        $this->versionConstraintParser = $versionConstraintParser;
    }

    /**
     * @param RequestedPhar $requestedPhar
     * @param Phar $installedPhar
     * @param Directory $location
     */
    public function addPhar(RequestedPhar $requestedPhar, Phar $installedPhar, Directory $location) {
        $name = (string)$requestedPhar->getAlias();
        if ($this->hasPharNode($name)) {
            $pharNode = $this->getPharNode($name);
        } else {
            $pharNode = $this->configFile->createElement('phar');
            $pharNode->setAttribute('name', $name);
            $this->configFile->addElement($pharNode);
        }

        $xmlFileDirectory = $this->configFile->getDirectory();

        $pharNode->setAttribute('version', $requestedPhar->getAlias()->getVersionConstraint()->asString());
        $pharNode->setAttribute('installed', $installedPhar->getVersion()->getVersionString());
        $pharNode->setAttribute('location', $location->getRelativePathTo($xmlFileDirectory));
        $this->configFile->save();
    }

    /**
     * @param string $name
     *
     * @return bool
     */
    public function hasPharLocation($name) {
        return $this->hasPharNode($name);
    }

    /**
     * @param string $name
     *
     * @return Filename
     */
    public function getPharLocation($name) {
        $node = $this->getPharNode($name);
        return (new Directory($node->getAttribute('location')))->file($name);
    }

    /**
     * @param string $name
     */
    public function removePhar($name) {
        if (!$this->hasPharNode($name)) {
            return;
        }
        $pharNode = $this->getPharNode($name);
        $pharNode->parentNode->removeChild($pharNode);
        $this->configFile->save();
    }

    /**
     * @param string $name
     *
     * @return bool
     */
    private function hasPharNode($name) {
        return $this->getPharNode($name) !== null;
    }

    /**
     * @param string $name
     *
     * @return \DOMElement
     */
    private function getPharNode($name) {
        return $this->configFile->query(sprintf('//phive:phar[@name="%s"]', mb_strtolower($name)))->item(0);
    }

    /**
     * @return ConfiguredPhar[]
     */
    public function getPhars() {
        $phars = [];
        /** @var \DOMElement $pharNode */
        foreach ($this->configFile->query('//phive:phar') as $pharNode) {
            if ($pharNode->hasAttribute('url')) {
                $url = new PharUrl($pharNode->getAttribute('url'));
                $pharName = (string)$url;
                $versionConstraint = $url->getPharVersion()->getVersionString();
            } else {
                $pharName = $pharNode->getAttribute('name');
                $versionConstraint = $pharNode->getAttribute('version');
            }
            $pharVersion = null;
            if ($pharNode->hasAttribute('installed') && !empty($pharNode->getAttribute('installed'))) {
                $pharVersion = new Version($pharNode->getAttribute('installed'));
            }
            $phars[] = new ConfiguredPhar(
                $pharName,
                $this->versionConstraintParser->parse($versionConstraint),
                $pharVersion
            );
        }

        return $phars;
    }
    
    /**
     * @return bool
     */
    public function hasTargetDirectory() {
        return $this->getTargetDirectoryNode() !== null;
    }

    /**
     * @return Directory
     * @throws ConfigException
     */
{'fraction_non_alphanumeric': 0.14993465605322562, 'fraction_numerical': 0.00023761435190685517, 'mean_word_length': 2.2805923616523773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '393446', 'n_tokens_mistral': 2425, 'n_tokens_neox': 2374, 'n_words': 386}