<commit_before><?php

namespace wcf\system\database;

use wcf\system\Regex;
use wcf\util\StringUtil;

/**
 * Wrapper around the \Redis class of php redis.
 *
 * @author  <NAME>
 * @copyright   2001-2019 WoltLab GmbH
 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package WoltLabSuite\Core\System\Database
 *
 * @method  int     del($key1, $key2 = null, $key3 = null)
 * @method  bool        expire($key, $ttl)
 * @method  string|bool get($key)
 * @method  int     hDel($key, $hashKey1, $hashKey2 = null, $hashKeyN = null)
 * @method  string      hGet($key, $hashKey)
 * @method  int     hSet($key, $hashKey, $value)
 * @method  int     incr($key)
 * @method  string      info($option = null)
 * @method  bool        setex($key, $ttl, $value)
 * @method  bool        setnx($key, $value)
 * @method  int     ttl($key)
 */
class Redis
{
    /**
     * wrapped redis connection
     * @var \Redis
     */
    protected $redis;

    /**
     * DSN string used to connect.
     * @var string
     */
    protected $dsn = '';

    /**
     * Connects to the redis server given by the DSN.
     *
     * @param string $dsn
     */
    public function __construct($dsn)
    {
        if (!\class_exists('Redis')) {
            throw new \BadMethodCallException('Redis support is not enabled.');
        }

        $this->dsn = $dsn;

        $this->redis = new \Redis();

        $regex = new Regex('^\[([a-z0-9\:\.]+)\](?::([0-9]{1,5}))?$', Regex::CASE_INSENSITIVE);
        $host = StringUtil::trim($this->dsn);
        $port = 6379; // default Redis port

        // check for IPv6
        if ($regex->match($host)) {
            $matches = $regex->getMatches();
            $host = $matches[1];

            if (isset($matches[2])) {
                $port = $matches[2];
            }
        } else {
            // IPv4 or host, try to get port
            if (\strpos($host, ':')) {
                $parsedHost = \explode(':', $host);
                $host = $parsedHost[0];
                $port = $parsedHost[1];
            }
        }

        if (!$this->redis->connect($host, (int)$port)) {
            throw new \RuntimeException('Unable to connect to Redis server');
        }<commit_msg>Add explicit check whether the port is numeric in Redis wrapper<commit_after><?php

namespace wcf\system\database;

use wcf\system\Regex;
use wcf\util\StringUtil;

/**
 * Wrapper around the \Redis class of php redis.
 *
 * @author  <NAME>
 * @copyright   2001-2019 WoltLab GmbH
 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package WoltLabSuite\Core\System\Database
 *
 * @method  int     del($key1, $key2 = null, $key3 = null)
 * @method  bool        expire($key, $ttl)
 * @method  string|bool get($key)
 * @method  int     hDel($key, $hashKey1, $hashKey2 = null, $hashKeyN = null)
 * @method  string      hGet($key, $hashKey)
 * @method  int     hSet($key, $hashKey, $value)
 * @method  int     incr($key)
 * @method  string      info($option = null)
 * @method  bool        setex($key, $ttl, $value)
 * @method  bool        setnx($key, $value)
 * @method  int     ttl($key)
 */
class Redis
{
    /**
     * wrapped redis connection
     * @var \Redis
     */
    protected $redis;

    /**
     * DSN string used to connect.
     * @var string
     */
    protected $dsn = '';

    /**
     * Connects to the redis server given by the DSN.
     *
     * @param string $dsn
     */
    public function __construct($dsn)
    {
        if (!\class_exists('Redis')) {
            throw new \BadMethodCallException('Redis support is not enabled.');
        }

        $this->dsn = $dsn;

        $this->redis = new \Redis();

        $regex = new Regex('^\[([a-z0-9\:\.]+)\](?::([0-9]{1,5}))?$', Regex::CASE_INSENSITIVE);
        $host = StringUtil::trim($this->dsn);
        $port = 6379; // default Redis port

        // check for IPv6
        if ($regex->match($host)) {
            $matches = $regex->getMatches();
            $host = $matches[1];

            if (isset($matches[2])) {
                $port = $matches[2];
            }
        } else {
            // IPv4 or host, try to get port
            if (\strpos($host, ':')) {
                $parsedHost = \explode(':', $host);
                $host = $parsedHost[0];
                $port = $parsedHost[1];
            }
        }

        if (!\is_numeric($port)) {
            throw new \InvalidArgumentException(\sprintf(
                "The given port '%s' is not numeric.",
                $port
            ));
        }

        if (!$this->redis->connect($host, (int)$port)) {
            throw new \RuntimeException('Unable to connect to Redis server');
        }
{'fraction_non_alphanumeric': 0.15903817759966252, 'fraction_numerical': 0.012655557899177388, 'mean_word_length': 1.9416873449131513, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '5214947', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1528, 'n_words': 381}