Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/Repositories/ConfigurationJsonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ protected function get()
$baseConfig = $this->resolveExtend($baseConfig);
}

if (isset($baseConfig['rules'])) {
$baseConfig['rules'] = $this->normalizeRuleValues($baseConfig['rules']);
}

return tap($baseConfig, function ($configuration) {
if (! is_array($configuration)) {
abort(1, sprintf('The configuration file [%s] is not valid JSON.', $this->path));
Expand All @@ -93,6 +97,25 @@ protected function get()
return [];
}

/**
* Normalize shorthand rule values into explicit configuration arrays as expected by PHP-CS-Fixer.
*
* @param array<string, mixed> $rules
* @return array<string, mixed>
*/
protected function normalizeRuleValues(array $rules): array
{
if (array_key_exists('cast_spaces', $rules)) {
$rules['cast_spaces'] = match ($rules['cast_spaces']) {
false => ['space' => 'none'],
true => ['space' => 'single'],
default => $rules['cast_spaces'],
};
}

return $rules;
}

/**
* Determine if a local or remote file exists.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/rules/pint_cast_spaces_array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"rules": {
"cast_spaces": {
"space": "single"
}
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/rules/pint_cast_spaces_false.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"cast_spaces": false
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/rules/pint_cast_spaces_true.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"cast_spaces": true
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Repositories/ConfigurationJsonRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,27 @@

$repository->finder();
})->throws(LogicException::class);

it('normalizes cast_spaces false to none', function () {
$repository = new ConfigurationJsonRepository(dirname(__DIR__, 2).'/Fixtures/rules/pint_cast_spaces_false.json', null);

expect($repository->rules())->toBe([
'cast_spaces' => ['space' => 'none'],
]);
});

it('normalizes cast_spaces true to single', function () {
$repository = new ConfigurationJsonRepository(dirname(__DIR__, 2).'/Fixtures/rules/pint_cast_spaces_true.json', null);

expect($repository->rules())->toBe([
'cast_spaces' => ['space' => 'single'],
]);
});

it('preserves explicit cast_spaces array', function () {
$repository = new ConfigurationJsonRepository(dirname(__DIR__, 2).'/Fixtures/rules/pint_cast_spaces_array.json', null);

expect($repository->rules())->toBe([
'cast_spaces' => ['space' => 'single'],
]);
});