<commit_before><?php

/**
 * MIT License. This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Propel\Common\Config;

use Propel\Common\Config\Exception\InvalidArgumentException;
use Propel\Common\Config\Exception\XmlParseException;
use SimpleXMLElement;

/**
 * Class to convert an xml string to array
 */
class XmlToArrayConverter
{
    /**
     * Create a PHP array from the XML file
     *
     * @param string $xmlToParse The XML file or a string containing xml to parse
     *
     * @throws \Propel\Common\Config\Exception\XmlParseException if parse errors occur
     * @throws \Propel\Common\Config\Exception\InvalidArgumentException
     *
     * @return array
     */
    public static function convert($xmlToParse)
    {
        if (!is_string($xmlToParse)) {
            throw new InvalidArgumentException('XmlToArrayConverter::convert method expects an xml file to parse, or a string containing valid xml');
        }

        $isFile = file_exists($xmlToParse);

        if ($isFile) {
            $xmlPrefix = file_get_contents($xmlToParse, false, null, 0, 1);
        } else {
            $xmlPrefix = $xmlToParse[0] ?? '';
        }

        //Empty xml file returns empty array
        if ($xmlPrefix === '') {
            return [];
        }

        if ($xmlPrefix !== '<') {
            throw new InvalidArgumentException('Invalid xml content');
        }

        $currentInternalErrors = libxml_use_internal_errors(true);

        if ($isFile) {
            $xml = simplexml_load_file($xmlToParse);
            if ($xml instanceof SimpleXMLElement) {
                dom_import_simplexml($xml)->ownerDocument->xinclude();
            }
        } else {
            $xml = simplexml_load_string($xmlToParse);
        }

        $errors = libxml_get_errors();

        libxml_clear_errors();
        libxml_use_internal_errors($currentInternalErrors);

        if (count($errors) > 0) {
            throw new XmlParseException($errors);
        }

        return self::simpleXmlToArray($xml);
    }

    /**
     * Recursive function that converts an SimpleXML object into an array.
     *
     * @author <NAME> (based on code form php.net manual comment)
     *
     * @param \SimpleXMLElement $xml SimpleXML object.
     *
     * @return array Array representation of SimpleXML object.
     */
    protected static function simpleXmlToArray($xml)
    {
        $ar = [];
        foreach ($xml->children() as $k => $v) {<commit_msg>fix stan<commit_after><?php

/**
 * MIT License. This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Propel\Common\Config;

use Propel\Common\Config\Exception\InvalidArgumentException;
use Propel\Common\Config\Exception\XmlParseException;
use SimpleXMLElement;

/**
 * Class to convert an xml string to array
 */
class XmlToArrayConverter
{
    /**
     * Create a PHP array from the XML file
     *
     * @psalm-return array<scalar, mixed>
     *
     * @param string $xmlToParse The XML file or a string containing xml to parse
     *
     * @throws \Propel\Common\Config\Exception\XmlParseException if parse errors occur
     * @throws \Propel\Common\Config\Exception\InvalidArgumentException
     *
     * @return array
     */
    public static function convert($xmlToParse)
    {
        if (!is_string($xmlToParse)) {
            throw new InvalidArgumentException('XmlToArrayConverter::convert method expects an xml file to parse, or a string containing valid xml');
        }

        $isFile = file_exists($xmlToParse);

        if ($isFile) {
            $xmlPrefix = file_get_contents($xmlToParse, false, null, 0, 1);
        } else {
            $xmlPrefix = $xmlToParse[0] ?? '';
        }

        //Empty xml file returns empty array
        if ($xmlPrefix === '') {
            return [];
        }

        if ($xmlPrefix !== '<') {
            throw new InvalidArgumentException('Invalid xml content');
        }

        $currentInternalErrors = libxml_use_internal_errors(true);

        if ($isFile) {
            $xml = simplexml_load_file($xmlToParse);
            if ($xml instanceof SimpleXMLElement) {
                dom_import_simplexml($xml)->ownerDocument->xinclude();
            }
        } else {
            $xml = simplexml_load_string($xmlToParse);
        }

        $errors = libxml_get_errors();

        libxml_clear_errors();
        libxml_use_internal_errors($currentInternalErrors);

        if (count($errors) > 0) {
            throw new XmlParseException($errors);
        }

        return self::simpleXmlToArray($xml);
    }

    /**
     * Recursive function that converts an SimpleXML object into an array.
     *
     * @author <NAME> (based on code form php.net manual comment)
     *
     * @psalm-return array<scalar, mixed>
     *
     * @param \SimpleXMLElement $xml SimpleXML object.
     *
     * @return array Array representation of SimpleXML object.
     */
    protected static function simpleXmlToArray($xml)
    {
        $ar = [];
        foreach ($xml->children() as $k => $v) {
{'fraction_non_alphanumeric': 0.1037431122933688, 'fraction_numerical': 0.0015200456013680411, 'mean_word_length': 2.3917525773195876, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 66}, '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': '4901185', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1402, 'n_words': 401}