Skip to content

Commit 1e377d8

Browse files
committed
Refactor to use context for environment handling.
Replaced direct environment arrays with the `context` API for better code readability and consistency. This enhances maintainability by centralizing environment management across all task commands.
1 parent dbe5071 commit 1e377d8

File tree

1 file changed

+62
-39
lines changed

1 file changed

+62
-39
lines changed

castor.php

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Castor\Attribute\AsTask;
6+
use function Castor\context;
67
use function Castor\io;
78
use function Castor\run;
89

@@ -27,32 +28,40 @@ function infect(int $minMsi = 0, int $minCoveredMsi = 0, bool $ci = false): void
2728
$command[] = '--logger-github';
2829
$command[] = '-s';
2930
}
30-
$environment = [
31-
'XDEBUG_MODE' => 'coverage',
32-
];
33-
run($command, environment: $environment);
31+
$context = context()
32+
->withEnvironment([
33+
'XDEBUG_MODE' => 'coverage',
34+
])
35+
;
36+
run($command, context: $context);
3437
}
3538

3639
#[AsTask(description: 'Run tests')]
3740
function test(bool $coverageHtml = false, bool $coverageText = false, null|string $group = null): void
3841
{
3942
io()->title('Running tests');
4043
$command = ['php', 'vendor/bin/phpunit', '--color'];
41-
$environment = [
42-
'XDEBUG_MODE' => 'off',
43-
];
44+
$context = context()
45+
->withEnvironment([
46+
'XDEBUG_MODE' => 'off',
47+
])
48+
;
4449
if ($coverageHtml) {
4550
$command[] = '--coverage-html=build/coverage';
46-
$environment['XDEBUG_MODE'] = 'coverage';
51+
$context = $context->withEnvironment([
52+
'XDEBUG_MODE' => 'coverage',
53+
]);
4754
}
4855
if ($coverageText) {
4956
$command[] = '--coverage-text';
50-
$environment['XDEBUG_MODE'] = 'coverage';
57+
$context = $context->withEnvironment([
58+
'XDEBUG_MODE' => 'coverage',
59+
]);
5160
}
5261
if ($group !== null) {
5362
$command[] = sprintf('--group=%s', $group);
5463
}
55-
run($command, environment: $environment);
64+
run($command, context: $context);
5665
}
5766

5867
#[AsTask(description: 'Coding standards check')]
@@ -64,16 +73,18 @@ function cs(
6473
): void {
6574
io()->title('Running coding standards check');
6675
$command = ['php', 'vendor/bin/ecs', 'check'];
67-
$environment = [
68-
'XDEBUG_MODE' => 'off',
69-
];
76+
$context = context()
77+
->withEnvironment([
78+
'XDEBUG_MODE' => 'off',
79+
])
80+
;
7081
if ($fix) {
7182
$command[] = '--fix';
7283
}
7384
if ($clearCache) {
7485
$command[] = '--clear-cache';
7586
}
76-
run($command, environment: $environment);
87+
run($command, context: $context);
7788
}
7889

7990
#[AsTask(description: 'Running PHPStan')]
@@ -84,24 +95,28 @@ function stan(bool $baseline = false): void
8495
if ($baseline) {
8596
$command[] = '--generate-baseline';
8697
}
87-
$environment = [
88-
'XDEBUG_MODE' => 'off',
89-
];
90-
run($command, environment: $environment);
98+
$context = context()
99+
->withEnvironment([
100+
'XDEBUG_MODE' => 'off',
101+
])
102+
;
103+
run($command, context: $context);
91104
}
92105

93106
#[AsTask(description: 'Validate Composer configuration')]
94107
function validate(): void
95108
{
96109
io()->title('Validating Composer configuration');
97110
$command = ['composer', 'validate', '--strict'];
98-
$environment = [
99-
'XDEBUG_MODE' => 'off',
100-
];
101-
run($command, environment: $environment);
111+
$context = context()
112+
->withEnvironment([
113+
'XDEBUG_MODE' => 'off',
114+
])
115+
;
116+
run($command, context: $context);
102117

103118
$command = ['composer', 'dump-autoload', '--optimize', '--strict-psr'];
104-
run($command, environment: $environment);
119+
run($command, context: $context);
105120
}
106121

107122
/**
@@ -114,10 +129,12 @@ function checkLicenses(
114129
io()->title('Checking licenses');
115130
$allowedExceptions = [];
116131
$command = ['composer', 'licenses', '-f', 'json'];
117-
$environment = [
118-
'XDEBUG_MODE' => 'off',
119-
];
120-
$result = run($command, environment: $environment, quiet: true);
132+
$context = context()
133+
->withEnvironment([
134+
'XDEBUG_MODE' => 'off',
135+
])
136+
;
137+
$result = run($command, context: $context, quiet: true);
121138
if (! $result->isSuccessful()) {
122139
io()->error('Cannot determine licenses');
123140
exit(1);
@@ -177,30 +194,36 @@ function rector(
177194
if ($clearCache) {
178195
$command[] = '--clear-cache';
179196
}
180-
$environment = [
181-
'XDEBUG_MODE' => 'off',
182-
];
183-
run($command, environment: $environment);
197+
$context = context()
198+
->withEnvironment([
199+
'XDEBUG_MODE' => 'off',
200+
])
201+
;
202+
run($command, context: $context);
184203
}
185204

186205
#[AsTask(description: 'Run Rector')]
187206
function deptrac(): void
188207
{
189208
io()->title('Running Rector');
190209
$command = ['php', 'vendor/bin/deptrac', 'analyse', '--fail-on-uncovered', '--no-cache'];
191-
$environment = [
192-
'XDEBUG_MODE' => 'off',
193-
];
194-
run($command, environment: $environment);
210+
$context = context()
211+
->withEnvironment([
212+
'XDEBUG_MODE' => 'off',
213+
])
214+
;
215+
run($command, context: $context);
195216
}
196217

197218
#[AsTask(description: 'Run Linter')]
198219
function lint(): void
199220
{
200221
io()->title('Running Linter');
201222
$command = ['composer', 'exec', '--', 'parallel-lint', __DIR__ . '/src/', __DIR__ . '/tests/'];
202-
$environment = [
203-
'XDEBUG_MODE' => 'off',
204-
];
205-
run($command, environment: $environment);
223+
$context = context()
224+
->withEnvironment([
225+
'XDEBUG_MODE' => 'off',
226+
])
227+
;
228+
run($command, context: $context);
206229
}

0 commit comments

Comments
 (0)