<commit_before><?php

/*
 * This file is part of the Symfony package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\HttpFoundation;

use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * FileBag is a container for HTTP headers.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class FileBag extends ParameterBag
{
    private $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');

    /**
     * (non-PHPdoc)
     * @see Symfony\Component\HttpFoundation\ParameterBag::replace()
     */
    public function replace(array $files = array())
    {
        $this->parameters = array();
        $this->add($files);
    }

    /**
     * (non-PHPdoc)
     * @see Symfony\Component\HttpFoundation\ParameterBag::set()
     */
    public function set($key, $value)
    {
        if (is_array($value)) {
            parent::set($key, $this->convertFileInformation($value));
        }
    }

    /**
     * (non-PHPdoc)
     * @see Symfony\Component\HttpFoundation\ParameterBag::add()
     */
    public function add(array $files = array())
    {
        foreach ($files as $key => $file) {
            $this->set($key, $file);
        }
    }

    /**
     * Converts uploaded files to UploadedFile instances.
     *
     * @param  array $file A (multi-dimensional) array of uploaded file information
     *
     * @return array A (multi-dimensional) array of UploadedFile instances
     */
    protected function convertFileInformation(array $file)
    {
        $file = $this->fixPhpFilesArray($file);
        if (is_array($file)) {
            $keys = array_keys($file);
            sort($keys);
            if ($keys == $this->fileKeys) {
                $file['error'] = (int) $file['error'];
            }
            if ($keys != $this->fileKeys) {
                $file = array_map(array($this, 'convertFileInformation'), $file);
            } else
                if ($file['error'] === UPLOAD_ERR_NO_FILE) {
                    $file = null;
                } else {
                    $file = new UploadedFile($file['tmp_name'], $file['name'],
                    $file['type'], $file['size'], $file['error']);
                }
        }
        return $file;
    }

    /**
     * Fixes a malformed PHP $_FILES array.
     *
     * PHP has a bug that the format of the $_FILES array differs, depending on
     * whether the uploaded file fields had normal field names or array-like
     * field names ("normal" vs. "parent[child]").
     *
     * This method fixes the array to look like the "normal" $_FILES array.
     *
     * It's safe to pass an already converted array, in which case this method
     * just returns the original array unmodified.
     *
     * @param  array $data
     * @return array
     */
    protected function fixPhpFilesArray($data)
    {
        if (! is_array($data)) {
            return $data;
        }
        $keys = array_keys($data);
        sort($keys);
        if ($this->fileKeys != $keys || ! isset($data['name']) ||
         ! is_array($data['name'])) {
            return $data;
        }
        $files = $data;
        foreach ($this->fileKeys as $k) {
            unset($files[$k]);
        }
        foreach (array_keys($data['name']) as $key) {
            $files[$key] = $this->fixPhpFilesArray(array(
                'error'    => $data['error'][$key],
                'name'     => $data['name'][$key], 'type' => $data['type'][$key],
                'tmp_name' => $data['tmp_name'][$key],
                'size'     => $data['size'][$key]
            ));
        }
        return $files;
    }
}
<commit_msg>[HttpFoundation] fixed FileBag to handle sub-requests<commit_after><?php

/*
 * This file is part of the Symfony package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\HttpFoundation;

use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * FileBag is a container for HTTP headers.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class FileBag extends ParameterBag
{
    private $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');

    /**
     * (non-PHPdoc)
     * @see Symfony\Component\HttpFoundation\ParameterBag::replace()
     */
    public function replace(array $files = array())
    {
        $this->parameters = array();
        $this->add($files);
    }

    /**
     * (non-PHPdoc)
     * @see Symfony\Component\HttpFoundation\ParameterBag::set()
     */
    public function set($key, $value)
    {
        if (is_array($value) || $value instanceof UploadedFile) {
            parent::set($key, $this->convertFileInformation($value));
        }
    }

    /**
     * (non-PHPdoc)
     * @see Symfony\Component\HttpFoundation\ParameterBag::add()
     */
    public function add(array $files = array())
    {
        foreach ($files as $key => $file) {
            $this->set($key, $file);
        }
    }

    /**
     * Converts uploaded files to UploadedFile instances.
     *
     * @param  array|UploadedFile $file A (multi-dimensional) array of uploaded file information
     *
     * @return array A (multi-dimensional) array of UploadedFile instances
     */
    protected function convertFileInformation($file)
    {
        if ($file instanceof UploadedFile) {
            return $file;
        }
        $file = $this->fixPhpFilesArray($file);
        if (is_array($file)) {
            $keys = array_keys($file);
            sort($keys);
            if ($keys == $this->fileKeys) {
                $file['error'] = (int) $file['error'];
            }
            if ($keys != $this->fileKeys) {
                $file = array_map(array($this, 'convertFileInformation'), $file);
            } else
                if ($file['error'] === UPLOAD_ERR_NO_FILE) {
                    $file = null;
                } else {
                    $file = new UploadedFile($file['tmp_name'], $file['name'],
                    $file['type'], $file['size'], $file['error']);
                }
        }
        return $file;
    }

    /**
     * Fixes a malformed PHP $_FILES array.
     *
     * PHP has a bug that the format of the $_FILES array differs, depending on
     * whether the uploaded file fields had normal field names or array-like
     * field names ("normal" vs. "parent[child]").
     *
     * This method fixes the array to look like the "normal" $_FILES array.
     *
     * It's safe to pass an already converted array, in which case this method
     * just returns the original array unmodified.
     *
     * @param  array $data
     * @return array
     */
    protected function fixPhpFilesArray($data)
    {
        if (! is_array($data)) {
            return $data;
        }
        $keys = array_keys($data);
        sort($keys);
        if ($this->fileKeys != $keys || ! isset($data['name']) ||
         ! is_array($data['name'])) {
            return $data;
        }
        $files = $data;
        foreach ($this->fileKeys as $k) {
            unset($files[$k]);
        }
        foreach (array_keys($data['name']) as $key) {
            $files[$key] = $this->fixPhpFilesArray(array(
                'error'    => $data['error'][$key],
                'name'     => $data['name'][$key], 'type' => $data['type'][$key],
                'tmp_name' => $data['tmp_name'][$key],
                'size'     => $data['size'][$key]
            ));
        }
        return $files;
    }
}

{'fraction_non_alphanumeric': 0.1506670154329061, 'fraction_numerical': 0.0, 'mean_word_length': 2.0106299212598424, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 53, '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': '6043874', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2204, 'n_words': 561}