From 0a18313e463ff6c8a8bad53dd84b6d651ba12cb7 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 24 Apr 2026 11:42:23 +0100 Subject: [PATCH] Chore: Switch Pint preset from psr12 to per Adopts the PER-CS 2.0 coding standard (superset of PSR-12). Reformats affected files with the preset's fixers: concat_space, trailing_comma_in_multiline, single_line_empty_body, function_declaration, operator_linebreak, nullable_type_declaration, types_spaces, cast_spaces. Co-Authored-By: Claude Opus 4.7 (1M context) --- example/src/server.php | 2 +- pint.json | 4 ++-- src/Http/Adapter/FPM/Request.php | 6 ++--- src/Http/Adapter/FPM/Response.php | 4 ++-- src/Http/Adapter/FPM/Server.php | 8 +++---- src/Http/Adapter/Swoole/Request.php | 8 +++---- src/Http/Adapter/Swoole/Server.php | 4 ++-- src/Http/Adapter/SwooleCoroutine/Request.php | 4 +--- src/Http/Adapter/SwooleCoroutine/Response.php | 4 +--- src/Http/Adapter/SwooleCoroutine/Server.php | 6 ++--- src/Http/Exception.php | 4 +--- src/Http/Files.php | 6 ++--- src/Http/Http.php | 22 +++++++++---------- src/Http/Response.php | 14 ++++++------ src/Http/Router.php | 12 +++++----- src/Http/View.php | 16 +++++++------- tests/HttpTest.php | 14 ++++++------ tests/RouteTest.php | 8 +++---- tests/e2e/Client.php | 8 +++---- tests/e2e/init.php | 2 +- tests/e2e/server-fpm.php | 2 +- tests/e2e/server-swoole.php | 2 +- 22 files changed, 75 insertions(+), 85 deletions(-) diff --git a/example/src/server.php b/example/src/server.php index d05f80f2..792c639f 100644 --- a/example/src/server.php +++ b/example/src/server.php @@ -1,6 +1,6 @@ getProtocol().'://'.$this->getServer('HTTP_HOST', ''), PHP_URL_PORT); + return (string) \parse_url($this->getProtocol() . '://' . $this->getServer('HTTP_HOST', ''), PHP_URL_PORT); } /** @@ -125,7 +125,7 @@ public function getPort(): string */ public function getHostname(): string { - $hostname = \parse_url($this->getProtocol().'://'.$this->getServer('HTTP_HOST', ''), PHP_URL_HOST); + $hostname = \parse_url($this->getProtocol() . '://' . $this->getServer('HTTP_HOST', ''), PHP_URL_HOST); return strtolower((string) ($hostname)); } @@ -352,7 +352,7 @@ protected function generateInput(): array self::METHOD_PUT, self::METHOD_PATCH, self::METHOD_DELETE => $this->payload, - default => $this->queryString + default => $this->queryString, }; } diff --git a/src/Http/Adapter/FPM/Response.php b/src/Http/Adapter/FPM/Response.php index 6a5d4dd4..8b4817d0 100644 --- a/src/Http/Adapter/FPM/Response.php +++ b/src/Http/Adapter/FPM/Response.php @@ -60,10 +60,10 @@ public function sendHeader(string $key, mixed $value): void { if (\is_array($value)) { foreach ($value as $v) { - \header($key.': '.$v, false); + \header($key . ': ' . $v, false); } } else { - \header($key.': '.$value); + \header($key . ': ' . $value); } } diff --git a/src/Http/Adapter/FPM/Server.php b/src/Http/Adapter/FPM/Server.php index d48d6fec..9811ecb3 100755 --- a/src/Http/Adapter/FPM/Server.php +++ b/src/Http/Adapter/FPM/Server.php @@ -7,17 +7,15 @@ class Server extends Adapter { - public function __construct(private Container $container) - { - } + public function __construct(private Container $container) {} public function onRequest(callable $callback) { $request = new Request(); $response = new Response(); - $this->container->set('fpmRequest', fn () => $request); - $this->container->set('fpmResponse', fn () => $response); + $this->container->set('fpmRequest', fn() => $request); + $this->container->set('fpmResponse', fn() => $response); \call_user_func($callback, $request, $response); } diff --git a/src/Http/Adapter/Swoole/Request.php b/src/Http/Adapter/Swoole/Request.php index 92b7360b..43b96421 100644 --- a/src/Http/Adapter/Swoole/Request.php +++ b/src/Http/Adapter/Swoole/Request.php @@ -114,7 +114,7 @@ public function getProtocol(): string return match ($protocol) { 'http', 'https', 'ws', 'wss' => $protocol, - default => 'https' + default => 'https', }; } @@ -127,7 +127,7 @@ public function getProtocol(): string */ public function getPort(): string { - return $this->getHeader('x-forwarded-port', (string) \parse_url($this->getProtocol().'://'.$this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_PORT)); + return $this->getHeader('x-forwarded-port', (string) \parse_url($this->getProtocol() . '://' . $this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_PORT)); } /** @@ -139,7 +139,7 @@ public function getPort(): string */ public function getHostname(): string { - $hostname = \parse_url($this->getProtocol().'://'.$this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_HOST); + $hostname = \parse_url($this->getProtocol() . '://' . $this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_HOST); return strtolower(strval($hostname)); } @@ -364,7 +364,7 @@ protected function generateInput(): array self::METHOD_PUT, self::METHOD_PATCH, self::METHOD_DELETE => $this->payload, - default => $this->queryString + default => $this->queryString, }; } diff --git a/src/Http/Adapter/Swoole/Server.php b/src/Http/Adapter/Swoole/Server.php index 8ba2adbe..7c482ef7 100755 --- a/src/Http/Adapter/Swoole/Server.php +++ b/src/Http/Adapter/Swoole/Server.php @@ -26,8 +26,8 @@ public function onRequest(callable $callback) { $this->server->on('request', function (SwooleRequest $request, SwooleResponse $response) use ($callback) { $requestContainer = new Container($this->container); - $requestContainer->set('swooleRequest', fn () => $request); - $requestContainer->set('swooleResponse', fn () => $response); + $requestContainer->set('swooleRequest', fn() => $request); + $requestContainer->set('swooleResponse', fn() => $response); Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = $requestContainer; diff --git a/src/Http/Adapter/SwooleCoroutine/Request.php b/src/Http/Adapter/SwooleCoroutine/Request.php index a2aa49a5..8522b2ed 100644 --- a/src/Http/Adapter/SwooleCoroutine/Request.php +++ b/src/Http/Adapter/SwooleCoroutine/Request.php @@ -4,6 +4,4 @@ use Utopia\Http\Adapter\Swoole\Request as SwooleAdapterRequest; -class Request extends SwooleAdapterRequest -{ -} +class Request extends SwooleAdapterRequest {} diff --git a/src/Http/Adapter/SwooleCoroutine/Response.php b/src/Http/Adapter/SwooleCoroutine/Response.php index 6c05ed1f..5217a953 100644 --- a/src/Http/Adapter/SwooleCoroutine/Response.php +++ b/src/Http/Adapter/SwooleCoroutine/Response.php @@ -4,6 +4,4 @@ use Utopia\Http\Adapter\Swoole\Response as SwooleAdapterResponse; -class Response extends SwooleAdapterResponse -{ -} +class Response extends SwooleAdapterResponse {} diff --git a/src/Http/Adapter/SwooleCoroutine/Server.php b/src/Http/Adapter/SwooleCoroutine/Server.php index 52b14116..10cfe4ef 100644 --- a/src/Http/Adapter/SwooleCoroutine/Server.php +++ b/src/Http/Adapter/SwooleCoroutine/Server.php @@ -23,7 +23,7 @@ public function __construct( string $host, ?string $port = null, array $settings = [], - ?Container $container = null + ?Container $container = null, ) { $this->server = new SwooleServer($host, $port, false, true); $this->server->set($settings); @@ -34,8 +34,8 @@ public function onRequest(callable $callback) { $this->server->handle('/', function (SwooleRequest $request, SwooleResponse $response) use ($callback) { $requestContainer = new Container($this->container); - $requestContainer->set('swooleRequest', fn () => $request); - $requestContainer->set('swooleResponse', fn () => $response); + $requestContainer->set('swooleRequest', fn() => $request); + $requestContainer->set('swooleResponse', fn() => $response); Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = $requestContainer; diff --git a/src/Http/Exception.php b/src/Http/Exception.php index 46e55958..98814f48 100644 --- a/src/Http/Exception.php +++ b/src/Http/Exception.php @@ -2,6 +2,4 @@ namespace Utopia\Http; -class Exception extends \Exception -{ -} +class Exception extends \Exception {} diff --git a/src/Http/Files.php b/src/Http/Files.php index b5fc5199..3b2c96d4 100644 --- a/src/Http/Files.php +++ b/src/Http/Files.php @@ -110,7 +110,7 @@ public function load(string $directory, ?string $root = null): void continue; } - $dirPath = $directory.'/'.$path; + $dirPath = $directory . '/' . $path; if (is_dir($dirPath)) { $this->load($dirPath, strval($root)); @@ -163,7 +163,7 @@ public function isFileLoaded(string $uri): bool public function getFileContents(string $uri): mixed { if (!array_key_exists($uri, $this->loaded)) { - throw new Exception('File not found or not loaded: '.$uri); + throw new Exception('File not found or not loaded: ' . $uri); } return $this->loaded[$uri]['contents']; @@ -180,7 +180,7 @@ public function getFileContents(string $uri): mixed public function getFileMimeType(string $uri): mixed { if (!array_key_exists($uri, $this->loaded)) { - throw new Exception('File not found or not loaded: '.$uri); + throw new Exception('File not found or not loaded: ' . $uri); } return $this->loaded[$uri]['mimeType']; diff --git a/src/Http/Http.php b/src/Http/Http.php index 7bdd100f..6472c9c8 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -177,7 +177,7 @@ public function setTelemetry(Telemetry $telemetry): void 'http.server.request.duration', 's', null, - ['ExplicitBucketBoundaries' => [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10]] + ['ExplicitBucketBoundaries' => [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10]], ); // https://opentelemetry.io/docs/specs/semconv/http/http-metrics/#metric-httpserveractive_requests @@ -430,7 +430,7 @@ public function getResource(string $name): mixed { try { return $this->server->getContainer()->get($name); - } catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) { + } catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) { // Normalize DI container errors to the Http layer's "resource" terminology. $message = \str_replace('dependency', 'resource', $e->getMessage()); @@ -632,7 +632,7 @@ public function start() { $this->server->onRequest( - fn (Request $request, Response $response) => $this->run($request, $response) + fn(Request $request, Response $response) => $this->run($request, $response), ); $this->server->onStart(function ($server) { @@ -646,7 +646,7 @@ public function start() \call_user_func_array($hook->getAction(), $arguments); } } catch (\Exception $e) { - $this->setResource('error', fn () => $e); + $this->setResource('error', fn() => $e); foreach (self::$errors as $error) { // Global error hooks if (in_array('*', $error->getGroups())) { @@ -745,7 +745,7 @@ public function execute(Route $route, Request $request, Response $response): sta } } } catch (\Throwable $e) { - $this->setRequestResource('error', fn () => $e, []); + $this->setRequestResource('error', fn() => $e, []); foreach ($groups as $group) { foreach (self::$errors as $error) { // Group error hooks @@ -868,8 +868,8 @@ private function runInternal(Request $request, Response $response): static $response->setCompressionSupported($this->compressionSupported); } - $this->setRequestResource('request', fn () => $request); - $this->setRequestResource('response', fn () => $response); + $this->setRequestResource('request', fn() => $request); + $this->setRequestResource('response', fn() => $response); try { foreach (self::$requestHooks as $hook) { @@ -877,7 +877,7 @@ private function runInternal(Request $request, Response $response): static \call_user_func_array($hook->getAction(), $arguments); } } catch (\Exception $e) { - $this->setRequestResource('error', fn () => $e, []); + $this->setRequestResource('error', fn() => $e, []); foreach (self::$errors as $error) { // Global error hooks if (\in_array('*', $error->getGroups())) { @@ -907,7 +907,7 @@ private function runInternal(Request $request, Response $response): static $route = $this->match($request); $groups = ($route instanceof Route) ? $route->getGroups() : []; - $this->setRequestResource('route', fn () => $route, []); + $this->setRequestResource('route', fn() => $route, []); if (self::REQUEST_METHOD_HEAD == $method) { $method = self::REQUEST_METHOD_GET; @@ -953,7 +953,7 @@ private function runInternal(Request $request, Response $response): static $path = \is_string($path) ? ($path === '' ? '/' : $path) : '/'; $route->path($path); - $this->setRequestResource('route', fn () => $route, []); + $this->setRequestResource('route', fn() => $route, []); } if (null !== $route) { @@ -986,7 +986,7 @@ private function runInternal(Request $request, Response $response): static } else { foreach (self::$errors as $error) { // Global error hooks if (\in_array('*', $error->getGroups())) { - $this->setRequestResource('error', fn () => new Exception('Not Found', 404), []); + $this->setRequestResource('error', fn() => new Exception('Not Found', 404), []); \call_user_func_array($error->getAction(), $this->getArguments($error, [], $request->getParams())); } } diff --git a/src/Http/Response.php b/src/Http/Response.php index 41996858..72ff00a3 100755 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -391,7 +391,7 @@ public function setCompressionSupported(mixed $compressionSupported): static */ public function setContentType(string $type, string $charset = ''): static { - $this->contentType = $type.((!empty($charset) ? '; charset='.$charset : '')); + $this->contentType = $type . ((!empty($charset) ? '; charset=' . $charset : '')); return $this; } @@ -619,10 +619,10 @@ public function send(string $body = ''): void // Compress body only if all conditions are met: if ( - !$hasContentEncoding && - !empty($this->acceptEncoding) && - $this->isCompressible($this->contentType) && - strlen($body) > $this->compressionMinSize + !$hasContentEncoding + && !empty($this->acceptEncoding) + && $this->isCompressible($this->contentType) + && strlen($body) > $this->compressionMinSize ) { $algorithm = Compression::fromAcceptEncoding($this->acceptEncoding, $this->compressionSupported); @@ -924,7 +924,7 @@ public function jsonp(string $callback, array $data): void { $this ->setContentType(self::CONTENT_TYPE_JAVASCRIPT, self::CHARSET_UTF8) - ->send('parent.'.$callback.'('.\json_encode($data).');'); + ->send('parent.' . $callback . '(' . \json_encode($data) . ');'); } /** @@ -941,7 +941,7 @@ public function iframe(string $callback, array $data): void { $this ->setContentType(self::CONTENT_TYPE_HTML, self::CHARSET_UTF8) - ->send(''); + ->send(''); } /** diff --git a/src/Http/Router.php b/src/Http/Router.php index 9bc2a969..ce06ad84 100644 --- a/src/Http/Router.php +++ b/src/Http/Router.php @@ -121,24 +121,24 @@ public static function addRouteAlias(string $path, Route $route): void * @param string $path * @return \Utopia\Http\Route|null */ - public static function match(string $method, string $path): Route|null + public static function match(string $method, string $path): ?Route { if (!array_key_exists($method, self::$routes)) { return null; } - $parts = array_values(array_filter(explode('/', $path), fn ($segment) => $segment !== '')); + $parts = array_values(array_filter(explode('/', $path), fn($segment) => $segment !== '')); $length = count($parts) - 1; - $filteredParams = array_filter(self::$params, fn ($i) => $i <= $length); + $filteredParams = array_filter(self::$params, fn($i) => $i <= $length); foreach (self::combinations($filteredParams) as $sample) { - $sample = array_filter($sample, fn (int $i) => $i <= $length); + $sample = array_filter($sample, fn(int $i) => $i <= $length); $match = implode( '/', array_replace( $parts, - array_fill_keys($sample, self::PLACEHOLDER_TOKEN) - ) + array_fill_keys($sample, self::PLACEHOLDER_TOKEN), + ), ); if (array_key_exists($match, self::$routes[$method])) { diff --git a/src/Http/View.php b/src/Http/View.php index 5a223409..1aa51525 100644 --- a/src/Http/View.php +++ b/src/Http/View.php @@ -57,7 +57,7 @@ public function __construct(string $path = '') foreach (\explode("\n\n", $value) as $line) { if (\trim($line)) { - $paragraphs .= '

