<commit_before><?php

/**
 * PHP Domain Parser: Public Suffix List based URL parsing.
 *
 * @link      http://github.com/jeremykendall/php-domain-parser for the canonical source repository
 *
 * @copyright Copyright (c) 2014 <NAME> (http://about.me/jeremykendall)
 * @license   http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
 */
namespace Pdp;

use Pdp\Exception\SeriouslyMalformedUrlException;
use Pdp\Uri\Url;
use Pdp\Uri\Url\Host;

/**
 * Parser.
 *
 * This class is reponsible for Public Suffix List based url parsing
 */
class Parser
{
    /**
     * @var string RFC 3986 compliant scheme regex pattern
     *
     * @see https://tools.ietf.org/html/rfc3986#section-3.1
     */
    const SCHEME_PATTERN = '#^([a-zA-Z][a-zA-Z0-9+\-.]*)://#';

    /**
     * @var string IP address regex pattern
     */
    const IP_ADDRESS_PATTERN = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';

    /**
     * @var PublicSuffixList Public Suffix List
     */
    protected $publicSuffixList;

    /**
     * @var bool Whether or not a host part has been normalized
     */
    protected $isNormalized = false;

    /**
     * Public constructor.
     *
     * @codeCoverageIgnore
     *
     * @param PublicSuffixList $publicSuffixList Instance of PublicSuffixList
     */
    public function __construct(PublicSuffixList $publicSuffixList)
    {
        $this->publicSuffixList = $publicSuffixList;
    }

    /**
     * Parses url.
     *
     * @param string $url Url to parse
     *
     * @return Url Object representation of url
     */
    public function parseUrl($url)
    {
        $rawUrl = $url;
        $elem = array(
            'scheme' => null,
            'user' => null,
            'pass' => null,
            'host' => null,
            'port' => null,
            'path' => null,
            'query' => null,
            'fragment' => null,
        );

        if (preg_match(self::SCHEME_PATTERN, $url) === 0) {
            // Wacky scheme required to overcome parse_url behavior in PHP lt 5.4.7
            // See https://github.com/jeremykendall/php-domain-parser/issues/49
            $url = 'php-lt-5.4.7-hack://' . preg_replace('#^//#', '', $url, 1);
        }

        $parts = pdp_parse_url($url);

        if ($parts === false) {
            throw new SeriouslyMalformedUrlException($rawUrl);
        }

        if ($parts['scheme'] === 'php-lt-5.4.7-hack') {
            // Remove wacky scheme required to overcome parse_url behavior in PHP lt 5.4.7
            // See https://github.com/jeremykendall/php-domain-parser/issues/49
            $parts['scheme'] = null;
        }

        $elem = (array) $parts + $elem;

        $host = $this->parseHost($parts['host']);

        return new Url(
            $elem['scheme'],
            $elem['user'],
            $elem['pass'],
            $host,
            $elem['port'],
            $elem['path'],
            $elem['query'],
            $elem['fragment']
        );
    }

    /**
     * Parses host part of url.
     *
     * @param string $host Host part of url
     *
     * @return Host Object representation of host portion of url
     */
    public function parseHost($host)
    {
        $host = mb_strtolower($host, 'UTF-8');

        $subdomain = null;
        $registrableDomain = null;
        $publicSuffix = null;

        // Fixes #22: Single label domains are set as Host::$host and all other
        // properties are null.
        // Fixes #43: Ip Addresses should not be parsed
        if ($this->isMultiLabelDomain($host) || !$this->isIpv4Address($host)) {
            $subdomain = $this->getSubdomain($host);
            $registrableDomain = $this->getRegistrableDomain($host);
            $publicSuffix = $this->getPublicSuffix($host);
        }

        return new Host(
            $subdomain,
            $registrableDomain,
            $publicSuffix,
            $host
        );
    }

