<commit_before><?php
/*
 * This file is part of the phpflo/phpflo package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace PhpFlo;

use PhpFlo\Exception\InvalidDefinitionException;
use PhpFlo\Exception\InvalidTypeException;
use PhpFlo\Exception\PortException;
use PhpFlo\Exception\SocketException;

/**
 * Class Port
 *
 * @package PhpFlo
 * @author <NAME> <<EMAIL>>
 */
final class Port extends AbstractPort
{
    /**
     * @param SocketInterface $socket
     * @throws InvalidDefinitionException
     */
    public function attach(SocketInterface $socket)
    {
        if ($this->socket) {
            throw new InvalidDefinitionException("{$this->name} socket already attached {$this->socket->getId()}");
        }

        $this->socket = $socket;
        $this->attachSocket($socket);
    }

    /**
     * @param mixed $data
     * @param SocketInterface $socket
     */
    public function onData($data, SocketInterface $socket)
    {
        $this->emit('data', [$data, $socket]);
    }

    /**
     * @param string $groupName
     * @param SocketInterface $socket
     */
    public function onBeginGroup($groupName, SocketInterface $socket)
    {
        $this->emit('beginGroup', [$groupName, $socket]);
    }

    /**
     * @param string $groupName
     * @param SocketInterface $socket
     */
    public function onEndGroup($groupName, SocketInterface $socket)
    {
        $this->emit('endGroup', [$groupName, $socket]);
    }

    /**
     * @throws SocketException
     */
    public function connect()
    {
        if (!$this->socket) {
            throw new SocketException("No socket available");
        }
        $this->socket->connect();
    }

    public function disconnect()
    {
        if (!$this->socket) {
            return;
        }

        $this->socket->disconnect();
    }

    /**
     * @return bool
     */
    public function isConnected()
    {
        if (!$this->socket) {
            return false;
        }

        return $this->socket->isConnected();
    }

    /**
     * Checks if port is attached.
     *
     * @return bool
     */
    public function isAttached()
    {
        if (!$this->socket) {
            return false;
        }

        return true;
    }

    /**
     * @param $groupName
     * @throws PortException
     */
    public function endGroup($groupName)
    {
        if (!$this->socket) {
            throw new PortException("This port is not connected");
        }

        $this->socket->endGroup($groupName);
    }

    /**
     * @param mixed $data
     * @return mixed|null
     * @throws InvalidTypeException
     */
    public function send($data)
    {
        if (!$this->socket) {
            throw new PortException("This port is not connected");
        }

        if (false == $this->hasType($data, 'send')) {
            throw new InvalidTypeException(
                'Port tries to send invalid data type "' . gettype($data) . '"!'
            );
        }

        if ($this->isConnected()) {
            return $this->socket->send($data);
        }<commit_msg>Fix regression in port type check on initialization<commit_after><?php
/*
 * This file is part of the phpflo/phpflo package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace PhpFlo;

use PhpFlo\Exception\InvalidDefinitionException;
use PhpFlo\Exception\InvalidTypeException;
use PhpFlo\Exception\PortException;
use PhpFlo\Exception\SocketException;

/**
 * Class Port
 *
 * @package PhpFlo
 * @author <NAME> <<EMAIL>>
 */
final class Port extends AbstractPort
{
    /**
     * @param SocketInterface $socket
     * @throws InvalidDefinitionException
     */
    public function attach(SocketInterface $socket)
    {
        if ($this->socket) {
            throw new InvalidDefinitionException("{$this->name} socket already attached {$this->socket->getId()}");
        }

        $this->socket = $socket;
        $this->attachSocket($socket);
    }

    /**
     * @param mixed $data
     * @param SocketInterface $socket
     */
    public function onData($data, SocketInterface $socket)
    {
        $this->emit('data', [$data, $socket]);
    }

    /**
     * @param string $groupName
     * @param SocketInterface $socket
     */
    public function onBeginGroup($groupName, SocketInterface $socket)
    {
        $this->emit('beginGroup', [$groupName, $socket]);
    }

    /**
     * @param string $groupName
     * @param SocketInterface $socket
     */
    public function onEndGroup($groupName, SocketInterface $socket)
    {
        $this->emit('endGroup', [$groupName, $socket]);
    }

    /**
     * @throws SocketException
     */
    public function connect()
    {
        if (!$this->socket) {
            throw new SocketException("No socket available");
        }
        $this->socket->connect();
    }

    public function disconnect()
    {
        if (!$this->socket) {
            return;
        }

        $this->socket->disconnect();
    }

    /**
     * @return bool
     */
    public function isConnected()
    {
        if (!$this->socket) {
            return false;
        }

        return $this->socket->isConnected();
    }

    /**
     * Checks if port is attached.
     *
     * @return bool
     */
    public function isAttached()
    {
        if (!$this->socket) {
            return false;
        }

        return true;
    }

    /**
     * @param $groupName
     * @throws PortException
     */
    public function endGroup($groupName)
    {
        if (!$this->socket) {
            throw new PortException("This port is not connected");
        }

        $this->socket->endGroup($groupName);
    }

    /**
     * @param mixed $data
     * @return mixed|null
     * @throws InvalidTypeException
     */
    public function send($data)
    {
        if (!$this->socket) {
            throw new PortException("This port is not connected");
        }

        if ($this->isConnected()) {
            return $this->socket->send($data);
        }
{'fraction_non_alphanumeric': 0.13316137352893762, 'fraction_numerical': 0.0, 'mean_word_length': 2.054652880354505, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 68, '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': '3699663', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1744, 'n_words': 428}