Skip to content

Commit a8d6d6d

Browse files
authored
feat: support boolean shorthand for 'cast_spaces' and add tests (#398)
* feat: support boolean shorthand for 'cast_spaces' and add tests Refs: #397 * chore: minor refactor in normalizeRuleValues * style: minor update code style * style: minor update code style * style: minor update code style
1 parent 7922d30 commit a8d6d6d

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

app/Repositories/ConfigurationJsonRepository.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ protected function get()
8383
$baseConfig = $this->resolveExtend($baseConfig);
8484
}
8585

86+
if (isset($baseConfig['rules'])) {
87+
$baseConfig['rules'] = $this->normalizeRuleValues($baseConfig['rules']);
88+
}
89+
8690
return tap($baseConfig, function ($configuration) {
8791
if (! is_array($configuration)) {
8892
abort(1, sprintf('The configuration file [%s] is not valid JSON.', $this->path));
@@ -93,6 +97,25 @@ protected function get()
9397
return [];
9498
}
9599

100+
/**
101+
* Normalize shorthand rule values into explicit configuration arrays as expected by PHP-CS-Fixer.
102+
*
103+
* @param array<string, mixed> $rules
104+
* @return array<string, mixed>
105+
*/
106+
protected function normalizeRuleValues(array $rules): array
107+
{
108+
if (array_key_exists('cast_spaces', $rules)) {
109+
$rules['cast_spaces'] = match ($rules['cast_spaces']) {
110+
false => ['space' => 'none'],
111+
true => ['space' => 'single'],
112+
default => $rules['cast_spaces'],
113+
};
114+
}
115+
116+
return $rules;
117+
}
118+
96119
/**
97120
* Determine if a local or remote file exists.
98121
*
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"rules": {
3+
"cast_spaces": {
4+
"space": "single"
5+
}
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"cast_spaces": false
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"cast_spaces": true
4+
}
5+
}

tests/Unit/Repositories/ConfigurationJsonRepositoryTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,27 @@
7575

7676
$repository->finder();
7777
})->throws(LogicException::class);
78+
79+
it('normalizes cast_spaces false to none', function () {
80+
$repository = new ConfigurationJsonRepository(dirname(__DIR__, 2).'/Fixtures/rules/pint_cast_spaces_false.json', null);
81+
82+
expect($repository->rules())->toBe([
83+
'cast_spaces' => ['space' => 'none'],
84+
]);
85+
});
86+
87+
it('normalizes cast_spaces true to single', function () {
88+
$repository = new ConfigurationJsonRepository(dirname(__DIR__, 2).'/Fixtures/rules/pint_cast_spaces_true.json', null);
89+
90+
expect($repository->rules())->toBe([
91+
'cast_spaces' => ['space' => 'single'],
92+
]);
93+
});
94+
95+
it('preserves explicit cast_spaces array', function () {
96+
$repository = new ConfigurationJsonRepository(dirname(__DIR__, 2).'/Fixtures/rules/pint_cast_spaces_array.json', null);
97+
98+
expect($repository->rules())->toBe([
99+
'cast_spaces' => ['space' => 'single'],
100+
]);
101+
});

0 commit comments

Comments
 (0)