    /**
     * Get the raw public suffix based on the cached public suffix list file.
     * Return false if the provided suffix is not included in the PSL.
     *
     * @param string $host The host to process
     *
     * @return string|false The suffix or false if suffix not included in the PSL
     */
    protected function getRawPublicSuffix($host)
    {
        $host = $this->normalize($host);

        $parts = array_reverse(explode('.', $host));<commit_msg>simplify parseHost as host validation is already handled within sub-methods<commit_after><?php

/**
 * PHP Domain Parser: Public Suffix List based URL parsing.
 *
 * @link      http://github.com/jeremykendall/php-domain-parser for the canonical source repository
 *
 * @copyright Copyright (c) 2014 <NAME> (http://about.me/jeremykendall)
 * @license   http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License
 */
namespace Pdp;

use Pdp\Exception\SeriouslyMalformedUrlException;
use Pdp\Uri\Url;
use Pdp\Uri\Url\Host;

/**
 * Parser.
 *
 * This class is reponsible for Public Suffix List based url parsing
 */
class Parser
{
    /**
     * @var string RFC 3986 compliant scheme regex pattern
     *
     * @see https://tools.ietf.org/html/rfc3986#section-3.1
     */
    const SCHEME_PATTERN = '#^([a-zA-Z][a-zA-Z0-9+\-.]*)://#';

    /**
     * @var string IP address regex pattern
     */
    const IP_ADDRESS_PATTERN = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';

    /**
     * @var PublicSuffixList Public Suffix List
     */
    protected $publicSuffixList;

    /**
     * @var bool Whether or not a host part has been normalized
     */
    protected $isNormalized = false;

    /**
     * Public constructor.
     *
     * @codeCoverageIgnore
     *
     * @param PublicSuffixList $publicSuffixList Instance of PublicSuffixList
     */
    public function __construct(PublicSuffixList $publicSuffixList)
    {
        $this->publicSuffixList = $publicSuffixList;
    }

    /**
     * Parses url.
     *
     * @param string $url Url to parse
     *
     * @return Url Object representation of url
     */
    public function parseUrl($url)
    {
        $rawUrl = $url;
        $elem = array(
            'scheme' => null,
            'user' => null,
            'pass' => <PASSWORD>,
            'host' => null,
            'port' => null,
            'path' => null,
            'query' => null,
            'fragment' => null,
        );

        if (preg_match(self::SCHEME_PATTERN, $url) === 0) {
            // Wacky scheme required to overcome parse_url behavior in PHP lt 5.4.7
            // See https://github.com/jeremykendall/php-domain-parser/issues/49
            $url = 'php-lt-5.4.7-hack://' . preg_replace('#^//#', '', $url, 1);
        }

        $parts = pdp_parse_url($url);

        if ($parts === false) {
            throw new SeriouslyMalformedUrlException($rawUrl);
        }

        if ($parts['scheme'] === 'php-lt-5.4.7-hack') {
            // Remove wacky scheme required to overcome parse_url behavior in PHP lt 5.4.7
            // See https://github.com/jeremykendall/php-domain-parser/issues/49
            $parts['scheme'] = null;
        }

        $elem = (array) $parts + $elem;

        $host = $this->parseHost($parts['host']);

        return new Url(
            $elem['scheme'],
            $elem['user'],
            $elem['pass'],
            $host,
            $elem['port'],
            $elem['path'],
            $elem['query'],
            $elem['fragment']
        );
    }

    /**
     * Parses host part of url.
     *
     * @param string $host Host part of url
     *
     * @return Host Object representation of host portion of url
     */
    public function parseHost($host)
    {
        $host = mb_strtolower($host, 'UTF-8');

        return new Host(
            $this->getSubdomain($host),
            $this->getRegistrableDomain($host),
            $this->getPublicSuffix($host),
            $host
        );
    }

    /**
     * Get the raw public suffix based on the cached public suffix list file.
     * Return false if the provided suffix is not included in the PSL.
     *
     * @param string $host The host to process
     *
     * @return string|false The suffix or false if suffix not included in the PSL
     */
    protected function getRawPublicSuffix($host)
    {
        $host = $this->normalize($host);

        $parts = array_reverse(explode('.', $host));
{'fraction_non_alphanumeric': 0.14471352628470172, 'fraction_numerical': 0.01618428824571766, 'mean_word_length': 1.9915194346289753, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 36, 'https://': 6, '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': '2532767', 'n_tokens_mistral': 2727, 'n_tokens_neox': 2637, 'n_words': 640}