Skip to content

Commit

Permalink
Merge pull request #869 from andrew-demb/phpdoc-types-and-minor-impro…
Browse files Browse the repository at this point in the history
…vements

Phpdoc types and minor improvements
  • Loading branch information
ramunasd committed Feb 18, 2021
2 parents 46d8632 + 728a083 commit 5a9a16f
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 92 deletions.
20 changes: 10 additions & 10 deletions PhpAmqpLib/Channel/AbstractChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static function getProtocolVersion()
}

/**
* @return string
* @return int
*/
public function getChannelId()
{
Expand Down Expand Up @@ -213,7 +213,7 @@ public function dispatch($method_sig, $args, $amqpMessage)

$this->dispatch_reader->reuse($args);

if ($amqpMessage == null) {
if ($amqpMessage === null) {
return call_user_func(array($this, $amqp_method), $this->dispatch_reader);
}

Expand Down Expand Up @@ -273,8 +273,8 @@ public function wait_content()

$this->wait_content_reader->reuse(mb_substr($payload, 0, 12, 'ASCII'));

$class_id = $this->wait_content_reader->read_short();
$weight = $this->wait_content_reader->read_short();
$this->wait_content_reader->read_short();
$this->wait_content_reader->read_short();

//hack to avoid creating new instances of AMQPReader;
$this->msg_property_reader->reuse(mb_substr($payload, 12, mb_strlen($payload, 'ASCII') - 12, 'ASCII'));
Expand Down Expand Up @@ -323,7 +323,7 @@ protected function createMessage($propertyReader, $contentReader)
* Unexpected methods are queued up for later calls to this PHP
* method.
*
* @param array $allowed_methods
* @param array|null $allowed_methods
* @param bool $non_blocking
* @param int|float|null $timeout
* @throws \PhpAmqpLib\Exception\AMQPOutOfBoundsException
Expand Down Expand Up @@ -385,7 +385,7 @@ public function wait($allowed_methods = null, $non_blocking = false, $timeout =
}

/**
* @param array $allowed_methods
* @param array|null $allowed_methods
* @return array
*/
protected function process_deferred_methods($allowed_methods)
Expand All @@ -398,7 +398,7 @@ protected function process_deferred_methods($allowed_methods)

$method_sig = $qm[0];

if ($allowed_methods == null || in_array($method_sig, $allowed_methods)) {
if ($allowed_methods === null || in_array($method_sig, $allowed_methods, true)) {
unset($this->method_queue[$qk]);
$dispatch = true;
$queued_method = $qm;
Expand Down Expand Up @@ -454,7 +454,7 @@ protected function validate_body_frame($frame_type)
*/
protected function validate_frame($frameType, $expectedType, $expectedMessage)
{
if ($frameType != $expectedType) {
if ($frameType !== $expectedType) {
throw new AMQPInvalidFrameException(sprintf(
'Expecting %s, received frame type %s (%s)',
$expectedMessage,
Expand Down Expand Up @@ -502,8 +502,8 @@ protected function extract_args($payload)
*/
protected function should_dispatch_method($allowed_methods, $method_sig)
{
return $allowed_methods == null
|| in_array($method_sig, $allowed_methods)
return $allowed_methods === null
|| in_array($method_sig, $allowed_methods, true)
|| $this->constants->isCloseMethod($method_sig);
}

Expand Down
2 changes: 1 addition & 1 deletion PhpAmqpLib/Connection/AMQPStreamConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AMQPStreamConnection extends AbstractConnection
* @param string $locale
* @param float $connection_timeout
* @param float $read_write_timeout
* @param null $context
* @param resource|array|null $context
* @param bool $keepalive
* @param int $heartbeat
* @param float $channel_rpc_timeout
Expand Down
32 changes: 16 additions & 16 deletions PhpAmqpLib/Connection/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public function send_content($channel, $class_id, $weight, $body_size, $packed_p
* @param int $body_size
* @param string $packed_properties
* @param string $body
* @param AMQPWriter $pkt
* @param AMQPWriter|null $pkt
* @return AMQPWriter
*/
public function prepare_content($channel, $class_id, $weight, $body_size, $packed_properties, $body, $pkt)
Expand Down Expand Up @@ -524,7 +524,7 @@ protected function send_channel_method_frame($channel, $method_sig, $args = '',
* @param string $channel
* @param array $method_sig
* @param AMQPWriter|string $args
* @param AMQPWriter $pkt
* @param AMQPWriter|null $pkt
* @return AMQPWriter
*/
protected function prepare_channel_method_frame($channel, $method_sig, $args = '', $pkt = null)
Expand Down Expand Up @@ -605,7 +605,7 @@ protected function wait_frame($timeout = 0)

$this->input->setTimeout($currentTimeout);

if ($ch != 0xCE) {
if ($ch !== 0xCE) {
throw new AMQPInvalidFrameException(sprintf(
'Framing error, unexpected byte: %x',
$ch
Expand All @@ -618,7 +618,7 @@ protected function wait_frame($timeout = 0)
/**
* Waits for a frame from the server destined for a particular channel.
*
* @param string $channel_id
* @param int $channel_id
* @param int|float|null $timeout
* @return array
*/
Expand All @@ -635,9 +635,9 @@ protected function wait_channel($channel_id, $timeout = 0)
$this->heartbeat && $this->last_frame
&& microtime(true) - ($this->heartbeat * 2) > $this->last_frame
) {
$this->debug->debug_msg("missed server heartbeat (at threshold * 2)");
$this->debug->debug_msg('missed server heartbeat (at threshold * 2)');
$this->setIsConnected(false);
throw new AMQPHeartbeatMissedException("Missed server heartbeat");
throw new AMQPHeartbeatMissedException('Missed server heartbeat');
}

throw $e;
Expand All @@ -647,18 +647,18 @@ protected function wait_channel($channel_id, $timeout = 0)

if ($frame_channel === 0 && $frame_type === 8) {
// skip heartbeat frames and reduce the timeout by the time passed
$this->debug->debug_msg("received server heartbeat");
$this->debug->debug_msg('received server heartbeat');
if ($_timeout > 0) {
$_timeout -= $this->last_frame - $start;
if ($_timeout <= 0) {
// If timeout has been reached, throw the exception without calling wait_frame
throw new AMQPTimeoutException("Timeout waiting on channel");
throw new AMQPTimeoutException('Timeout waiting on channel');
}
}
continue;
}

if ($frame_channel == $channel_id) {
if ($frame_channel === $channel_id) {
return array($frame_type, $payload);
}

Expand All @@ -667,7 +667,7 @@ protected function wait_channel($channel_id, $timeout = 0)
// Make sure the channel still exists, it could have been
// closed by a previous Exception.
if (isset($this->channels[$frame_channel])) {
array_push($this->channels[$frame_channel]->frame_queue, array($frame_type, $payload));
$this->channels[$frame_channel]->frame_queue[] = [$frame_type, $payload];
}

// If we just queued up a method for channel 0 (the Connection
Expand All @@ -683,7 +683,7 @@ protected function wait_channel($channel_id, $timeout = 0)
* Fetches a channel object identified by the numeric channel_id, or
* create that object if it doesn't already exist.
*
* @param int $channel_id
* @param int|null $channel_id
* @return AMQPChannel
*/
public function channel($channel_id = null)
Expand All @@ -692,7 +692,7 @@ public function channel($channel_id = null)
return $this->channels[$channel_id];
}

$channel_id = $channel_id ? $channel_id : $this->get_free_channel_id();
$channel_id = $channel_id ?: $this->get_free_channel_id();
$ch = new AMQPChannel($this, $channel_id, true, $this->channel_rpc_timeout);
$this->channels[$channel_id] = $ch;

Expand All @@ -709,12 +709,12 @@ public function channel($channel_id = null)
*/
public function close($reply_code = 0, $reply_text = '', $method_sig = array(0, 0))
{
$result = null;
$this->io->disableHeartbeat();
if (empty($this->protocolWriter) || !$this->isConnected()) {
return $result;
return null;
}

$result = null;
try {
$this->closeChannels();
list($class_id, $method_id, $args) = $this->protocolWriter->connectionClose(
Expand Down Expand Up @@ -1126,12 +1126,12 @@ public static function create_connection($hosts, $options = array())
}

foreach ($hosts as $hostdef) {
AbstractConnection::validate_host($hostdef);
self::validate_host($hostdef);
$host = $hostdef['host'];
$port = $hostdef['port'];
$user = $hostdef['user'];
$password = $hostdef['password'];
$vhost = isset($hostdef['vhost']) ? $hostdef['vhost'] : "/";
$vhost = isset($hostdef['vhost']) ? $hostdef['vhost'] : '/';
try {
$conn = static::try_create_connection($host, $port, $user, $password, $vhost, $options);
return $conn;
Expand Down
6 changes: 3 additions & 3 deletions PhpAmqpLib/Helper/DebugHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function debug_msg($msg)
}

/**
* @param array $allowed_methods
* @param array|null $allowed_methods
*/
public function debug_allowed_methods($allowed_methods)
{
Expand All @@ -61,7 +61,7 @@ public function debug_allowed_methods($allowed_methods)
}

/**
* @param string $method_sig
* @param string|array $method_sig
*/
public function debug_method_signature1($method_sig)
{
Expand All @@ -70,7 +70,7 @@ public function debug_method_signature1($method_sig)

/**
* @param string $msg
* @param string $method_sig
* @param string|array $method_sig
*/
public function debug_method_signature($msg, $method_sig)
{
Expand Down
2 changes: 1 addition & 1 deletion PhpAmqpLib/Message/AMQPMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ public function serialize_properties()
}

$flag_bits |= (1 << $shift);
if ($prototype != 'bit') {
if ($prototype !== 'bit') {
$raw_bytes->{'write_' . $prototype}($val);
}

Expand Down
8 changes: 4 additions & 4 deletions PhpAmqpLib/Wire/AMQPAbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ abstract public function getType();

/**
* @param mixed $val
* @param int $type
* @param int|null $type
* @param string $key
*/
final protected function setValue($val, $type = null, $key = null)
{
if ($val instanceof self) {
if ($type && ($type != $val->getType())) {
if ($type && ($type !== $val->getType())) {
throw new Exception\AMQPInvalidArgumentException(
sprintf(
'Attempted to add instance of %s representing type [%s] as mismatching type [%s]',
Expand Down Expand Up @@ -340,7 +340,7 @@ protected function encodeInt($val)
*/
protected function encodeFloat($val)
{
return static::encodeString((string) $val);
return $this->encodeString((string) $val);
}

/**
Expand Down Expand Up @@ -384,7 +384,7 @@ final public static function getProtocol()
*/
final public static function isProtocol($proto)
{
return self::getProtocol() == $proto;
return self::getProtocol() === $proto;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion PhpAmqpLib/Wire/AMQPArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final public function getType()

/**
* @param mixed $val
* @param null $type
* @param int|null $type
* @return $this
*/
public function push($val, $type = null)
Expand Down
1 change: 0 additions & 1 deletion PhpAmqpLib/Wire/AMQPReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PhpAmqpLib\Exception\AMQPDataReadException;
use PhpAmqpLib\Exception\AMQPInvalidArgumentException;
use PhpAmqpLib\Exception\AMQPIOWaitException;
use PhpAmqpLib\Exception\AMQPNoDataException;
use PhpAmqpLib\Exception\AMQPOutOfBoundsException;
use PhpAmqpLib\Exception\AMQPTimeoutException;
Expand Down
2 changes: 1 addition & 1 deletion PhpAmqpLib/Wire/AMQPWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public function write_table($d)
{
$typeIsSym = !($d instanceof AMQPTable); //purely for back-compat purposes

$table_data = new AMQPWriter();
$table_data = new self();
foreach ($d as $k => $va) {
list($ftype, $v) = $va;
$table_data->write_shortstr($k);
Expand Down
2 changes: 1 addition & 1 deletion PhpAmqpLib/Wire/IO/SocketIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function read($len)
$data .= $buffer;
}

if (mb_strlen($data, 'ASCII') != $len) {
if (mb_strlen($data, 'ASCII') !== $len) {
throw new AMQPIOException(sprintf(
'Error reading data. Received %s instead of expected %s bytes',
mb_strlen($data, 'ASCII'),
Expand Down
10 changes: 5 additions & 5 deletions PhpAmqpLib/Wire/IO/StreamIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class StreamIO extends AbstractIO
* @param int $port
* @param float $connection_timeout
* @param float $read_write_timeout
* @param null $context
* @param resource|array|null $context
* @param bool $keepalive
* @param int $heartbeat
* @param string|null $ssl_protocol
Expand All @@ -50,6 +50,10 @@ public function __construct(
}
*/

if (!is_resource($context) || get_resource_type($context) !== 'stream-context') {
$context = stream_context_create();
}

$this->protocol = 'tcp';
$this->host = $host;
$this->port = $port;
Expand All @@ -62,10 +66,6 @@ public function __construct(
$this->initial_heartbeat = $heartbeat;
$this->canDispatchPcntlSignal = $this->isPcntlSignalEnabled();

if (!is_resource($this->context) || get_resource_type($this->context) !== 'stream-context') {
$this->context = stream_context_create();
}

// tcp_nodelay was added in 7.1.0
if (PHP_VERSION_ID >= 70100) {
stream_context_set_option($this->context, 'socket', 'tcp_nodelay', true);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/file_publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//suboptimal function to generate random content
function generate_random_content($bytes)
{
$handle = @fopen("/dev/urandom", "rb");
$handle = @fopen('/dev/urandom', 'rb');

$buffer = '';
if ($handle) {
Expand Down
4 changes: 2 additions & 2 deletions benchmark/socket_tmp_produce.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
for ($i = 0; $i < $max; $i++) {

$ch = $conn->channel();
list($queue,) = $ch->queue_declare("", false, false, true, true);
list($queue,) = $ch->queue_declare('', false, false, true, true);
$msg = new AMQPMessage($msg_body);
$ch->basic_publish($msg, "", $queue);
$ch->basic_publish($msg, '', $queue);
$ch->close();

}
Expand Down
4 changes: 2 additions & 2 deletions benchmark/stream_tmp_produce.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
for ($i = 0; $i < $max; $i++) {

$ch = $conn->channel();
list($queue,) = $ch->queue_declare("", false, false, true, true);
list($queue,) = $ch->queue_declare('', false, false, true, true);
$msg = new AMQPMessage($msg_body);
$ch->basic_publish($msg, "", $queue);
$ch->basic_publish($msg, '', $queue);
$ch->close();
}

Expand Down
2 changes: 1 addition & 1 deletion demo/amqp_message_headers_snd.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

$data = implode(' ', array_slice($argv, 2));
if (empty($data)) {
$data = "Hello World!";
$data = 'Hello World!';
}

$message = new AMQPMessage($data);
Expand Down

0 comments on commit 5a9a16f

Please sign in to comment.