From 9dba42ab2a3990de29e18cc62b0a8270aceb74e3 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 19 Apr 2021 15:53:08 +0200 Subject: [PATCH] fix new psalm issues --- src/AnonymousAuth.php | 6 +++++- src/KerberosAuth.php | 10 ++++++++-- src/Native/NativeState.php | 11 ++++++----- src/Native/NativeStream.php | 8 ++++++-- src/Wrapped/Server.php | 2 +- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/AnonymousAuth.php b/src/AnonymousAuth.php index 0f35240..302a249 100644 --- a/src/AnonymousAuth.php +++ b/src/AnonymousAuth.php @@ -21,6 +21,8 @@ namespace Icewind\SMB; +use Icewind\SMB\Exception\Exception; + class AnonymousAuth implements IAuth { public function getUsername(): ?string { return null; @@ -39,6 +41,8 @@ public function getExtraCommandLineArguments(): string { } public function setExtraSmbClientOptions($smbClientState): void { - smbclient_option_set($smbClientState, SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN, true); + if (smbclient_option_set($smbClientState, SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN, true) === false) { + throw new Exception("Failed to set smbclient options for anonymous auth"); + } } } diff --git a/src/KerberosAuth.php b/src/KerberosAuth.php index 7cd9258..68fb74f 100644 --- a/src/KerberosAuth.php +++ b/src/KerberosAuth.php @@ -21,6 +21,8 @@ namespace Icewind\SMB; +use Icewind\SMB\Exception\Exception; + /** * Use existing kerberos ticket to authenticate */ @@ -42,7 +44,11 @@ public function getExtraCommandLineArguments(): string { } public function setExtraSmbClientOptions($smbClientState): void { - smbclient_option_set($smbClientState, SMBCLIENT_OPT_USE_KERBEROS, true); - smbclient_option_set($smbClientState, SMBCLIENT_OPT_FALLBACK_AFTER_KERBEROS, false); + $success = (bool)smbclient_option_set($smbClientState, SMBCLIENT_OPT_USE_KERBEROS, true); + $success = $success && smbclient_option_set($smbClientState, SMBCLIENT_OPT_FALLBACK_AFTER_KERBEROS, false); + + if (!$success) { + throw new Exception("Failed to set smbclient options for kerberos auth"); + } } } diff --git a/src/Native/NativeState.php b/src/Native/NativeState.php index 10ba6ce..e1a13ce 100644 --- a/src/Native/NativeState.php +++ b/src/Native/NativeState.php @@ -97,14 +97,13 @@ public function init(IAuth $auth, IOptions $options) { /** @var resource $state */ $state = smbclient_state_new(); $this->state = $state; + /** @psalm-suppress UnusedFunctionCall */ smbclient_option_set($this->state, SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN, false); + /** @psalm-suppress UnusedFunctionCall */ smbclient_option_set($this->state, SMBCLIENT_OPT_TIMEOUT, $options->getTimeout() * 1000); if (function_exists('smbclient_client_protocols')) { - $maxProtocol = $options->getMaxProtocol(); - $minProtocol = $options->getMinProtocol(); - - smbclient_client_protocols($this->state, $minProtocol, $maxProtocol); + smbclient_client_protocols($this->state, $options->getMinProtocol(), $options->getMaxProtocol()); } $auth->setExtraSmbClientOptions($this->state); @@ -357,7 +356,9 @@ public function setxattr(string $uri, string $key, string $value, int $flags = 0 public function __destruct() { if ($this->connected) { - smbclient_state_free($this->state); + if (smbclient_state_free($this->state) === false) { + throw new Exception("Failed to free smb state"); + } } } } diff --git a/src/Native/NativeStream.php b/src/Native/NativeStream.php index 216c27f..624ef82 100644 --- a/src/Native/NativeStream.php +++ b/src/Native/NativeStream.php @@ -52,7 +52,9 @@ abstract class NativeStream implements File { * @return resource */ protected static function wrapClass(NativeState $state, $smbStream, string $mode, string $url, string $class) { - stream_wrapper_register('nativesmb', $class); + if (stream_wrapper_register('nativesmb', $class) === false) { + throw new Exception("Failed to register stream wrapper"); + } $context = stream_context_create([ 'nativesmb' => [ 'state' => $state, @@ -61,7 +63,9 @@ protected static function wrapClass(NativeState $state, $smbStream, string $mode ] ]); $fh = fopen('nativesmb://', $mode, false, $context); - stream_wrapper_unregister('nativesmb'); + if (stream_wrapper_unregister('nativesmb') === false) { + throw new Exception("Failed to unregister stream wrapper"); + } return $fh; } diff --git a/src/Wrapped/Server.php b/src/Wrapped/Server.php index 60cc927..dd8513e 100644 --- a/src/Wrapped/Server.php +++ b/src/Wrapped/Server.php @@ -88,7 +88,7 @@ public function listShares(): array { $shareNames = $parser->parseListShares($output); $shares = []; - foreach ($shareNames as $name => $description) { + foreach ($shareNames as $name => $_description) { $shares[] = $this->getShare($name); } return $shares;