From 6639d2c789e48fe5e40c9c3c55a15018f387ea37 Mon Sep 17 00:00:00 2001 From: Kei Date: Sun, 28 Jul 2024 09:57:44 +0700 Subject: [PATCH] `trailing_comma_in_multiline` following `PER-CS2.0` spec --- src/Config.php | 2 +- src/RuleSet/Sets/Relax.php | 1 + tests/Fixtures/Ruleset/relax_actual.php | 39 +++++++++++++++++++- tests/Fixtures/Ruleset/relax_expected.php | 45 ++++++++++++++++++++--- tests/Integration/IntegrationTestCase.php | 2 +- tests/Unit/ConfigTest.php | 8 ++-- tests/Unit/ValidRulesTest.php | 2 +- 7 files changed, 85 insertions(+), 14 deletions(-) diff --git a/src/Config.php b/src/Config.php index eaea210..53deb87 100644 --- a/src/Config.php +++ b/src/Config.php @@ -47,7 +47,7 @@ public static function create($ruleSet = null) { if (! $ruleSet instanceof RuleSetInterface && ! is_string($ruleSet) && $ruleSet !== null) { throw new \InvalidArgumentException( - 'Ruleset must be of type Relax RuleSetInterface, string or null' + 'Ruleset must be of type Relax RuleSetInterface, string or null', ); } diff --git a/src/RuleSet/Sets/Relax.php b/src/RuleSet/Sets/Relax.php index 7d59a60..8a4f2f9 100644 --- a/src/RuleSet/Sets/Relax.php +++ b/src/RuleSet/Sets/Relax.php @@ -71,6 +71,7 @@ public function mainRules(): array ], ], 'space_after_semicolon' => ['remove_in_empty_for_expressions' => true], + 'trailing_comma_in_multiline' => ['after_heredoc' => true, 'elements' => ['arguments', 'arrays', 'match', 'parameters']], 'unary_operator_spaces' => ['only_dec_inc' => true], 'whitespace_after_comma_in_array' => ['ensure_single_space' => true], 'phpdoc_align' => ['tags' => ['method', 'param', 'property', 'throws', 'type', 'var']], diff --git a/tests/Fixtures/Ruleset/relax_actual.php b/tests/Fixtures/Ruleset/relax_actual.php index 5351b03..b38c952 100644 --- a/tests/Fixtures/Ruleset/relax_actual.php +++ b/tests/Fixtures/Ruleset/relax_actual.php @@ -234,9 +234,9 @@ public function control_structure__no_useless_else() public function control_structure__switch_case_semicolon_to_colon() { switch (true) { - case 1; + case 1: break; - default; + default: break; } } @@ -710,6 +710,41 @@ public function array_notation__whitespace_after_comma_in_array() $sample = [1,2, 3, 4, 5]; } + public function control_structure__trailing_comma_in_multiline() + { + // array + [ + 1, + 2 + ]; + + // arguments + foo( + 1, + 2 + ); + + // parameters + bar( + 1, + 2 + ); + + // match + match (true) { + 1 => '1', + 2 => '2' + }; + + // after_heredoc + [ + 'foo', + <<<'EOD' + bar + EOD + ]; + } + public function function_notation__function_declaration() { // closure_fn_spacing diff --git a/tests/Fixtures/Ruleset/relax_expected.php b/tests/Fixtures/Ruleset/relax_expected.php index 0c28a73..cd7efda 100644 --- a/tests/Fixtures/Ruleset/relax_expected.php +++ b/tests/Fixtures/Ruleset/relax_expected.php @@ -22,7 +22,7 @@ class relax_actual extends Config public function __invoke(array $type_declaration_spaces) {} public function __construct( - ?RuleSetInterface $ruleSet + ?RuleSetInterface $ruleSet, ) {} /** @@ -648,7 +648,7 @@ public function whitespace__no_extra_blank_lines_2() // parenthesis_brace_block $foo = is_string( - 'foo' + 'foo', ); // square_brace_block @@ -688,6 +688,41 @@ public function array_notation__whitespace_after_comma_in_array() $sample = [1, 2, 3, 4, 5]; } + public function control_structure__trailing_comma_in_multiline() + { + // array + [ + 1, + 2, + ]; + + // arguments + foo( + 1, + 2, + ); + + // parameters + bar( + 1, + 2, + ); + + // match + match (true) { + 1 => '1', + 2 => '2', + }; + + // after_heredoc + [ + 'foo', + <<<'EOD' + bar + EOD, + ]; + } + public function function_notation__function_declaration() { // closure_fn_spacing @@ -709,11 +744,11 @@ function sample($a = 10, function sample2( $a = 10, $b = 20, - $c = 30 + $c = 30, ) {} sample2( 1, - 2 + 2, ); // 'after_heredoc' => true @@ -721,7 +756,7 @@ function sample2( <<<'EOD' foo EOD, - 'bar' + 'bar', ); // Default value diff --git a/tests/Integration/IntegrationTestCase.php b/tests/Integration/IntegrationTestCase.php index 56d275f..b23607d 100644 --- a/tests/Integration/IntegrationTestCase.php +++ b/tests/Integration/IntegrationTestCase.php @@ -53,7 +53,7 @@ protected function runFixer(string $name): bool $config = "tests/Integration/Config/config_{$name}.php"; $result = $application->run( new StringInput("fix --config={$config} --quiet"), - new \Symfony\Component\Console\Output\BufferedOutput + new \Symfony\Component\Console\Output\BufferedOutput, ); return $result === 0; diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php index 4932793..8e5c31e 100644 --- a/tests/Unit/ConfigTest.php +++ b/tests/Unit/ConfigTest.php @@ -19,7 +19,7 @@ public function testSetRuleset(): void $localRules = ['foo' => 'bar']; $this->assertSame( count($ruleset->rules()) + count($localRules), - count($config->setRules($localRules)->getRules()) + count($config->setRules($localRules)->getRules()), ); } @@ -34,7 +34,7 @@ public function testSetRulesetWithStringInput(): void $localRules = ['foo' => 'bar']; $this->assertSame( count($ruleset->rules()) + count($localRules), - count($config->setRules($localRules)->getRules()) + count($config->setRules($localRules)->getRules()), ); } @@ -54,8 +54,8 @@ public function testAddLocalRules(): void count($rules1) + count($rules2), count( Config::create(new RuleSetFile) - ->setRules($rules2)->getRules() - ) + ->setRules($rules2)->getRules(), + ), ); } diff --git a/tests/Unit/ValidRulesTest.php b/tests/Unit/ValidRulesTest.php index 616f9f9..07ccbc9 100644 --- a/tests/Unit/ValidRulesTest.php +++ b/tests/Unit/ValidRulesTest.php @@ -42,7 +42,7 @@ public function testThatThereIsNoDeprecatedFixerInRuleSet($setName, $ruleName): $this->assertNotInstanceOf( \PhpCsFixer\Fixer\DeprecatedFixerInterface::class, $fixer, - \sprintf('RuleSet "%s" contains deprecated rule "%s".', $setName, $ruleName) + \sprintf('RuleSet "%s" contains deprecated rule "%s".', $setName, $ruleName), ); }