From d254473be2253912741e9ce062df31c7018213d2 Mon Sep 17 00:00:00 2001 From: artosepyan Date: Tue, 9 Sep 2025 23:13:28 +0300 Subject: [PATCH 1/5] feat: support boolean shorthand for 'cast_spaces' and add tests Refs: https://github.com/laravel/pint/issues/397 --- .../ConfigurationJsonRepository.php | 23 ++++++++++++++++++ .../rules/pint_cast_spaces_array.json | 7 ++++++ .../rules/pint_cast_spaces_false.json | 5 ++++ .../Fixtures/rules/pint_cast_spaces_true.json | 5 ++++ .../ConfigurationJsonRepositoryTest.php | 24 +++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 tests/Fixtures/rules/pint_cast_spaces_array.json create mode 100644 tests/Fixtures/rules/pint_cast_spaces_false.json create mode 100644 tests/Fixtures/rules/pint_cast_spaces_true.json diff --git a/app/Repositories/ConfigurationJsonRepository.php b/app/Repositories/ConfigurationJsonRepository.php index c9924286..af79c18c 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']) && is_array($baseConfig['rules'])) { + $baseConfig['rules'] = $this->normalizeRules($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 fixer option shapes. + * + * @param array $rules + * @return array + */ + private function normalizeRules(array $rules): array + { + if (array_key_exists('cast_spaces', $rules)) { + if ($rules['cast_spaces'] === false) { + $rules['cast_spaces'] = ['space' => 'none']; + } elseif ($rules['cast_spaces'] === true) { + $rules['cast_spaces'] = ['space' => 'single']; + } + } + + 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'], + ]); +}); From b5fdc8ee80ecaf8638c707c2ed9aedd07a5e88ac Mon Sep 17 00:00:00 2001 From: artosepyan Date: Wed, 10 Sep 2025 15:30:54 +0300 Subject: [PATCH 2/5] chore: minor refactor in normalizeRuleValues --- .../ConfigurationJsonRepository.php | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/app/Repositories/ConfigurationJsonRepository.php b/app/Repositories/ConfigurationJsonRepository.php index af79c18c..585d680d 100644 --- a/app/Repositories/ConfigurationJsonRepository.php +++ b/app/Repositories/ConfigurationJsonRepository.php @@ -83,8 +83,8 @@ protected function get() $baseConfig = $this->resolveExtend($baseConfig); } - if (isset($baseConfig['rules']) && is_array($baseConfig['rules'])) { - $baseConfig['rules'] = $this->normalizeRules($baseConfig['rules']); + if (isset($baseConfig['rules'])) { + $baseConfig['rules'] = $this->normalizeRuleValues($baseConfig['rules']); } return tap($baseConfig, function ($configuration) { @@ -98,19 +98,20 @@ protected function get() } /** - * Normalize shorthand rule values into explicit fixer option shapes. - * + * Normalize shorthand rule values into explicit configuration arrays + * as expected by PHP-CS-Fixer. + * * @param array $rules * @return array */ - private function normalizeRules(array $rules): array + protected function normalizeRuleValues(array $rules): array { if (array_key_exists('cast_spaces', $rules)) { - if ($rules['cast_spaces'] === false) { - $rules['cast_spaces'] = ['space' => 'none']; - } elseif ($rules['cast_spaces'] === true) { - $rules['cast_spaces'] = ['space' => 'single']; - } + $rules['cast_spaces'] = match ($rules['cast_spaces']) { + false => ['space' => 'none'], + true => ['space' => 'single'], + default => $rules['cast_spaces'], + }; } return $rules; From f9a22dac651db02083122a41e9f25daae3d035fd Mon Sep 17 00:00:00 2001 From: artosepyan Date: Wed, 10 Sep 2025 15:39:32 +0300 Subject: [PATCH 3/5] style: minor update code style --- app/Repositories/ConfigurationJsonRepository.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Repositories/ConfigurationJsonRepository.php b/app/Repositories/ConfigurationJsonRepository.php index 585d680d..170594a3 100644 --- a/app/Repositories/ConfigurationJsonRepository.php +++ b/app/Repositories/ConfigurationJsonRepository.php @@ -98,8 +98,7 @@ protected function get() } /** - * Normalize shorthand rule values into explicit configuration arrays - * as expected by PHP-CS-Fixer. + * Normalize shorthand rule values into explicit configuration arrays as expected by PHP-CS-Fixer. * * @param array $rules * @return array From 64dae259f011f9d7ba685e5ae2632ebbf6c19967 Mon Sep 17 00:00:00 2001 From: artosepyan Date: Wed, 10 Sep 2025 15:41:50 +0300 Subject: [PATCH 4/5] style: minor update code style --- app/Repositories/ConfigurationJsonRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Repositories/ConfigurationJsonRepository.php b/app/Repositories/ConfigurationJsonRepository.php index 170594a3..5514b5d5 100644 --- a/app/Repositories/ConfigurationJsonRepository.php +++ b/app/Repositories/ConfigurationJsonRepository.php @@ -99,7 +99,7 @@ protected function get() /** * Normalize shorthand rule values into explicit configuration arrays as expected by PHP-CS-Fixer. - * + * * @param array $rules * @return array */ From d10669bcd99396e04548697b15cffa69c14e2a98 Mon Sep 17 00:00:00 2001 From: artosepyan Date: Wed, 10 Sep 2025 15:47:37 +0300 Subject: [PATCH 5/5] style: minor update code style --- app/Repositories/ConfigurationJsonRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Repositories/ConfigurationJsonRepository.php b/app/Repositories/ConfigurationJsonRepository.php index 5514b5d5..43359a56 100644 --- a/app/Repositories/ConfigurationJsonRepository.php +++ b/app/Repositories/ConfigurationJsonRepository.php @@ -108,7 +108,7 @@ 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'], + true => ['space' => 'single'], default => $rules['cast_spaces'], }; }