'.$line.'

'; + $paragraphs .= '

' . $line . '

'; } } @@ -215,14 +215,14 @@ public function print(mixed $value, string|array $filter = ''): mixed if (\is_array($filter)) { foreach ($filter as $callback) { if (!isset($this->filters[$callback])) { - throw new Exception('Filter "'.$callback.'" is not registered'); + throw new Exception('Filter "' . $callback . '" is not registered'); } $value = $this->filters[$callback]($value); } } else { if (!isset($this->filters[$filter])) { - throw new Exception('Filter "'.$filter.'" is not registered'); + throw new Exception('Filter "' . $filter . '" is not registered'); } $value = $this->filters[$filter]($value); @@ -260,7 +260,7 @@ public function render(bool $minify = true): string include $this->path; } else { \ob_end_clean(); - throw new Exception('"'.$this->path.'" view template is not readable'); + throw new Exception('"' . $this->path . '" view template is not readable'); } $html = \ob_get_contents(); @@ -274,10 +274,10 @@ public function render(bool $minify = true): string // replacing both with /
$index
$html = \str_replace($foundTxt[0], \array_map(function ($el) { - return ''; + return ''; }, \array_keys($foundTxt[0])), $html); $html = \str_replace($foundPre[0], \array_map(function ($el) { - return '
'.$el.'
'; + return '
' . $el . '
'; }, \array_keys($foundPre[0])), $html); // your stuff @@ -297,10 +297,10 @@ public function render(bool $minify = true): string // Replacing back with content $html = \str_replace(\array_map(function ($el) { - return ''; + return ''; }, \array_keys($foundTxt[0])), $foundTxt[0], $html); $html = \str_replace(\array_map(function ($el) { - return '
'.$el.'
'; + return '
' . $el . '
'; }, \array_keys($foundPre[0])), $foundPre[0], $html); } diff --git a/tests/HttpTest.php b/tests/HttpTest.php index 2e00645a..4b570d94 100755 --- a/tests/HttpTest.php +++ b/tests/HttpTest.php @@ -87,7 +87,7 @@ public function testCanGetEnvironmentVariable(): void public function testCanExecuteRoute(): void { - $this->container->set('rand', fn () => rand()); + $this->container->set('rand', fn() => rand()); $resource = $this->container->get('rand'); $this->http @@ -386,7 +386,7 @@ public function providerRouteMatching(): array 'DELETE request' => [Http::REQUEST_METHOD_DELETE, '/path1'], '1 separators' => [Http::REQUEST_METHOD_GET, '/a/'], '2 separators' => [Http::REQUEST_METHOD_GET, '/a/b'], - '3 separators' => [Http::REQUEST_METHOD_GET, '/a/b/c'] + '3 separators' => [Http::REQUEST_METHOD_GET, '/a/b/c'], ]; } @@ -428,15 +428,15 @@ public function testNoMismatchRoute(): void $requests = [ [ 'path' => '/d/:id', - 'url' => '/d/' + 'url' => '/d/', ], [ 'path' => '/d/:id/e/:id2', - 'url' => '/d/123/e/' + 'url' => '/d/123/e/', ], [ 'path' => '/d/:id/e/:id2/f/:id3', - 'url' => '/d/123/e/456/f/' + 'url' => '/d/123/e/456/f/', ], ]; @@ -532,7 +532,7 @@ public function testWildcardRoute(): void Http::init() ->action(function () { $route = $this->http->getRoute(); - $this->container->set('myRoute', fn () => $route); + $this->container->set('myRoute', fn() => $route); }); @@ -653,7 +653,7 @@ public function testCanInjectResourceAndParamWithSameName(): void { // Register a 'locale' resource returning a Locale instance whose // `name` statically resolves to "en". - $this->container->set('locale', fn () => new Locale()); + $this->container->set('locale', fn() => new Locale()); $route = new Route('GET', '/path'); diff --git a/tests/RouteTest.php b/tests/RouteTest.php index ea48e111..6ee4680b 100755 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -48,10 +48,9 @@ public function testCanSetAndGetGroups() public function testCanSetAndGetAction() { - $this->assertEquals(function (): void { - }, $this->route->getAction()); + $this->assertEquals(function (): void {}, $this->route->getAction()); - $this->route->action(fn () => 'hello world'); + $this->route->action(fn() => 'hello world'); $this->assertEquals('hello world', $this->route->getAction()()); } @@ -74,8 +73,7 @@ public function testCanInjectResources() $this->route ->inject('user') ->inject('time') - ->action(function () { - }); + ->action(function () {}); $this->assertCount(2, $this->route->getInjections()); $this->assertEquals('user', $this->route->getInjections()['user']['name']); diff --git a/tests/e2e/Client.php b/tests/e2e/Client.php index 42fdd5a8..be96c25b 100644 --- a/tests/e2e/Client.php +++ b/tests/e2e/Client.php @@ -55,7 +55,7 @@ public function __construct(string $baseUrl = 'http://fpm') public function call(string $method, string $path = '', array $headers = [], array $params = []) { usleep(50000); - $ch = curl_init($this->baseUrl.$path.(($method == self::METHOD_GET && !empty($params)) ? '?'.http_build_query($params) : '')); + $ch = curl_init($this->baseUrl . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : '')); $responseHeaders = []; $responseStatus = -1; $responseType = ''; @@ -79,7 +79,7 @@ public function call(string $method, string $path = '', array $headers = [], arr } if (strtolower(trim($header[0])) == 'set-cookie') { - $parsed = $this->parseCookie((string)trim($header[1])); + $parsed = $this->parseCookie((string) trim($header[1])); $name = array_key_first($parsed); $cookies[$name] = $parsed[$name]; } @@ -93,7 +93,7 @@ public function call(string $method, string $path = '', array $headers = [], arr $responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ((curl_errno($ch)/* || 200 != $responseStatus*/)) { - throw new Exception(curl_error($ch).' with status code '.$responseStatus, $responseStatus); + throw new Exception(curl_error($ch) . ' with status code ' . $responseStatus, $responseStatus); } curl_close($ch); @@ -101,7 +101,7 @@ public function call(string $method, string $path = '', array $headers = [], arr $responseHeaders['status-code'] = $responseStatus; if ($responseStatus === 500) { - echo 'Server error('.$method.': '.$path.'. Params: '.json_encode($params).'): '.json_encode($responseBody)."\n"; + echo 'Server error(' . $method . ': ' . $path . '. Params: ' . json_encode($params) . '): ' . json_encode($responseBody) . "\n"; } return [ diff --git a/tests/e2e/init.php b/tests/e2e/init.php index 9f410ce7..beba3646 100644 --- a/tests/e2e/init.php +++ b/tests/e2e/init.php @@ -1,6 +1,6 @@