diff --git a/app/Repositories/ConfigurationJsonRepository.php b/app/Repositories/ConfigurationJsonRepository.php index c9924286..43359a56 100644 --- a/app/Repositories/ConfigurationJsonRepository.php +++ b/app/Repositories/ConfigurationJsonRepository.php @@ -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)); @@ -93,6 +97,25 @@ protected function get() return []; } + /** + * Normalize shorthand rule values into explicit configuration arrays as expected by PHP-CS-Fixer. + * + * @param array $rules + * @return array + */ + 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. * diff --git a/tests/Fixtures/rules/pint_cast_spaces_array.json b/tests/Fixtures/rules/pint_cast_spaces_array.json new file mode 100644 index 00000000..4da2e502 --- /dev/null +++ b/tests/Fixtures/rules/pint_cast_spaces_array.json @@ -0,0 +1,7 @@ +{ + "rules": { + "cast_spaces": { + "space": "single" + } + } +} diff --git a/tests/Fixtures/rules/pint_cast_spaces_false.json b/tests/Fixtures/rules/pint_cast_spaces_false.json new file mode 100644 index 00000000..0347f819 --- /dev/null +++ b/tests/Fixtures/rules/pint_cast_spaces_false.json @@ -0,0 +1,5 @@ +{ + "rules": { + "cast_spaces": false + } +} diff --git a/tests/Fixtures/rules/pint_cast_spaces_true.json b/tests/Fixtures/rules/pint_cast_spaces_true.json new file mode 100644 index 00000000..1b412f4e --- /dev/null +++ b/tests/Fixtures/rules/pint_cast_spaces_true.json @@ -0,0 +1,5 @@ +{ + "rules": { + "cast_spaces": true + } +} diff --git a/tests/Unit/Repositories/ConfigurationJsonRepositoryTest.php b/tests/Unit/Repositories/ConfigurationJsonRepositoryTest.php index b6d6bae0..d2df5c5e 100644 --- a/tests/Unit/Repositories/ConfigurationJsonRepositoryTest.php +++ b/tests/Unit/Repositories/ConfigurationJsonRepositoryTest.php @@ -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'], + ]); +});