From b42d6da780ea0c43a3a61e843229760a7e2e2069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederic=20G=2E=20=C3=98stby?= Date: Mon, 11 Nov 2024 15:06:26 +0100 Subject: [PATCH] Cleaning up --- src/mako/cli/output/components/Countdown.php | 4 ++-- src/mako/cli/output/components/OrderedList.php | 4 ++-- .../components/progress/traits/ProgressTrait.php | 2 +- src/mako/error/handlers/web/DevelopmentHandler.php | 2 +- src/mako/file/FileSystem.php | 6 +++--- src/mako/http/response/senders/File.php | 4 ++-- src/mako/redis/Redis.php | 11 +++-------- src/mako/utility/Str.php | 2 +- src/mako/utility/UUID.php | 2 +- 9 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/mako/cli/output/components/Countdown.php b/src/mako/cli/output/components/Countdown.php index 78e21ef9a..89a087a4e 100644 --- a/src/mako/cli/output/components/Countdown.php +++ b/src/mako/cli/output/components/Countdown.php @@ -57,13 +57,13 @@ public function draw(int $from = 5): void $dots = 0; - $fromLength = strlen($from); + $fromLength = strlen((string) $from); $totalLength = $fromLength + 5; do { do { - $numbers = str_pad($from, $fromLength, '0', STR_PAD_LEFT); + $numbers = str_pad((string) $from, $fromLength, '0', STR_PAD_LEFT); $this->output->write("\r" . str_pad($numbers . ' ' . str_repeat('.', $dots) . ' ', $totalLength, ' ')); diff --git a/src/mako/cli/output/components/OrderedList.php b/src/mako/cli/output/components/OrderedList.php index 49cb1c1dd..9002c3b9a 100644 --- a/src/mako/cli/output/components/OrderedList.php +++ b/src/mako/cli/output/components/OrderedList.php @@ -54,7 +54,7 @@ protected function calculateWidth(array $items, string $marker): array } } - $number = strlen($count); + $number = strlen((string) $count); $marker = strlen(sprintf($this->formatter === null ? $marker : $this->formatter->stripTags($marker), '')) + $number; @@ -66,7 +66,7 @@ protected function calculateWidth(array $items, string $marker): array */ protected function buildListItem(string $item, string $marker, int $width, int $number, int $nestingLevel, int $parentWidth): string { - $marker = str_repeat(' ', $width - strlen($number)) . sprintf($marker, $number); + $marker = str_repeat(' ', $width - strlen((string) $number)) . sprintf($marker, $number); return str_repeat($this->padding, $nestingLevel) . str_repeat(' ', $parentWidth) . "{$marker} {$item}" . PHP_EOL; } diff --git a/src/mako/cli/output/components/progress/traits/ProgressTrait.php b/src/mako/cli/output/components/progress/traits/ProgressTrait.php index 3bb71bab7..8bba81892 100644 --- a/src/mako/cli/output/components/progress/traits/ProgressTrait.php +++ b/src/mako/cli/output/components/progress/traits/ProgressTrait.php @@ -42,7 +42,7 @@ protected function buildProgressBar(float $percent): string $progress = number_format($percent * 100, 2, '.'); - return str_pad($this->progress, strlen($this->itemCount), '0', STR_PAD_LEFT) + return str_pad((string) $this->progress, strlen((string) $this->itemCount), '0', STR_PAD_LEFT) . "/{$this->itemCount} " . str_repeat($this->progressBar->getFilled(), $fill) . str_repeat($this->progressBar->getEmpty(), ($this->width - $fill)) diff --git a/src/mako/error/handlers/web/DevelopmentHandler.php b/src/mako/error/handlers/web/DevelopmentHandler.php index 452fadfa9..9017a5770 100644 --- a/src/mako/error/handlers/web/DevelopmentHandler.php +++ b/src/mako/error/handlers/web/DevelopmentHandler.php @@ -111,7 +111,7 @@ protected function getExceptionAsXml(Throwable $exception): string $xml->addChild('file', $exception->getFile()); - $xml->addChild('line', $exception->getLine()); + $xml->addChild('line', (string) $exception->getLine()); $xml->addChild('exception_id', $this->exceptionId); diff --git a/src/mako/file/FileSystem.php b/src/mako/file/FileSystem.php index cc9fdfd04..f6a33456a 100644 --- a/src/mako/file/FileSystem.php +++ b/src/mako/file/FileSystem.php @@ -256,7 +256,7 @@ public function get(string $path): false|string /** * Writes the supplied data to a file. */ - public static function put(string $path, mixed $data, bool $lock = false): false|string + public static function put(string $path, mixed $data, bool $lock = false): false|int { return file_put_contents($path, $data, $lock ? LOCK_EX : 0); } @@ -264,7 +264,7 @@ public static function put(string $path, mixed $data, bool $lock = false): false /** * Prepends the supplied data to a file. */ - public static function prepend(string $path, mixed $data, bool $lock = false): false|string + public static function prepend(string $path, mixed $data, bool $lock = false): false|int { return file_put_contents($path, $data . file_get_contents($path), $lock ? LOCK_EX : 0); } @@ -272,7 +272,7 @@ public static function prepend(string $path, mixed $data, bool $lock = false): f /** * Appends the supplied data to a file. */ - public static function append(string $path, mixed $data, bool $lock = false): false|string + public static function append(string $path, mixed $data, bool $lock = false): false|int { return file_put_contents($path, $data, $lock ? FILE_APPEND | LOCK_EX : FILE_APPEND); } diff --git a/src/mako/http/response/senders/File.php b/src/mako/http/response/senders/File.php index 576931f49..29ea1227d 100644 --- a/src/mako/http/response/senders/File.php +++ b/src/mako/http/response/senders/File.php @@ -256,7 +256,7 @@ public function send(Request $request, Response $response): void $range = ['start' => 0, 'end' => $this->fileSize - 1]; - $response->headers->add('Content-Length', $this->fileSize); + $response->headers->add('Content-Length', (string) $this->fileSize); } else { // Valid range so we'll need to tell the client which range we're sending @@ -266,7 +266,7 @@ public function send(Request $request, Response $response): void $response->headers->add('Content-Range', sprintf('bytes %s-%s/%s', $range['start'], $range['end'], $this->fileSize)); - $response->headers->add('Content-Length', $range['end'] - $range['start'] + 1); + $response->headers->add('Content-Length', (string) ($range['end'] - $range['start'] + 1)); } // Send headers and the requested byte range diff --git a/src/mako/redis/Redis.php b/src/mako/redis/Redis.php index ecc598fba..feb817058 100644 --- a/src/mako/redis/Redis.php +++ b/src/mako/redis/Redis.php @@ -25,7 +25,6 @@ use function explode; use function implode; use function in_array; -use function is_string; use function strlen; use function substr; use function trim; @@ -192,9 +191,9 @@ public function getAttributes(): array */ protected function createClusterClient(string $server): Redis { - [$server, $port] = explode(':', $server, 2); + [$host, $port] = explode(':', $server, 2); - return new static(new Connection($server, $port, $this->connection->getOptions()), [ + return new static(new Connection($host, (int) $port, $this->connection->getOptions()), [ 'resp' => $this->resp, 'username' => $this->username, 'password' => $this->password, @@ -641,10 +640,6 @@ protected function buildAndSendCommandAndReturnResponse(array $command, array $a */ public function executeCommand(string $command, mixed ...$arguments): mixed { - if (is_string($command)) { - $command = explode(' ', $command); - } - - return $this->buildAndSendCommandAndReturnResponse($command, $arguments); + return $this->buildAndSendCommandAndReturnResponse(explode(' ', $command), $arguments); } } diff --git a/src/mako/utility/Str.php b/src/mako/utility/Str.php index 7937e8196..f342d496c 100644 --- a/src/mako/utility/Str.php +++ b/src/mako/utility/Str.php @@ -234,7 +234,7 @@ public static function mask(string $string, int $visible = 3, string $mask = '*' */ public static function increment(string $string, int $start = 1, string $separator = '_'): string { - preg_match('/(.+)' . preg_quote($separator) . '([0-9]+)$/', $string, $matches); + preg_match('/(.+)' . preg_quote($separator, delimiter: '/') . '([0-9]+)$/', $string, $matches); return isset($matches[2]) ? "{$matches[1]}{$separator}" . ((int) $matches[2] + 1) : "{$string}{$separator}{$start}"; } diff --git a/src/mako/utility/UUID.php b/src/mako/utility/UUID.php index 7999ec999..83b7f76da 100644 --- a/src/mako/utility/UUID.php +++ b/src/mako/utility/UUID.php @@ -142,7 +142,7 @@ public static function v4Sequential(): string { [$usec, $sec] = explode(' ', microtime()); - $random = hex2bin(dechex($sec . substr($usec, 2, 5)) . bin2hex(random_bytes(10))); + $random = hex2bin(dechex((int) ($sec . substr($usec, 2, 5))) . bin2hex(random_bytes(10))); $random[6] = chr(ord($random[6]) & 0x0f | 0x40);