From 217834bc2baa6ee62904836b889716eb2f71c3b6 Mon Sep 17 00:00:00 2001 From: Jay Knight Date: Sat, 28 Sep 2024 21:55:11 -0500 Subject: [PATCH] Fix some issues identified by phpstan --- src/HL7/Segments/GT1.php | 1 + src/HL7/Segments/MSH.php | 6 +++--- tests/HL7Test.php | 1 + tests/Hl7ListenerTrait.php | 15 ++++++++------- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/HL7/Segments/GT1.php b/src/HL7/Segments/GT1.php index 5eb06e2..a78ac40 100644 --- a/src/HL7/Segments/GT1.php +++ b/src/HL7/Segments/GT1.php @@ -5,6 +5,7 @@ namespace Aranyasen\HL7\Segments; use Aranyasen\HL7\Segment; +use InvalidArgumentException; /** * GT1 segment class diff --git a/src/HL7/Segments/MSH.php b/src/HL7/Segments/MSH.php index b196096..e5e75fb 100644 --- a/src/HL7/Segments/MSH.php +++ b/src/HL7/Segments/MSH.php @@ -79,13 +79,13 @@ public function __construct(array $fields = null, array $hl7Globals = null) * @param int $index Index of field * @param string $value */ - public function setField(int $index, $value = ''): bool + public function setField(int $index, string|int|array|null $value = ''): bool { - if (($index === 1) && strlen($value) !== 1) { + if (($index === 1) && (!is_string($value) || strlen($value) !== 1)) { return false; } - if (($index === 2) && strlen($value) !== 4) { + if (($index === 2) && (!is_string($value) || strlen($value) !== 4)) { return false; } diff --git a/tests/HL7Test.php b/tests/HL7Test.php index d8718ee..230e8ed 100644 --- a/tests/HL7Test.php +++ b/tests/HL7Test.php @@ -8,6 +8,7 @@ use Aranyasen\HL7; use Aranyasen\HL7\Segments\PID; use DMS\PHPUnitExtensions\ArraySubset\Assert; +use Exception; class HL7Test extends TestCase { diff --git a/tests/Hl7ListenerTrait.php b/tests/Hl7ListenerTrait.php index 3d382ad..7d3f6b6 100644 --- a/tests/Hl7ListenerTrait.php +++ b/tests/Hl7ListenerTrait.php @@ -67,22 +67,23 @@ public function createTcpServer(int $port, int $totalClientsToConnect): void } if (($ret = socket_bind($socket, "localhost", $port)) === false) { - throw new RuntimeException('socket_bind() failed: reason: ' . socket_strerror($ret) . "\n"); + throw new RuntimeException( + 'socket_bind() failed: reason: ' . socket_strerror(socket_last_error($socket)) . "\n" + ); } if (($ret = socket_listen($socket, 5)) === false) { - throw new RuntimeException('socket_listen() failed: reason: ' . socket_strerror($ret) . "\n"); + throw new RuntimeException( + 'socket_listen() failed: reason: ' . socket_strerror(socket_last_error($socket)) . "\n" + ); } $clientCount = 0; while (true) { // Loop over each client if (($clientSocket = socket_accept($socket)) === false) { - echo 'socket_accept() failed: reason: ' . socket_strerror(socket_last_error()) . "\n"; - socket_close($clientSocket); + echo 'socket_accept() failed: reason: ' . socket_strerror(socket_last_error($socket)) . "\n"; + socket_close($socket); exit(); } - if ($clientSocket === false) { - continue; - } $clientCount++; $clientName = 'Unknown';