From b25f454516abd7fdcf4e64629d2e0fa364975128 Mon Sep 17 00:00:00 2001 From: PedroTroller Date: Thu, 5 Jan 2023 10:14:03 +0100 Subject: [PATCH] feat: add inline-attributes option to PedroTroller/line_break_between_method_arguments --- README.md | 11 ++- .../LineBreakBetweenMethodArgumentsFixer.php | 11 +++ src/PedroTroller/CS/Fixer/TokensAnalyzer.php | 28 ++++++ .../Regression/Case7.php | 98 +++++++++++++++++++ .../Regression/Case8.php | 88 +++++++++++++++++ 5 files changed, 232 insertions(+), 4 deletions(-) create mode 100644 tests/UseCase/LineBreakBetweenMethods/Regression/Case7.php create mode 100644 tests/UseCase/LineBreakBetweenMethods/Regression/Case8.php diff --git a/README.md b/README.md index 6b9648a..3fb7c9b 100644 --- a/README.md +++ b/README.md @@ -520,6 +520,9 @@ If the declaration of a method is too long, the arguments of this method MUST BE - `automatic-argument-merge` (*optional*): If both conditions are met (the line is not too long and there are not too many arguments), then the arguments are put back inline - default: `true` + - `inline-attributes` (*optional*): In the case of a split, the declaration of the attributes of the arguments of the method will be on the same line as the arguments themselves + - default: `false` + ### Configuration examples ```php @@ -531,7 +534,7 @@ $config = new PhpCsFixer\Config(); $config->setRules( [ // ... - 'PedroTroller/line_break_between_method_arguments' => [ 'max-args' => 4, 'max-length' => 120, 'automatic-argument-merge' => true ], + 'PedroTroller/line_break_between_method_arguments' => [ 'max-args' => 4, 'max-length' => 120, 'automatic-argument-merge' => true, 'inline-attributes' => true ], // ... ] ); @@ -549,7 +552,7 @@ $config = new PhpCsFixer\Config(); // ... $config->setRules( PedroTroller\CS\Fixer\RuleSetFactory::create() - ->enable('PedroTroller/line_break_between_method_arguments', [ 'max-args' => 4, 'max-length' => 120, 'automatic-argument-merge' => true ]) + ->enable('PedroTroller/line_break_between_method_arguments', [ 'max-args' => 4, 'max-length' => 120, 'automatic-argument-merge' => true, 'inline-attributes' => true ]) ->getRules() ); $config->registerCustomFixers(new PedroTroller\CS\Fixer\Fixers()); @@ -601,7 +604,7 @@ $config = new PhpCsFixer\Config(); $config->setRules( [ // ... - 'PedroTroller/line_break_between_method_arguments' => [ 'max-args' => false, 'max-length' => 120, 'automatic-argument-merge' => true ], + 'PedroTroller/line_break_between_method_arguments' => [ 'max-args' => false, 'max-length' => 120, 'automatic-argument-merge' => true, 'inline-attributes' => true ], // ... ] ); @@ -619,7 +622,7 @@ $config = new PhpCsFixer\Config(); // ... $config->setRules( PedroTroller\CS\Fixer\RuleSetFactory::create() - ->enable('PedroTroller/line_break_between_method_arguments', [ 'max-args' => false, 'max-length' => 120, 'automatic-argument-merge' => true ]) + ->enable('PedroTroller/line_break_between_method_arguments', [ 'max-args' => false, 'max-length' => 120, 'automatic-argument-merge' => true, 'inline-attributes' => true ]) ->getRules() ); $config->registerCustomFixers(new PedroTroller\CS\Fixer\Fixers()); diff --git a/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php b/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php index 208ef3c..655cd26 100644 --- a/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php +++ b/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php @@ -32,11 +32,13 @@ public function getSampleConfigurations(): array 'max-args' => 4, 'max-length' => 120, 'automatic-argument-merge' => true, + 'inline-attributes' => true, ], [ 'max-args' => false, 'max-length' => 120, 'automatic-argument-merge' => true, + 'inline-attributes' => true, ], ]; } @@ -87,6 +89,9 @@ public function getConfigurationDefinition(): FixerConfigurationResolverInterfac (new FixerOptionBuilder('automatic-argument-merge', 'If both conditions are met (the line is not too long and there are not too many arguments), then the arguments are put back inline')) ->setDefault(true) ->getOption(), + (new FixerOptionBuilder('inline-attributes', 'In the case of a split, the declaration of the attributes of the arguments of the method will be on the same line as the arguments themselves')) + ->setDefault(false) + ->getOption(), ]); } @@ -188,6 +193,12 @@ private function splitArgs(Tokens $tokens, $index): void if ($tokens[$i]->equals(',')) { $linebreaks[] = $i; } + + if (false === $this->configuration['inline-attributes'] && $tokens[$i]->isGivenKind(T_ATTRIBUTE)) { + $i = $this->analyze($tokens)->getClosingAttribute($i); + + $linebreaks[] = $i; + } } sort($linebreaks); diff --git a/src/PedroTroller/CS/Fixer/TokensAnalyzer.php b/src/PedroTroller/CS/Fixer/TokensAnalyzer.php index 0c4506a..a7c2016 100644 --- a/src/PedroTroller/CS/Fixer/TokensAnalyzer.php +++ b/src/PedroTroller/CS/Fixer/TokensAnalyzer.php @@ -394,6 +394,34 @@ public function getClosingCurlyBracket($index) } } + /** + * @param int $index + * + * @return null|int + */ + public function getClosingAttribute($index) + { + if (false === $this->tokens[$index]->isGivenKind(T_ATTRIBUTE)) { + throw new Exception(sprintf('Expected token: T_ATTRIBUTE Token %d id contains %s.', $index, $this->tokens[$index]->getContent())); + } + + for ($i = $index + 1; $i < $this->tokens->count(); ++$i) { + if ($this->tokens[$i]->isGivenKind(T_ATTRIBUTE)) { + $i = $this->getClosingAttribute($i); + + if (null === $i) { + return null; + } + + continue; + } + + if ($this->tokens[$i]->isGivenKind(CT::T_ATTRIBUTE_CLOSE)) { + return $i; + } + } + } + /** * @param int $index * diff --git a/tests/UseCase/LineBreakBetweenMethods/Regression/Case7.php b/tests/UseCase/LineBreakBetweenMethods/Regression/Case7.php new file mode 100644 index 0000000..e798277 --- /dev/null +++ b/tests/UseCase/LineBreakBetweenMethods/Regression/Case7.php @@ -0,0 +1,98 @@ +configure([ + 'inline-attributes' => false, + ]); + + yield $fixer; + } + + public function getRawScript(): string + { + return <<<'PHP' + configure([ + 'inline-attributes' => true, + ]); + + yield $fixer; + } + + public function getRawScript(): string + { + return <<<'PHP' +