From 0491356bca3ad54bb1e4132ea3ef3652d5d37fbf Mon Sep 17 00:00:00 2001 From: cdosoftei Date: Thu, 16 May 2024 18:36:57 -0400 Subject: [PATCH] :safety_vest: Removed possibly falsy string comparisons --- etc/phpstan.neon | 2 +- etc/psalm.xml | 1 + src/Config/CliArguments.php | 10 +++++----- src/Config/EnvironmentVars.php | 6 +++--- src/Config/LegacyConfigFile.php | 6 +++--- src/HttpClient.php | 2 +- src/Rest/Controller/AuthenticatedTrait.php | 2 +- src/Rest/Inquiry/V0_1/BulkCall.php | 22 ++++++++++++++++------ src/Rest/Inquiry/V0_1/Call.php | 9 ++++++--- src/Rest/Inquiry/V0_1/GroupCall.php | 22 ++++++++++++++++------ src/Rest/Server.php | 2 +- 11 files changed, 54 insertions(+), 30 deletions(-) diff --git a/etc/phpstan.neon b/etc/phpstan.neon index ae9257c..f92eb41 100644 --- a/etc/phpstan.neon +++ b/etc/phpstan.neon @@ -1,6 +1,6 @@ includes: - ../vendor/phpstan/phpstan/conf/bleedingEdge.neon parameters: - checkGenericClassInNonGenericObjectType: false ignoreErrors: + - identifier: missingType.generics - '#Parameter \#1 \$onFulfilled of method React\\Promise\\PromiseInterface::then\(\) expects \(callable\(mixed\):#' diff --git a/etc/psalm.xml b/etc/psalm.xml index 01306b1..473214b 100644 --- a/etc/psalm.xml +++ b/etc/psalm.xml @@ -20,6 +20,7 @@ + diff --git a/src/Config/CliArguments.php b/src/Config/CliArguments.php index c5d8402..3530406 100644 --- a/src/Config/CliArguments.php +++ b/src/Config/CliArguments.php @@ -67,7 +67,7 @@ public function resolve(AbstractSet $config): void $configFile = $args['c']; } - if ($configFile) { + if (is_string($configFile)) { switch (pathinfo($configFile, PATHINFO_EXTENSION)) { case 'json': case 'yml': @@ -168,7 +168,7 @@ function (string $value): bool { if (isset($args['rest-bind-address']) && is_string($args['rest-bind-address'])) { $err = Set::parseSocketAddr($args['rest-bind-address'], $ip, $port); - if ($err) { + if (is_string($err)) { fwrite(STDERR, 'Malformed --rest-bind-address argument: ip:port required' . PHP_EOL); fwrite(STDERR, $err . PHP_EOL); } else { @@ -245,7 +245,7 @@ function (string $value): bool { if (isset($args['outbound-bind-address']) && is_string($args['outbound-bind-address'])) { $err = Set::parseSocketAddr($args['outbound-bind-address'], $ip, $port); - if ($err) { + if (is_string($err)) { fwrite(STDERR, 'Malformed --outbound-bind-address argument: ip:port required' . PHP_EOL); fwrite(STDERR, $err . PHP_EOL); } else { @@ -263,7 +263,7 @@ function (string $value): bool { } else { $err = Set::parseSocketAddr($args['outbound-advertised-address'], $ip, $port); - if ($err) { + if (is_string($err)) { fwrite(STDERR, 'Malformed --outbound-advertised-address argument: ip:port or `inbound_socket_address` required' . PHP_EOL); fwrite(STDERR, $err . PHP_EOL); } else { @@ -313,7 +313,7 @@ function (string $value): bool { protected function help(): never { - $cmd = !isset($_SERVER['argv']) || !is_array($_SERVER['argv']) || empty($_SERVER['argv'][0]) + $cmd = !isset($_SERVER['argv']) || !is_array($_SERVER['argv']) || !is_string($_SERVER['argv'][0]) || !strlen($_SERVER['argv'][0]) ? './bin/ficore' : $_SERVER['argv'][0]; diff --git a/src/Config/EnvironmentVars.php b/src/Config/EnvironmentVars.php index 40dd7e2..a99101a 100644 --- a/src/Config/EnvironmentVars.php +++ b/src/Config/EnvironmentVars.php @@ -96,7 +96,7 @@ function (string $value): bool { if (isset($env[self::PREFIX . 'REST_BIND_ADDRESS'])) { $err = Set::parseSocketAddr($env[self::PREFIX . 'REST_BIND_ADDRESS'], $ip, $port); - if ($err) { + if (is_string($err)) { fwrite(STDERR, 'Malformed ' . self::PREFIX . 'REST_BIND_ADDRESS environment variable' . PHP_EOL); fwrite(STDERR, $err . PHP_EOL); } else { @@ -173,7 +173,7 @@ function (string $value): bool { if (isset($env[self::PREFIX . 'OUTBOUND_BIND_ADDRESS'])) { $err = Set::parseSocketAddr($env[self::PREFIX . 'OUTBOUND_BIND_ADDRESS'], $ip, $port); - if ($err) { + if (is_string($err)) { fwrite(STDERR, 'Malformed ' . self::PREFIX . 'OUTBOUND_BIND_ADDRESS environment variable' . PHP_EOL); fwrite(STDERR, $err . PHP_EOL); } else { @@ -191,7 +191,7 @@ function (string $value): bool { } else { $err = Set::parseSocketAddr($env[self::PREFIX . 'OUTBOUND_ADVERTISED_ADDRESS'], $ip, $port); - if ($err) { + if (is_string($err)) { fwrite(STDERR, 'Malformed ' . self::PREFIX . 'OUTBOUND_ADVERTISED_ADDRESS environment variable' . PHP_EOL); fwrite(STDERR, $err . PHP_EOL); } else { diff --git a/src/Config/LegacyConfigFile.php b/src/Config/LegacyConfigFile.php index c26bcde..ae71e67 100644 --- a/src/Config/LegacyConfigFile.php +++ b/src/Config/LegacyConfigFile.php @@ -102,7 +102,7 @@ function (string $value): bool { if (isset($legacy['rest_server']['HTTP_ADDRESS']) && is_string($legacy['rest_server']['HTTP_ADDRESS'])) { $err = Set::parseSocketAddr($legacy['rest_server']['HTTP_ADDRESS'], $ip, $port); - if ($err) { + if (is_string($err)) { fwrite(STDERR, 'Malformed HTTP_ADDRESS (rest_server) line in legacy configuration file' . PHP_EOL); fwrite(STDERR, $err . PHP_EOL); } else { @@ -117,7 +117,7 @@ function (string $value): bool { if (isset($legacy['rest_server']['FS_INBOUND_ADDRESS']) && is_string($legacy['rest_server']['FS_INBOUND_ADDRESS'])) { $err = Set::parseHostPort($legacy['rest_server']['FS_INBOUND_ADDRESS'], $host, $port); - if ($err) { + if (is_string($err)) { fwrite(STDERR, 'Malformed FS_INBOUND_ADDRESS (rest_server) line in legacy configuration file' . PHP_EOL); fwrite(STDERR, $err . PHP_EOL); } else { @@ -182,7 +182,7 @@ function (string $value): bool { if (isset($legacy['outbound_server']['FS_OUTBOUND_ADDRESS']) && is_string($legacy['outbound_server']['FS_OUTBOUND_ADDRESS'])) { $err = Set::parseSocketAddr($legacy['outbound_server']['FS_OUTBOUND_ADDRESS'], $ip, $port); - if ($err) { + if (is_string($err)) { fwrite(STDERR, 'Malformed FS_OUTBOUND_ADDRESS (outbound_server) line in legacy configuration file' . PHP_EOL); fwrite(STDERR, $err . PHP_EOL); } else { diff --git a/src/HttpClient.php b/src/HttpClient.php index 85d9172..1d6c1f2 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -63,7 +63,7 @@ public function makeRequest(string $url, string $method = 'POST', array $params throw new HttpClientException("Cannot send {$url}, cannot parse url"); } - if (!empty($parsed['query'])) { + if (isset($parsed['query']) && strlen($parsed['query'])) { parse_str($parsed['query'], $extra); $params = array_merge($params, $extra); } diff --git a/src/Rest/Controller/AuthenticatedTrait.php b/src/Rest/Controller/AuthenticatedTrait.php index f516034..d2e6fc4 100644 --- a/src/Rest/Controller/AuthenticatedTrait.php +++ b/src/Rest/Controller/AuthenticatedTrait.php @@ -101,7 +101,7 @@ protected function authenticate(ServerRequestInterface $request): PromiseInterfa $decoded = base64_decode($parts[1], true); - if (!$decoded) { + if ($decoded === false) { return reject(new AuthException('Cannot decode authentication payload')); } diff --git a/src/Rest/Inquiry/V0_1/BulkCall.php b/src/Rest/Inquiry/V0_1/BulkCall.php index 5fe5bd7..c2b1076 100644 --- a/src/Rest/Inquiry/V0_1/BulkCall.php +++ b/src/Rest/Inquiry/V0_1/BulkCall.php @@ -256,25 +256,35 @@ public function export(): RequestInterface /** @psalm-suppress PropertyTypeCoercion */ $request->gateways[$destIdx] = []; $gateways = explode(',', $destGateways); - $gatewayCodecs = !empty($this->gwCodecsList[$destIdx]) + $gatewayCodecs = + (isset($this->gwCodecsList[$destIdx]) && strlen($this->gwCodecsList[$destIdx])) ? str_getcsv($this->gwCodecsList[$destIdx], ',', "'") : []; - $gatewayTimeouts = !empty($this->gwTimeoutsList[$destIdx]) ? explode(',', $this->gwTimeoutsList[$destIdx]) : []; - $gatewayRetries = !empty($this->gwRetriesList[$destIdx]) ? explode(',', $this->gwRetriesList[$destIdx]) : []; + $gatewayTimeouts = + (isset($this->gwTimeoutsList[$destIdx]) && strlen($this->gwTimeoutsList[$destIdx])) + ? explode(',', $this->gwTimeoutsList[$destIdx]) + : []; + $gatewayRetries = + (isset($this->gwRetriesList[$destIdx]) && strlen($this->gwRetriesList[$destIdx])) + ? explode(',', $this->gwRetriesList[$destIdx]) + : []; foreach ($gateways as $gwIdx => $gateway) { $gw = new Gateway(); $gw->name = $gateway; - if (!empty($gatewayCodecs[$gwIdx])) { + if (isset($gatewayCodecs[$gwIdx]) && strlen($gatewayCodecs[$gwIdx])) { $gw->codecs = $gatewayCodecs[$gwIdx]; } - if (!empty($gatewayTimeouts[$gwIdx])) { + if (isset($gatewayTimeouts[$gwIdx]) && strlen($gatewayTimeouts[$gwIdx])) { $gw->timeout = intval($gatewayTimeouts[$gwIdx]); } - $gw->tries = empty($gatewayRetries[$gwIdx]) ? 1 : (int)$gatewayRetries[$gwIdx]; + $gw->tries = + (isset($gatewayRetries[$gwIdx]) && strlen($gatewayRetries[$gwIdx])) + ? (int)$gatewayRetries[$gwIdx] + : 1; /** @psalm-suppress PropertyTypeCoercion */ $request->gateways[$destIdx][] = $gw; diff --git a/src/Rest/Inquiry/V0_1/Call.php b/src/Rest/Inquiry/V0_1/Call.php index 7d820c6..538d0de 100644 --- a/src/Rest/Inquiry/V0_1/Call.php +++ b/src/Rest/Inquiry/V0_1/Call.php @@ -329,15 +329,18 @@ public function export(): RequestInterface $gw = new Gateway(); $gw->name = $gateway; - if (!empty($gatewayCodecs[$gwIdx])) { + if (isset($gatewayCodecs[$gwIdx]) && strlen($gatewayCodecs[$gwIdx])) { $gw->codecs = $gatewayCodecs[$gwIdx]; } - if (!empty($gatewayTimeouts[$gwIdx])) { + if (isset($gatewayTimeouts[$gwIdx]) && strlen($gatewayTimeouts[$gwIdx])) { $gw->timeout = intval($gatewayTimeouts[$gwIdx]); } - $gw->tries = empty($gatewayRetries[$gwIdx]) ? 1 : (int)$gatewayRetries[$gwIdx]; + $gw->tries = + (isset($gatewayRetries[$gwIdx]) && strlen($gatewayRetries[$gwIdx])) + ? (int)$gatewayRetries[$gwIdx] + : 1; $request->gateways[0][] = $gw; } diff --git a/src/Rest/Inquiry/V0_1/GroupCall.php b/src/Rest/Inquiry/V0_1/GroupCall.php index e0807b9..1fa00ae 100644 --- a/src/Rest/Inquiry/V0_1/GroupCall.php +++ b/src/Rest/Inquiry/V0_1/GroupCall.php @@ -284,25 +284,35 @@ public function export(): RequestInterface /** @psalm-suppress PropertyTypeCoercion */ $request->gateways[$destIdx] = []; $gateways = explode(',', $destGateways); - $gatewayCodecs = !empty($this->gwCodecsList[$destIdx]) + $gatewayCodecs = + (isset($this->gwCodecsList[$destIdx]) && strlen($this->gwCodecsList[$destIdx])) ? str_getcsv($this->gwCodecsList[$destIdx], ',', "'") : []; - $gatewayTimeouts = !empty($this->gwTimeoutsList[$destIdx]) ? explode(',', $this->gwTimeoutsList[$destIdx]) : []; - $gatewayRetries = !empty($this->gwRetriesList[$destIdx]) ? explode(',', $this->gwRetriesList[$destIdx]) : []; + $gatewayTimeouts = + (isset($this->gwTimeoutsList[$destIdx]) && strlen($this->gwTimeoutsList[$destIdx])) + ? explode(',', $this->gwTimeoutsList[$destIdx]) + : []; + $gatewayRetries = + (isset($this->gwRetriesList[$destIdx]) && strlen($this->gwRetriesList[$destIdx])) + ? explode(',', $this->gwRetriesList[$destIdx]) + : []; foreach ($gateways as $gwIdx => $gateway) { $gw = new Gateway(); $gw->name = $gateway; - if (!empty($gatewayCodecs[$gwIdx])) { + if (isset($gatewayCodecs[$gwIdx]) && strlen($gatewayCodecs[$gwIdx])) { $gw->codecs = $gatewayCodecs[$gwIdx]; } - if (!empty($gatewayTimeouts[$gwIdx])) { + if (isset($gatewayTimeouts[$gwIdx]) && strlen($gatewayTimeouts[$gwIdx])) { $gw->timeout = intval($gatewayTimeouts[$gwIdx]); } - $gw->tries = empty($gatewayRetries[$gwIdx]) ? 1 : (int)$gatewayRetries[$gwIdx]; + $gw->tries = + (isset($gatewayRetries[$gwIdx]) && strlen($gatewayRetries[$gwIdx])) + ? (int)$gatewayRetries[$gwIdx] + : 1; /** @psalm-suppress PropertyTypeCoercion */ $request->gateways[$destIdx][] = $gw; diff --git a/src/Rest/Server.php b/src/Rest/Server.php index c23c682..f75a3d7 100644 --- a/src/Rest/Server.php +++ b/src/Rest/Server.php @@ -100,7 +100,7 @@ public function run(): void if (!isset($this->config->restServerAdvertisedHost)) { $hostname = gethostname(); - if (!$hostname) { + if ($hostname === false || !strlen($hostname)) { $this->config->restServerAdvertisedHost = $this->config->appPrefix; } else { $this->config->restServerAdvertisedHost = $hostname;