Skip to content

Commit cacb3e8

Browse files
authored
Merge pull request #220 from open-runtimes/refactor-rector
refactor: rector
2 parents 16c9235 + 120e0af commit cacb3e8

File tree

24 files changed

+644
-685
lines changed

24 files changed

+644
-685
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ jobs:
3636
docker run --rm -v $PWD:/app composer:2.8 sh -c \
3737
"composer install --profile --ignore-platform-reqs && composer analyze"
3838
39+
refactor:
40+
name: Refactor
41+
runs-on: ubuntu-latest
42+
43+
steps:
44+
- name: Check out the repo
45+
uses: actions/checkout@v6
46+
47+
- name: Run Refactor
48+
run: |
49+
docker run --rm -v $PWD:/app composer:2.8 sh -c \
50+
"composer install --profile --ignore-platform-reqs && composer refactor:check"
51+
3952
unit-tests:
4053
name: Unit Tests
4154
runs-on: ubuntu-latest

app/controllers.php

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
->param('timeout', '600', new Text(16), 'Maximum logs timeout.', true)
2626
->inject('response')
2727
->inject('runner')
28-
->action(function (string $runtimeId, string $timeoutStr, Response $response, Runner $runner) {
28+
->action(function (string $runtimeId, string $timeoutStr, Response $response, Runner $runner): void {
2929
$timeout = \intval($timeoutStr);
3030

3131
$response->sendHeader('Content-Type', 'text/event-stream');
@@ -43,7 +43,7 @@
4343
->param('timeout', 600, new Integer(), 'Commands execution time in seconds.', true)
4444
->inject('response')
4545
->inject('runner')
46-
->action(function (string $runtimeId, string $command, int $timeout, Response $response, Runner $runner) {
46+
->action(function (string $runtimeId, string $command, int $timeout, Response $response, Runner $runner): void {
4747
$output = $runner->executeCommand($runtimeId, $command, $timeout);
4848
$response->setStatusCode(Response::STATUS_CODE_OK)->json([ 'output' => $output ]);
4949
});
@@ -68,7 +68,7 @@
6868
->param('restartPolicy', DockerAPI::RESTART_NO, new WhiteList([DockerAPI::RESTART_NO, DockerAPI::RESTART_ALWAYS, DockerAPI::RESTART_ON_FAILURE, DockerAPI::RESTART_UNLESS_STOPPED], true), 'Define restart policy for the runtime once an exit code is returned. Default value is "no". Possible values are "no", "always", "on-failure", "unless-stopped".', true)
6969
->inject('response')
7070
->inject('runner')
71-
->action(function (string $runtimeId, string $image, string $entrypoint, string $source, string $destination, string $outputDirectory, array $variables, string $runtimeEntrypoint, string $command, int $timeout, bool $remove, float $cpus, int $memory, string $version, string $restartPolicy, Response $response, Runner $runner) {
71+
->action(function (string $runtimeId, string $image, string $entrypoint, string $source, string $destination, string $outputDirectory, array $variables, string $runtimeEntrypoint, string $command, int $timeout, bool $remove, float $cpus, int $memory, string $version, string $restartPolicy, Response $response, Runner $runner): void {
7272
$secret = \bin2hex(\random_bytes(16));
7373

7474
/**
@@ -90,7 +90,7 @@
9090
default => [],
9191
});
9292

93-
if (!empty($outputDirectory)) {
93+
if ($outputDirectory !== '' && $outputDirectory !== '0') {
9494
$variables = \array_merge($variables, [
9595
'OPEN_RUNTIMES_OUTPUT_DIRECTORY' => $outputDirectory
9696
]);
@@ -100,7 +100,7 @@
100100
'CI' => 'true'
101101
]);
102102

103-
$variables = array_map(fn ($v) => strval($v), $variables);
103+
$variables = array_map(strval(...), $variables);
104104

105105
$container = $runner->createRuntime($runtimeId, $secret, $image, $entrypoint, $source, $destination, $variables, $runtimeEntrypoint, $command, $timeout, $remove, $cpus, $memory, $version, $restartPolicy);
106106
$response->setStatusCode(Response::STATUS_CODE_CREATED)->json($container);
@@ -111,7 +111,7 @@
111111
->desc("List currently active runtimes")
112112
->inject('runner')
113113
->inject('response')
114-
->action(function (Runner $runner, Response $response) {
114+
->action(function (Runner $runner, Response $response): void {
115115
$response->setStatusCode(Response::STATUS_CODE_OK)->json($runner->getRuntimes());
116116
});
117117

@@ -121,7 +121,7 @@
121121
->param('runtimeId', '', new Text(64), 'Runtime unique ID.')
122122
->inject('runner')
123123
->inject('response')
124-
->action(function (string $runtimeId, Runner $runner, Response $response) {
124+
->action(function (string $runtimeId, Runner $runner, Response $response): void {
125125
$runtimeName = System::getHostname() . '-' . $runtimeId;
126126
$response->setStatusCode(Response::STATUS_CODE_OK)->json($runner->getRuntime($runtimeName));
127127
});
@@ -132,7 +132,7 @@
132132
->param('runtimeId', '', new Text(64), 'Runtime unique ID.')
133133
->inject('response')
134134
->inject('runner')
135-
->action(function (string $runtimeId, Response $response, Runner $runner) {
135+
->action(function (string $runtimeId, Response $response, Runner $runner): void {
136136
$runner->deleteRuntime($runtimeId);
137137
$response->setStatusCode(Response::STATUS_CODE_OK)->send();
138138
});
@@ -183,39 +183,18 @@ function (
183183
Response $response,
184184
Request $request,
185185
Runner $runner
186-
) {
187-
// Extra parsers and validators to support both JSON and multipart
188-
$intParams = ['timeout', 'memory'];
189-
foreach ($intParams as $intParam) {
190-
if (!empty($$intParam) && !is_numeric($$intParam)) {
191-
$$intParam = \intval($$intParam);
192-
}
186+
): void {
187+
// Parse JSON strings for assoc params when coming from multipart
188+
if (\is_string($headers)) {
189+
$headers = \json_decode($headers, true) ?? [];
193190
}
194191

195-
$floatParams = ['cpus'];
196-
foreach ($floatParams as $floatPram) {
197-
if (!empty($$floatPram) && !is_numeric($$floatPram)) {
198-
$$floatPram = \floatval($$floatPram);
199-
}
192+
if (\is_string($variables)) {
193+
$variables = \json_decode($variables, true) ?? [];
200194
}
201195

202-
/**
203-
* @var array<string, mixed> $headers
204-
* @var array<string, mixed> $variables
205-
*/
206-
$assocParams = ['headers', 'variables'];
207-
foreach ($assocParams as $assocParam) {
208-
if (!empty($$assocParam) && !is_array($$assocParam)) {
209-
$$assocParam = \json_decode($$assocParam, true);
210-
}
211-
}
212-
213-
$booleanParams = ['logging'];
214-
foreach ($booleanParams as $booleamParam) {
215-
if (!empty($$booleamParam) && !is_bool($$booleamParam)) {
216-
$$booleamParam = $$booleamParam === "true" ? true : false;
217-
}
218-
}
196+
/** @var array<string, mixed> $headers */
197+
/** @var array<string, mixed> $variables */
219198

220199
// 'headers' validator
221200
$validator = new Assoc();
@@ -229,11 +208,11 @@ function (
229208
throw new Exception(Exception::EXECUTION_BAD_REQUEST, $validator->getDescription());
230209
}
231210

232-
if (empty($payload)) {
211+
if (in_array($payload, [null, '', '0'], true)) {
233212
$payload = '';
234213
}
235214

236-
$variables = array_map(fn ($v) => strval($v), $variables);
215+
$variables = array_map(strval(...), $variables);
237216

238217
$execution = $runner->createExecution(
239218
$runtimeId,
@@ -303,16 +282,16 @@ function (
303282
Http::get('/v1/health')
304283
->desc("Get health status")
305284
->inject('response')
306-
->action(function (Response $response) {
285+
->action(function (Response $response): void {
307286
$response->setStatusCode(Response::STATUS_CODE_OK)->text("OK");
308287
});
309288

310289
Http::init()
311290
->groups(['api'])
312291
->inject('request')
313-
->action(function (Request $request) {
292+
->action(function (Request $request): void {
314293
$secretKey = \explode(' ', $request->getHeader('authorization', ''))[1] ?? '';
315-
if (empty($secretKey) || $secretKey !== System::getEnv('OPR_EXECUTOR_SECRET', '')) {
294+
if ($secretKey === '' || $secretKey === '0' || $secretKey !== System::getEnv('OPR_EXECUTOR_SECRET', '')) {
316295
throw new Exception(Exception::GENERAL_UNAUTHORIZED, 'Missing executor key');
317296
}
318297
});

app/error.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Http::error()
99
->inject('error')
1010
->inject('response')
11-
->action(function (Throwable $error, Response $response) {
11+
->action(function (Throwable $error, Response $response): void {
1212
// Show all Executor\Exceptions, or everything if in development
1313
$public = $error instanceof Exception || Http::isDevelopment();
1414
$exception = $public ? $error : new Exception(Exception::GENERAL_UNKNOWN);

app/http.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
->inject('network')
3333
->inject('imagePuller')
3434
->inject('maintenance')
35-
->action(function (Orchestration $orchestration, Network $network, ImagePuller $imagePuller, Maintenance $maintenance) {
35+
->action(function (Orchestration $orchestration, Network $network, ImagePuller $imagePuller, Maintenance $maintenance): void {
3636
/* Fetch own container information */
3737
$hostname = gethostname() ?: throw new \RuntimeException('Could not determine hostname');
3838
$selfContainer = $orchestration->list(['name' => $hostname])[0] ?? throw new \RuntimeException('Own container not found');
@@ -58,11 +58,11 @@
5858

5959
Http::onRequest()
6060
->inject('response')
61-
->action(function (Response $response) {
61+
->action(function (Response $response): void {
6262
$response->addHeader('Server', 'Executor');
6363
});
6464

65-
run(function () use ($settings) {
65+
run(function () use ($settings): void {
6666
$server = new Server('0.0.0.0', '80', $settings);
6767
$http = new Http($server, 'UTC');
6868
$http->start();

app/init.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
use Utopia\Config\Config;
1515

1616
const MAX_LOG_SIZE = 5 * 1024 * 1024;
17-
const MAX_BUILD_LOG_SIZE = 1 * 1000 * 1000;
17+
const MAX_BUILD_LOG_SIZE = 1000 * 1000;
1818

1919
Config::load('errors', __DIR__ . '/config/errors.php');
2020

2121
$registry = new Registry();
2222

23-
$registry->set('runtimes', fn () => new Runtimes());
23+
$registry->set('runtimes', fn (): \OpenRuntimes\Executor\Runner\Repository\Runtimes => new Runtimes());
2424

2525
Http::setResource('runtimes', fn () => $registry->get('runtimes'));
2626

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
"scripts": {
1414
"format": "./vendor/bin/pint --config pint.json",
1515
"format:check": "./vendor/bin/pint --test --config pint.json",
16-
"analyze": "./vendor/bin/phpstan analyse --level 8 --memory-limit=2G -c phpstan.neon app src tests",
16+
"analyze": "./vendor/bin/phpstan analyse --memory-limit=1G -c phpstan.neon app src tests",
1717
"test:unit": "./vendor/bin/phpunit --configuration phpunit.xml --debug --testsuite=unit",
18-
"test:e2e": "./vendor/bin/phpunit --configuration phpunit.xml --debug --testsuite=e2e"
18+
"test:e2e": "./vendor/bin/phpunit --configuration phpunit.xml --debug --testsuite=e2e",
19+
"refactor": "./vendor/bin/rector",
20+
"refactor:check": "./vendor/bin/rector --dry-run"
1921
},
2022
"require": {
2123
"php": ">=8.3.0",
@@ -35,9 +37,10 @@
3537
},
3638
"require-dev": {
3739
"laravel/pint": "1.*",
38-
"phpstan/phpstan": "1.*",
40+
"phpstan/phpstan": "2.*",
3941
"phpunit/phpunit": "9.*",
40-
"swoole/ide-helper": "5.1.2"
42+
"swoole/ide-helper": "5.1.2",
43+
"rector/rector": "2.3.1"
4144
},
4245
"config": {
4346
"platform": {

0 commit comments

Comments
 (0)