<commit_before><?php

namespace Predis;

use Predis\IConnectionParameters;

class ConnectionParameters implements IConnectionParameters {
    private static $_defaultParameters;
    private static $_validators;

    private $_parameters;
    private $_userDefined;

    public function __construct($parameters = array()) {
        self::ensureDefaults();
        $extractor = is_array($parameters) ? 'filter' : 'parseURI';
        $parameters = $this->$extractor($parameters);
        $this->_userDefined = array_keys($parameters);
        $this->_parameters = array_merge(self::$_defaultParameters, $parameters);
    }

    private static function ensureDefaults() {
        if (!isset(self::$_defaultParameters)) {
            self::$_defaultParameters = array(
                'scheme' => 'tcp',
                'host' => '127.0.0.1',
                'port' => 6379,
                'database' => null,
                'password' => <PASSWORD>,
                'connection_async' => false,
                'connection_persistent' => false,
                'connection_timeout' => 5.0,
                'read_write_timeout' => null,
                'alias' => null,
                'weight' => null,
                'path' => null,
                'iterable_multibulk' => false,
                'throw_errors' => true,
            );
        }
        if (!isset(self::$_validators)) {
            $boolValidator = function($value) { return (bool) $value; };
            $floatValidator = function($value) { return (float) $value; };
            $intValidator = function($value) { return (int) $value; };

            self::$_validators = array(
                'port' => $intValidator,
                'connection_async' => $boolValidator,
                'connection_persistent' => $boolValidator,
                'connection_timeout' => $floatValidator,
                'read_write_timeout' => $floatValidator,
                'iterable_multibulk' => $boolValidator,
                'throw_errors' => $boolValidator,
            );
        }
    }

    public static function define($parameter, $default, $callable = null) {
        self::ensureDefaults();
        self::$_defaultParameters[$parameter] = $default;
        if (!isset($callable)) {
            unset(self::$_validators[$parameter]);
            return;
        }
        if (!is_callable($callable)) {
            throw new \InvalidArgumentException(
                "The validator for $parameter must be a callable object"
            );
        }
        self::$_validators[$parameter] = $callable;
    }

    private function parseURI($uri) {
        if (stripos($uri, 'unix') === 0) {
            // Hack to support URIs for UNIX sockets with minimal effort.
            $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
        }
        if (($parsed = @parse_url($uri)) === false || !isset($parsed['host'])) {
            throw new ClientException("Invalid URI: $uri");
        }
        if (isset($parsed['query'])) {
            foreach (explode('&', $parsed['query']) as $kv) {
                @list($k, $v) = explode('=', $kv);
                $parsed[$k] = $v;
            }
            unset($parsed['query']);
        }
        return $this->filter($parsed);
    }

    private function filter(Array $parameters) {
        if (count($parameters) > 0) {
            $validators = self::$_validators;
            foreach ($parameters as $parameter => $value) {
                if (isset($validators[$parameter])) {
                    $parameters[$parameter] = $validators[$parameter]($value);
                }
            }
        }
        return $parameters;
    }

    public function __get($parameter) {
        return $this->_parameters[$parameter];
    }

    public function __isset($parameter) {
        return in_array($parameter, $this->_userDefined);
    }

    protected function getBaseURI() {<commit_msg>Implement support for using Predis\Options\IOption with Predis\ConnectionParameters.<commit_after><?php

namespace Predis;

use Predis\IConnectionParameters;
use Predis\Options\IOption;

class ConnectionParameters implements IConnectionParameters {
    private static $_defaultParameters;
    private static $_validators;

    private $_parameters;
    private $_userDefined;

    public function __construct($parameters = array()) {
        self::ensureDefaults();
        $extractor = is_array($parameters) ? 'filter' : 'parseURI';
        $parameters = $this->$extractor($parameters);
        $this->_userDefined = array_keys($parameters);
        $this->_parameters = array_merge(self::$_defaultParameters, $parameters);
    }

    private static function ensureDefaults() {
        if (!isset(self::$_defaultParameters)) {
            self::$_defaultParameters = array(
                'scheme' => 'tcp',
                'host' => '127.0.0.1',
                'port' => 6379,
                'database' => null,
                'password' => <PASSWORD>,
                'connection_async' => false,
                'connection_persistent' => false,
                'connection_timeout' => 5.0,
                'read_write_timeout' => null,
                'alias' => null,
                'weight' => null,
                'path' => null,
                'iterable_multibulk' => false,
                'throw_errors' => true,
            );
        }
        if (!isset(self::$_validators)) {
            $boolValidator = function($value) { return (bool) $value; };
            $floatValidator = function($value) { return (float) $value; };
            $intValidator = function($value) { return (int) $value; };

            self::$_validators = array(
                'port' => $intValidator,
                'connection_async' => $boolValidator,
                'connection_persistent' => $boolValidator,
                'connection_timeout' => $floatValidator,
                'read_write_timeout' => $floatValidator,
                'iterable_multibulk' => $boolValidator,
                'throw_errors' => $boolValidator,
            );
        }
    }

    public static function define($parameter, $default, $callable = null) {
        self::ensureDefaults();
        self::$_defaultParameters[$parameter] = $default;
        if ($default instanceof IOption) {
            self::$_validators[$parameter] = $default;
            return;
        }
        if (!isset($callable)) {
            unset(self::$_validators[$parameter]);
            return;
        }
        if (!is_callable($callable)) {
            throw new \InvalidArgumentException(
                "The validator for $parameter must be a callable object"
            );
        }
        self::$_validators[$parameter] = $callable;
    }

    private function parseURI($uri) {
        if (stripos($uri, 'unix') === 0) {
            // Hack to support URIs for UNIX sockets with minimal effort.
            $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
        }
        if (($parsed = @parse_url($uri)) === false || !isset($parsed['host'])) {
            throw new ClientException("Invalid URI: $uri");
        }
        if (isset($parsed['query'])) {
            foreach (explode('&', $parsed['query']) as $kv) {
                @list($k, $v) = explode('=', $kv);
                $parsed[$k] = $v;
            }
            unset($parsed['query']);
        }
        return $this->filter($parsed);
    }

    private function filter(Array $parameters) {
        if (count($parameters) > 0) {
            $validators = self::$_validators;
            foreach ($parameters as $parameter => $value) {
                if (isset($validators[$parameter])) {
                    $parameters[$parameter] = $validators[$parameter]($value);
                }
            }
        }
        return $parameters;
    }

    public function __get($parameter) {
        $value = $this->_parameters[$parameter];
        if ($value instanceof IOption) {
            $this->_parameters[$parameter] = ($value = $value->getDefault());
        }
        return $value;
    }

    public function __isset($parameter) {
        return in_array($parameter, $this->_userDefined);
    }

    protected function getBaseURI() {
{'fraction_non_alphanumeric': 0.14040219378427787, 'fraction_numerical': 0.003412553321145643, 'mean_word_length': 1.978584392014519, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2745934', 'n_tokens_mistral': 2203, 'n_tokens_neox': 2124, 'n_words': 454}