<commit_before><?php
/**
 * This file is part of the League.url library
 *
 * @license http://opensource.org/licenses/MIT
 * @link https://github.com/thephpleague/url/
 * @version 4.0.0
 * @package League.url
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace League\Url\Utilities;

use InvalidArgumentException;

/**
 * A Trait to fetch URI info from the Server variables
 *
 * @package League.url
 * @since 4.0.0
 */
trait ServerInfo
{
    /**
     * Returns the environment scheme
     *
     * @param  array $server the environment server typically $_SERVER
     *
     * @return string
     */
    protected static function fetchServerScheme(array $server)
    {
        if (isset($server["HTTP_X_FORWARDED_PROTO"]) && ! empty($server['HTTP_X_FORWARDED_PROTO'])) {
            return strtolower($server["HTTP_X_FORWARDED_PROTO"]).":";
        }

        $scheme = "http";
        if (isset($server["HTTPS"]) && "off" != $server["HTTPS"]) {
            $scheme .= "s";
        }

        return $scheme.":";
    }

    /**
     * Returns the environment host
     *
     * @param array $server the environment server typically $_SERVER
     *
     * @throws \InvalidArgumentException If the host can not be detected
     *
     * @return string
     */
    protected static function fetchServerHost(array $server)
    {
        if (isset($server["HTTP_HOST"])) {
            $header = $server["HTTP_HOST"];
            if (preg_match("/^(.*)(:\d+)$/", $header, $matches)) {
                return $matches[1];
            }

            return $header;
        }

        if (isset($server["SERVER_ADDR"])) {
            if (filter_var($server["SERVER_ADDR"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
                return "[".$server["SERVER_ADDR"]."]";
            }
            return $server["SERVER_ADDR"];
        }

        throw new InvalidArgumentException("Host could not be detected");
    }

    /**
     * Returns the environment user info
     *
     * @param array $server the environment server typically $_SERVER
     *
     * @return string
     */
    protected static function fetchServerUserInfo(array $server)
    {
        $user = '';
        if (isset($server['PHP_AUTH_USER'])) {
            $user = $server['PHP_AUTH_USER'];
        }

        $pass = '';
        if (isset($server['PHP_AUTH_PW']) && ! empty($server['PHP_AUTH_PW'])) {
            $pass = ':'.$server['PHP_AUTH_PW'];
        }

        $info = $user.$pass;
        if (! empty($info)) {
            $info .= '@';
        }

        return $info;
    }

    /**
     * Returns the environment port
     *
     * @param array $server the environment server typically $_SERVER
     *
     * @return string
     */
    protected static function fetchServerPort(array $server)
    {
        $port = "";
        if (isset($server["SERVER_PORT"])) {
            $port = ":".$server["SERVER_PORT"];
        }

        return $port;
    }

    /**
     * Returns the environment path
     *
     * @param array $server the environment server typically $_SERVER
     *
     * @return string
     */
    protected static function fetchServerRequestUri(array $server)
    {
        if (isset($server["REQUEST_URI"])) {
            return $server["REQUEST_URI"];
        }

        $request = "";
        if (isset($server["PHP_SELF"])) {
            $request .= $server["PHP_SELF"];
        }

        if (isset($server["QUERY_STRING"]) && ! empty($server["QUERY_STRING"])) {
            $request .= "?".$server["QUERY_STRING"];
        }

        return $request;
    }
}<commit_msg>improve ServerInfo trait<commit_after><?php
/**
 * This file is part of the League.url library
 *
 * @license http://opensource.org/licenses/MIT
 * @link https://github.com/thephpleague/url/
 * @version 4.0.0
 * @package League.url
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace League\Url\Utilities;

use InvalidArgumentException;

/**
 * A Trait to fetch URI info from the Server variables
 *
 * @package League.url
 * @since 4.0.0
 */
trait ServerInfo
{
    /**
     * Returns the environment scheme
     *
     * @param  array $server the environment server typically $_SERVER
     *
     * @return string
     */
    protected static function fetchServerScheme(array $server)
    {
        $args = filter_var_array($server, [
            'HTTP_X_FORWARDED_PROTO' => ['filter' => FILTER_SANITIZE_STRING, 'options' => ['default' => '']],
            'HTTPS' => ['filter' => FILTER_VALIDATE_BOOLEAN, 'flags' => FILTER_NULL_ON_FAILURE],
        ]);

        if (! empty($args["HTTP_X_FORWARDED_PROTO"])) {
            return strtolower($args["HTTP_X_FORWARDED_PROTO"]).":";
        }

        if ($args["HTTPS"]) {
            return "https:";
        }

        return "http:";
    }

    /**
     * Returns the environment host
     *
     * @param array $server the environment server typically $_SERVER
     *
     * @throws \InvalidArgumentException If the host can not be detected
     *
     * @return string
     */
    protected static function fetchServerHost(array $server)
    {
        if (isset($server["HTTP_HOST"])) {
            $header = $server["HTTP_HOST"];
            if (preg_match("/^(.*)(:\d+)$/", $header, $matches)) {
                return $matches[1];
            }

            return $header;
        }

        if (! isset($server["SERVER_ADDR"])) {
            throw new InvalidArgumentException("Host could not be detected");
        }

        if (filter_var($server["SERVER_ADDR"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
            return "[".$server["SERVER_ADDR"]."]";
        }

        return $server["SERVER_ADDR"];
    }

    /**
     * Returns the environment user info
     *
     * @param array $server the environment server typically $_SERVER
     *
     * @return string
     */
    protected static function fetchServerUserInfo(array $server)
    {
        if (! isset($server['PHP_AUTH_USER'])) {
            return '';
        }

        $info = $server['PHP_AUTH_USER'];
        if (isset($server['PHP_AUTH_PW'])) {
            $info .= ':'.$server['PHP_AUTH_PW'];
        }

        if (! empty($info)) {
            $info .= '@';
        }

        return $info;
    }

    /**
     * Returns the environment port
     *
     * @param array $server the environment server typically $_SERVER
     *
     * @return string
     */
    protected static function fetchServerPort(array $server)
    {
        $port = "";
        if (isset($server["SERVER_PORT"])) {
            $port = ":".$server["SERVER_PORT"];
        }

        return $port;
    }

    /**
     * Returns the environment path
     *
     * @param array $server the environment server typically $_SERVER
     *
     * @return string
     */
    protected static function fetchServerRequestUri(array $server)
    {
        if (isset($server["REQUEST_URI"])) {
            return $server["REQUEST_URI"];
        }

        $request = "";
        if (isset($server["PHP_SELF"])) {
            $request .= $server["PHP_SELF"];
        }

        if (isset($server["QUERY_STRING"])) {
            $request .= "?".$server["QUERY_STRING"];
        }

        return $request;
    }
}
{'fraction_non_alphanumeric': 0.14105177333876887, 'fraction_numerical': 0.002174208452235358, 'mean_word_length': 2.1094212082805237, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 2, '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': '3075107', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2069, 'n_words': 496}