<commit_before><?php

/**
 * Copyright 2017 The WildPHP Team
 *
 * You should have received a copy of the MIT license with the project.
 * See the LICENSE file for more information.
 */

namespace WildPHP\Core\Connection;


class UserPrefix
{
	/**
	 * @var string
	 */
	public static $regex = '/^(?<server>[^!@]+)?$|^(?<nick>[^!@]+) (?:!(?<user>[^@]+))? (?:@(?<host>.+))?$/x';

	/**
	 * @var string
	 */
	protected $nickname = '';

	/**
	 * @var string
	 */
	protected $username = '';

	/**
	 * @var string
	 */
	protected $hostname = '';

	/**
	 * UserPrefix constructor.
	 *
	 * @param string $nickname
	 * @param string $username
	 * @param string $hostname
	 */
	public function __construct(string $nickname = '', string $username = '', string $hostname = '')
	{
		$this->setNickname($nickname);
		$this->setUsername($username);
		$this->setHostname($hostname);
	}

	/**
	 * @return string
	 */
	public function getNickname(): string
	{
		return $this->nickname;
	}

	/**
	 * @param string $nickname
	 */
	public function setNickname(string $nickname)
	{
		$this->nickname = $nickname;
	}

	/**
	 * @return string
	 */
	public function getUsername(): string
	{
		return $this->username;
	}

	/**
	 * @param string $username
	 */
	public function setUsername(string $username)
	{
		$this->username = $username;
	}

	/**
	 * @return string
	 */
	public function getHostname(): string
	{
		return $this->hostname;
	}

	/**
	 * @param string $hostname
	 */
	public function setHostname(string $hostname)
	{
		$this->hostname = $hostname;
	}

	/**
	 * @param string $prefix
	 *
	 * @return \self
	 */
	public static function fromString(string $prefix): self
	{
		if (preg_match(self::$regex, $prefix, $matches) === false)
			throw new \InvalidArgumentException('Got invalid prefix');

		$nickname = $matches['nick'] ?? '';
		$username = $matches['user'] ?? '';
		$hostname = $matches['host'] ?? ($matches['server'] ?? '');

		return new self($nickname, $username, $hostname);
	}

	/**
	 * @param IncomingIrcMessage $incomingIrcMessage
	 *
	 * @return \self
	 */
	public static function fromIncomingIrcMessage(IncomingIrcMessage $incomingIrcMessage): self
	{
		return self::fromString($incomingIrcMessage->getPrefix());
	}
}<commit_msg>UserPrefix: Create empty \self when incomingIrcMessage->getPrefix is empty<commit_after><?php

/**
 * Copyright 2017 The WildPHP Team
 *
 * You should have received a copy of the MIT license with the project.
 * See the LICENSE file for more information.
 */

namespace WildPHP\Core\Connection;


class UserPrefix
{
	/**
	 * @var string
	 */
	public static $regex = '/^(?<server>[^!@]+)?$|^(?<nick>[^!@]+) (?:!(?<user>[^@]+))? (?:@(?<host>.+))?$/x';

	/**
	 * @var string
	 */
	protected $nickname = '';

	/**
	 * @var string
	 */
	protected $username = '';

	/**
	 * @var string
	 */
	protected $hostname = '';

	/**
	 * UserPrefix constructor.
	 *
	 * @param string $nickname
	 * @param string $username
	 * @param string $hostname
	 */
	public function __construct(string $nickname = '', string $username = '', string $hostname = '')
	{
		$this->setNickname($nickname);
		$this->setUsername($username);
		$this->setHostname($hostname);
	}

	/**
	 * @return string
	 */
	public function getNickname(): string
	{
		return $this->nickname;
	}

	/**
	 * @param string $nickname
	 */
	public function setNickname(string $nickname)
	{
		$this->nickname = $nickname;
	}

	/**
	 * @return string
	 */
	public function getUsername(): string
	{
		return $this->username;
	}

	/**
	 * @param string $username
	 */
	public function setUsername(string $username)
	{
		$this->username = $username;
	}

	/**
	 * @return string
	 */
	public function getHostname(): string
	{
		return $this->hostname;
	}

	/**
	 * @param string $hostname
	 */
	public function setHostname(string $hostname)
	{
		$this->hostname = $hostname;
	}

	/**
	 * @param string $prefix
	 *
	 * @return \self
	 */
	public static function fromString(string $prefix): self
	{
		if (preg_match(self::$regex, $prefix, $matches) === false)
			throw new \InvalidArgumentException('Got invalid prefix');

		$nickname = $matches['nick'] ?? '';
		$username = $matches['user'] ?? '';
		$hostname = $matches['host'] ?? ($matches['server'] ?? '');

		return new self($nickname, $username, $hostname);
	}

	/**
	 * @param IncomingIrcMessage $incomingIrcMessage
	 *
	 * @return \self
	 */
	public static function fromIncomingIrcMessage(IncomingIrcMessage $incomingIrcMessage): self
	{
	    if (!empty($incomingIrcMessage->getPrefix()))
		    return self::fromString($incomingIrcMessage->getPrefix());

	    return new self();
	}
}
{'fraction_non_alphanumeric': 0.18073334779778694, 'fraction_numerical': 0.0017357344326318074, 'mean_word_length': 3.6053946053946055, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, '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': '5603821', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1482, 'n_words': 350}