Skip to content

Fix some issues identified by phpstan#123

Open
jay-knight wants to merge 1 commit into
senaranya:masterfrom
jay-knight:fix-phpstan-issues
Open

Fix some issues identified by phpstan#123
jay-knight wants to merge 1 commit into
senaranya:masterfrom
jay-knight:fix-phpstan-issues

Conversation

@jay-knight
Copy link
Copy Markdown
Contributor

This fixes most issues identified by PHPstan up to level 5. I'll comment on each change to describe the issue.

Comment thread src/HL7/Segments/GT1.php
namespace Aranyasen\HL7\Segments;

use Aranyasen\HL7\Segment;
use InvalidArgumentException;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception is thrown in this class, but was not used.

Comment thread src/HL7/Segments/MSH.php
Comment on lines -82 to +88
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)) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSH::setTriggerEvent() calls $this->setField($position, $value), and $value is an array. This changes this function to take the same types as Segment::setField(), and ensures that index 1 or 2 must be a string. It could be better to throw an exception instead of returning false if a non-string value is given for MSH.1 or MSH.2.

Comment thread tests/HL7Test.php
use Aranyasen\HL7;
use Aranyasen\HL7\Segments\PID;
use DMS\PHPUnitExtensions\ArraySubset\Assert;
use Exception;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docblock for empty_subfields_can_be_retained_if_needed() says it throws Exception. This uses that class to avoid ambiguity.

Comment thread tests/Hl7ListenerTrait.php Outdated
Comment on lines +70 to +73
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");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In both of these $ret is a bool, and socket_strerror() expects an int. So, this calls socket_last_error($socket) instead.

Comment on lines -70 to +84
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);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block runs if $clientSocket is false, so this is calling socket_close(false). I changed it to close the "parent" socket, since the code exits here.

Comment on lines -83 to -85
if ($clientSocket === false) {
continue;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above block exits if $clientSocket is false, so at this point $clientSocket will always be a Socket object, and so this block will never run.

@senaranya
Copy link
Copy Markdown
Owner

Thanks for these changes. However, some of these are part of 4.x roadmap, so I'll need to check if there could be any conflicts. Could be a while as I'll be traveling, but will try as soon as I can

@jay-knight
Copy link
Copy Markdown
Contributor Author

4.x roadmap

Is that published/available somewhere? I'd be interested to see what's in store.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants