Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception array_merge(): Argument #1 must be of type array in NonBlockingClient.php:183 #137

Closed
niniob opened this issue May 5, 2023 · 6 comments
Labels

Comments

@niniob
Copy link

niniob commented May 5, 2023

Using WriteRegistersBuilder to write registers leads to following exception:
Fatal error: Uncaught TypeError: array_merge(): Argument #1 must be of type array, ModbusTcpClient\Packet\ModbusFunction\WriteMultipleRegistersResponse given in /php/modbus-test-01/vendor/aldas/modbus-tcp-client/src/Network/NonBlockingClient.php:183

Issue seems to be line 183 in NonBlockingClient.php:
$data = array_merge(...$data);

I'm not sure why array_merge and the spread operator are used together. I assume it should be just
$data = array_merge($data);

At least this fixes the issue for me.

Example to reproduce the issue:

<?php
require __DIR__ . '/vendor/autoload.php';
use ModbusTcpClient\Composer\Write\WriteRegistersBuilder;
use ModbusTcpClient\Network\NonBlockingClient;

$request = WriteRegistersBuilder::newWriteMultipleRegisters()
				->allFromArray([['uri' => 'tcp:https://192.168.100.10:5555', 'type' => 'uint16', 'value' => 123, 'address' => 1024, 'unitId' => 1]])
				->build();

$response = (new NonBlockingClient())->sendRequests($request);
?>

Environment:

  • OS: MacOS 13.3.1
  • PHP version 8.1.5
  • Library version 3.1.1
@niniob niniob added the bug label May 5, 2023
@aldas
Copy link
Owner

aldas commented May 5, 2023

This is a bug with Write requests. Basically Composer Requests should extract values as array. Like ReadRegisterRequest does it here

public function parse(string $binaryData): array|ErrorResponse

but WriteRegisterRequest

public function parse(string $binaryData): ModbusResponse
just returns own instance instead of array - this is a bug / something that I forgot.

I will fix it.

@aldas
Copy link
Owner

aldas commented May 5, 2023

I have released version 3.2.0 that contains fix for this problem.

@niniob
Copy link
Author

niniob commented May 6, 2023

Excellent, the fix works fine with PHP 8, many thanks for the quick response. But have in addition an environment still running PHP 7.4.28 which cannot be upgraded to PHP 8. Therefore I need to use version 2.4.0 of modbus-tcp-client. Any chance to get the fix also downported to 2.4.0?

@aldas
Copy link
Owner

aldas commented May 6, 2023

@niniob
Copy link
Author

niniob commented May 12, 2023

@niniob I think you could create custom patch for PHP 7 and let composer to apply it.

Thanks, custom patch might be an option, but I found another workaround: Create NonBlockingClient with option 'flatRequestResponse' => false. This avoids $data = array_merge(...$data) being called.

$response = (new NonBlockingClient([ 'flatRequestResponse' => false ]))->sendRequests($request);

@aldas
Copy link
Owner

aldas commented May 16, 2023

aa yeah, this is also an option as write requests do not need responses.

@aldas aldas closed this as completed May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants