diff --git a/Algorithm/Optimization/GeneticOptimization.php b/Algorithm/Optimization/GeneticOptimization.php index d048b2f6c..6c7fdccc1 100644 --- a/Algorithm/Optimization/GeneticOptimization.php +++ b/Algorithm/Optimization/GeneticOptimization.php @@ -104,7 +104,7 @@ class GeneticOptimization ) : array { $populationSize = \count($population); - $parameterCount = \count(\reset($population)); + $parameterCount = $populationSize === 0 ? 0 : \count(\reset($population)); // Genetic Algorithm Loop for ($generation = 0; $generation < $generations; ++$generation) { diff --git a/Api/CreditRating/CreditSafe.php b/Api/CreditRating/CreditSafe.php index 0361402d1..bac5a7ace 100644 --- a/Api/CreditRating/CreditSafe.php +++ b/Api/CreditRating/CreditSafe.php @@ -137,7 +137,7 @@ final class CreditSafe implements CreditRatingInterface $response = Rest::request($request); - return $response->get('companies') ?? ($response->get('matchedCompanies') ?? []); + return $response->getDataArray('companies') ?? ($response->getDataArray('matchedCompanies') ?? []); } /** @@ -158,7 +158,7 @@ final class CreditSafe implements CreditRatingInterface $response = Rest::request($request); - return $response->get('report') ?? []; + return $response->getDataArray('report') ?? []; } /** @@ -233,7 +233,7 @@ final class CreditSafe implements CreditRatingInterface $response = Rest::request($request); - return $response->get('orders') ?? []; + return $response->getDataArray('orders') ?? []; } /** diff --git a/DataStorage/Session/JWT.php b/DataStorage/Session/JWT.php index c1fd0ac2f..283651e03 100644 --- a/DataStorage/Session/JWT.php +++ b/DataStorage/Session/JWT.php @@ -37,7 +37,7 @@ final class JWT * * @param string $secret Secret (at least 256 bit) * @param array{alg:string, typ:string} $header Header - * @param array{sub:string, ?uid:string, ?name:string, iat:string} $payload Payload + * @param array{sub:string, uid?:string, name?:string, iat:string} $payload Payload * * @return string hmac(Header64 . Payload64, secret) * @@ -45,8 +45,15 @@ final class JWT */ private static function createSignature(string $secret, array $header, array $payload) : string { - $header64 = Base64Url::encode(\json_encode($header)); - $payload64 = Base64Url::encode(\json_encode($payload)); + $headerJson = \json_encode($header); + $payloadJson = \json_encode($payload); + + if (!\is_string($headerJson) || !\is_string($payloadJson)) { + return ''; + } + + $header64 = Base64Url::encode($headerJson); + $payload64 = Base64Url::encode($payloadJson); $algorithm = ''; $algorithm = 'sha256'; @@ -59,7 +66,7 @@ final class JWT * * @param string $secret Secret (at least 256 bit) * @param array{alg:string, typ:string} $header Header - * @param array{sub:string, ?uid:string, ?name:string, iat:string} $payload Payload + * @param array{sub:string, uid?:string, name?:string, iat:string} $payload Payload * * @return string Header64 . Payload64 . hmac(Header64 . Payload64, secret) * @@ -67,8 +74,16 @@ final class JWT */ public static function createJWT(string $secret, array $header, array $payload) : string { - $header64 = Base64Url::encode(\json_encode($header)); - $payload64 = Base64Url::encode(\json_encode($payload)); + $headerJson = \json_encode($header); + $payloadJson = \json_encode($payload); + + if (!\is_string($headerJson) || !\is_string($payloadJson)) { + return ''; + } + + $header64 = Base64Url::encode($headerJson); + $payload64 = Base64Url::encode($payloadJson); + $signature = self::createSignature($secret, $header, $payload); return $header64 . $payload64 . Base64Url::encode($signature); @@ -87,7 +102,7 @@ final class JWT { $explode = \explode('.', $jwt); - if ($explode !== 3) { + if (\count($explode) !== 3) { return []; } @@ -107,7 +122,7 @@ final class JWT { $explode = \explode('.', $jwt); - if ($explode !== 3) { + if (\count($explode) !== 3) { return []; } @@ -128,7 +143,7 @@ final class JWT { $explode = \explode('.', $jwt); - if ($explode !== 3) { + if (\count($explode) !== 3) { return false; } diff --git a/Message/Http/ImpressionStat.php b/Message/Http/ImpressionStat.php index aeca5ccfe..15cd33a84 100644 --- a/Message/Http/ImpressionStat.php +++ b/Message/Http/ImpressionStat.php @@ -102,7 +102,7 @@ class ImpressionStat /** * Additional custom meta data to be stored * - * @var string + * @var array * @since 1.0.0 */ public array $meta = []; diff --git a/Message/ResponseAbstract.php b/Message/ResponseAbstract.php index 28537b59a..53ad70700 100755 --- a/Message/ResponseAbstract.php +++ b/Message/ResponseAbstract.php @@ -100,6 +100,26 @@ abstract class ResponseAbstract implements \JsonSerializable, MessageInterface return (string) $this->data[$key]; } + /** + * Get data. + * + * @param string $key Data key + * + * @return ?array + * + * @since 1.0.0 + */ + public function getDataArray(string $key) : ?array + { + $key = \mb_strtolower($key); + + if (($this->data[$key] ?? '') === '' || !\is_array($this->data[$key])) { + return null; + } + + return $this->data[$key]; + } + /** * Get data. * diff --git a/System/SystemUtils.php b/System/SystemUtils.php index 7378a7246..0286cad5a 100755 --- a/System/SystemUtils.php +++ b/System/SystemUtils.php @@ -121,7 +121,12 @@ final class SystemUtils return -1; } - $cpuUsage = $loadavg[0] * 100 / \exec('nproc'); + $nproc = (int) \exec('nproc'); + if ($nproc === 0) { + return 0; + } + + $cpuUsage = $loadavg[0] * 100 / $nproc; } return (int) $cpuUsage;