Skip to content

Commit bddf77e

Browse files
authored
Chore: Type class constants and expand Pint rule set (#250)
Add explicit types to all 110 class constants across the public API (string/int/array) — leveraging PHP 8.3 typed constants now that the floor is 8.3. Expand pint.json beyond the bare `per` preset with rules that match common editor inspections, so CI catches what the editor flags: - native_function_invocation (@compiler_optimized, namespaced scope) - no_unused_imports, ordered_imports - no_useless_else, no_useless_return - no_empty_statement, no_empty_phpdoc, no_empty_comment - array_syntax (short), single_quote - trailing_comma_in_multiline Pint applied the new rules across the codebase (mainly prefixing compiler-optimized builtins with `\` in namespaced files and sorting imports).
1 parent d1eced0 commit bddf77e

19 files changed

Lines changed: 294 additions & 271 deletions

example/src/server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
require_once __DIR__ . '/../vendor/autoload.php';
44

5+
use Utopia\Http\Adapter\Swoole\Server;
56
use Utopia\Http\Http;
67
use Utopia\Http\Response;
7-
use Utopia\Http\Adapter\Swoole\Server;
88
use Utopia\Validator\Text;
99

1010
Http::get('/')

pint.json

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
11
{
2-
"preset": "per"
2+
"preset": "per",
3+
"rules": {
4+
"native_function_invocation": {
5+
"include": ["@compiler_optimized"],
6+
"scope": "namespaced",
7+
"strict": true
8+
},
9+
"no_unused_imports": true,
10+
"ordered_imports": {
11+
"sort_algorithm": "alpha"
12+
},
13+
"no_useless_else": true,
14+
"no_useless_return": true,
15+
"no_empty_statement": true,
16+
"no_empty_phpdoc": true,
17+
"no_empty_comment": true,
18+
"array_syntax": {
19+
"syntax": "short"
20+
},
21+
"single_quote": true,
22+
"trailing_comma_in_multiline": {
23+
"elements": ["arrays", "arguments", "parameters"]
24+
}
25+
}
326
}

rector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector;
99
use Rector\Config\RectorConfig;
1010
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
11-
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
1211
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
1312
use Rector\Php70\Rector\FuncCall\RandomFunctionRector;
1413
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;
@@ -20,6 +19,7 @@
2019
use Rector\PHPUnit\Set\PHPUnitSetList;
2120
use Rector\Set\ValueObject\LevelSetList;
2221
use Rector\Set\ValueObject\SetList;
22+
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
2323

2424
return RectorConfig::configure()
2525
->withPaths([

src/Http/Adapter/FPM/Request.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ public function getIP(): string
6666
}
6767

6868
// Leftmost IP address is the address of the originating client
69-
$ips = \explode(',', $headerValue);
70-
$ip = \trim($ips[0]);
69+
$ips = explode(',', $headerValue);
70+
$ip = trim($ips[0]);
7171

7272
// Validate IP format (supports both IPv4 and IPv6)
73-
if (\filter_var($ip, FILTER_VALIDATE_IP)) {
73+
if (filter_var($ip, FILTER_VALIDATE_IP)) {
7474
return $ip;
7575
}
7676
}
@@ -97,7 +97,7 @@ public function getProtocol(): string
9797
*/
9898
public function getPort(): string
9999
{
100-
return (string) \parse_url($this->getProtocol() . '://' . $this->getServer('HTTP_HOST', ''), PHP_URL_PORT);
100+
return (string) parse_url($this->getProtocol() . '://' . $this->getServer('HTTP_HOST', ''), PHP_URL_PORT);
101101
}
102102

103103
/**
@@ -107,7 +107,7 @@ public function getPort(): string
107107
*/
108108
public function getHostname(): string
109109
{
110-
$hostname = \parse_url($this->getProtocol() . '://' . $this->getServer('HTTP_HOST', ''), PHP_URL_HOST);
110+
$hostname = parse_url($this->getProtocol() . '://' . $this->getServer('HTTP_HOST', ''), PHP_URL_HOST);
111111
return strtolower((string) ($hostname));
112112
}
113113

@@ -233,7 +233,7 @@ public function getHeader(string $key, string $default = ''): string
233233

234234
$value = $headers[$key];
235235

236-
return \is_array($value) ? \implode(', ', $value) : $value;
236+
return \is_array($value) ? implode(', ', $value) : $value;
237237
}
238238

239239
/**
@@ -278,14 +278,14 @@ protected function generateInput(): array
278278
$contentType = $this->getHeader('content-type');
279279

280280
// Get content-type without the charset
281-
$length = \strpos($contentType, ';');
281+
$length = strpos($contentType, ';');
282282
$length = (empty($length)) ? \strlen($contentType) : $length;
283-
$contentType = \substr($contentType, 0, $length);
283+
$contentType = substr($contentType, 0, $length);
284284

285-
$this->rawPayload = \file_get_contents('php://input') ?: '';
285+
$this->rawPayload = file_get_contents('php://input') ?: '';
286286

287287
$this->payload = match ($contentType) {
288-
'application/json' => \json_decode($this->rawPayload, true),
288+
'application/json' => json_decode($this->rawPayload, true),
289289
default => $_POST,
290290
};
291291

@@ -323,7 +323,7 @@ protected function generateHeaders(): array
323323

324324
foreach ($_SERVER as $name => $value) {
325325
if (str_starts_with($name, 'HTTP_')) {
326-
$headers[\str_replace(' ', '-', \strtolower(\str_replace('_', ' ', \substr($name, 5))))] = $value;
326+
$headers[str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($name, 5))))] = $value;
327327
}
328328
}
329329

src/Http/Adapter/FPM/Response.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function write(string $content): bool
2828
*/
2929
public function end(?string $content = null): void
3030
{
31-
if (!is_null($content)) {
31+
if (!\is_null($content)) {
3232
echo $content;
3333
}
3434
}
@@ -53,10 +53,10 @@ public function sendHeader(string $key, mixed $value): void
5353
{
5454
if (\is_array($value)) {
5555
foreach ($value as $v) {
56-
\header($key . ': ' . $v, false);
56+
header($key . ': ' . $v, false);
5757
}
5858
} else {
59-
\header($key . ': ' . $value);
59+
header($key . ': ' . $value);
6060
}
6161
}
6262

@@ -74,6 +74,6 @@ protected function sendCookie(string $name, string $value, array $options): void
7474
unset($options['expire']);
7575

7676
// Set the cookie
77-
\setcookie($name, $value, $options);
77+
setcookie($name, $value, $options);
7878
}
7979
}

src/Http/Adapter/Swoole/Request.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function getProtocol(): string
111111
*/
112112
public function getPort(): string
113113
{
114-
return $this->getHeader('x-forwarded-port', (string) \parse_url($this->getProtocol() . '://' . $this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_PORT));
114+
return $this->getHeader('x-forwarded-port', (string) parse_url($this->getProtocol() . '://' . $this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_PORT));
115115
}
116116

117117
/**
@@ -121,8 +121,8 @@ public function getPort(): string
121121
*/
122122
public function getHostname(): string
123123
{
124-
$hostname = \parse_url($this->getProtocol() . '://' . $this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_HOST);
125-
return strtolower(strval($hostname));
124+
$hostname = parse_url($this->getProtocol() . '://' . $this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_HOST);
125+
return strtolower(\strval($hostname));
126126
}
127127

128128
/**
@@ -291,11 +291,11 @@ protected function generateInput(): array
291291

292292
// Get content-type without the charset
293293
$length = strpos($contentType, ';');
294-
$length = (empty($length)) ? strlen($contentType) : $length;
294+
$length = (empty($length)) ? \strlen($contentType) : $length;
295295
$contentType = substr($contentType, 0, $length);
296296

297297
$this->payload = match ($contentType) {
298-
'application/json' => json_decode(strval($this->swoole->rawContent()), true),
298+
'application/json' => json_decode(\strval($this->swoole->rawContent()), true),
299299
default => $this->swoole->post,
300300
};
301301

src/Http/Adapter/Swoole/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Response extends UtopiaResponse
2020
public function __construct(SwooleResponse $response)
2121
{
2222
$this->swoole = $response;
23-
parent::__construct(\microtime(true));
23+
parent::__construct(microtime(true));
2424
}
2525

2626
public function getSwooleResponse(): SwooleResponse

src/Http/Adapter/Swoole/Server.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
namespace Utopia\Http\Adapter\Swoole;
44

55
use Swoole\Coroutine;
6-
use Utopia\Http\Adapter;
7-
use Utopia\DI\Container;
8-
use Swoole\Http\Server as SwooleServer;
96
use Swoole\Http\Request as SwooleRequest;
107
use Swoole\Http\Response as SwooleResponse;
8+
use Swoole\Http\Server as SwooleServer;
9+
use Utopia\DI\Container;
10+
use Utopia\Http\Adapter;
1111

1212
class Server extends Adapter
1313
{
1414
protected SwooleServer $server;
15-
protected const REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';
15+
protected const string REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';
1616
protected Container $container;
1717

1818
/**

src/Http/Adapter/SwooleCoroutine/Server.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
namespace Utopia\Http\Adapter\SwooleCoroutine;
44

55
use Swoole\Coroutine;
6-
use Utopia\Http\Adapter;
7-
use Utopia\DI\Container;
86
use Swoole\Coroutine\Http\Server as SwooleServer;
97
use Swoole\Http\Request as SwooleRequest;
108
use Swoole\Http\Response as SwooleResponse;
9+
use Utopia\DI\Container;
10+
use Utopia\Http\Adapter;
1111

1212
class Server extends Adapter
1313
{
14-
protected const REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';
14+
protected const string REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';
1515

1616
protected SwooleServer $server;
1717
protected Container $container;

src/Http/Files.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Files
2121
/**
2222
* @var array<string, mixed>
2323
*/
24-
public const EXTENSIONS = [
24+
public const array EXTENSIONS = [
2525
'css' => 'text/css',
2626
'js' => 'text/javascript',
2727
'svg' => 'image/svg+xml',
@@ -79,7 +79,7 @@ public function load(string $directory, ?string $root = null): void
7979

8080
$root ??= $directory;
8181

82-
$handle = opendir(strval($directory));
82+
$handle = opendir(\strval($directory));
8383

8484
if ($handle === false) {
8585
throw new Exception("Failed to open directory: {$directory}");
@@ -88,11 +88,11 @@ public function load(string $directory, ?string $root = null): void
8888
while ($path = readdir($handle)) {
8989
$extension = pathinfo($path, PATHINFO_EXTENSION);
9090

91-
if (in_array($path, ['.', '..'])) {
91+
if (\in_array($path, ['.', '..'])) {
9292
continue;
9393
}
9494

95-
if (in_array($extension, ['php', 'phtml'])) {
95+
if (\in_array($extension, ['php', 'phtml'])) {
9696
continue;
9797
}
9898

@@ -103,20 +103,20 @@ public function load(string $directory, ?string $root = null): void
103103
$dirPath = $directory . '/' . $path;
104104

105105
if (is_dir($dirPath)) {
106-
$this->load($dirPath, strval($root));
106+
$this->load($dirPath, \strval($root));
107107

108108
continue;
109109
}
110110

111-
$key = substr($dirPath, strlen(strval($root)));
111+
$key = substr($dirPath, \strlen(\strval($root)));
112112

113-
if (array_key_exists($key, $this->loaded)) {
113+
if (\array_key_exists($key, $this->loaded)) {
114114
continue;
115115
}
116116

117117
$this->loaded[$key] = [
118118
'contents' => file_get_contents($dirPath),
119-
'mimeType' => (array_key_exists($extension, self::EXTENSIONS))
119+
'mimeType' => (\array_key_exists($extension, self::EXTENSIONS))
120120
? self::EXTENSIONS[$extension]
121121
: mime_content_type($dirPath),
122122
];
@@ -132,7 +132,7 @@ public function load(string $directory, ?string $root = null): void
132132
*/
133133
public function isFileLoaded(string $uri): bool
134134
{
135-
return array_key_exists($uri, $this->loaded);
135+
return \array_key_exists($uri, $this->loaded);
136136
}
137137

138138
/**
@@ -143,7 +143,7 @@ public function isFileLoaded(string $uri): bool
143143
*/
144144
public function getFileContents(string $uri): mixed
145145
{
146-
if (!array_key_exists($uri, $this->loaded)) {
146+
if (!\array_key_exists($uri, $this->loaded)) {
147147
throw new Exception('File not found or not loaded: ' . $uri);
148148
}
149149

@@ -158,7 +158,7 @@ public function getFileContents(string $uri): mixed
158158
*/
159159
public function getFileMimeType(string $uri): mixed
160160
{
161-
if (!array_key_exists($uri, $this->loaded)) {
161+
if (!\array_key_exists($uri, $this->loaded)) {
162162
throw new Exception('File not found or not loaded: ' . $uri);
163163
}
164164

0 commit comments

Comments
 (0)