From 481ba4dfd1bb8df76a884d0a3a3c5a7953633a96 Mon Sep 17 00:00:00 2001 From: Rudie Dirkx Date: Mon, 22 Apr 2024 03:43:19 +0200 Subject: [PATCH 1/3] +statement_indentation config `not_for_comments` --- src/Fixer/Whitespace/StatementIndentationFixer.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Fixer/Whitespace/StatementIndentationFixer.php b/src/Fixer/Whitespace/StatementIndentationFixer.php index ca448fb4305..26ddb371966 100644 --- a/src/Fixer/Whitespace/StatementIndentationFixer.php +++ b/src/Fixer/Whitespace/StatementIndentationFixer.php @@ -116,6 +116,10 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn ->setAllowedTypes(['bool']) ->setDefault(false) ->getOption(), + (new FixerOptionBuilder('not_for_comments', 'Leave commented lines alone.')) + ->setAllowedTypes(['bool']) + ->setDefault(false) + ->getOption(), ]); } @@ -437,6 +441,13 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void } } + if (true === $this->configuration['not_for_comments']) { + $lineIsCommented = $tokens[$firstNonWhitespaceTokenIndex]->isGivenKind([T_COMMENT]); + if ($lineIsCommented) { + continue; + } + } + $endIndex = $scopes[$currentScope]['end_index']; if (!$scopes[$currentScope]['end_index_inclusive']) { From e06a26e0bbed0f0063c8f6d778c749a2165d852c Mon Sep 17 00:00:00 2001 From: Rudie Dirkx Date: Mon, 22 Apr 2024 04:40:23 +0200 Subject: [PATCH 2/3] statement_indentation documentation --- .../whitespace/statement_indentation.rst | 28 +++++++++++++++++++ .../Whitespace/StatementIndentationFixer.php | 12 ++++++++ 2 files changed, 40 insertions(+) diff --git a/doc/rules/whitespace/statement_indentation.rst b/doc/rules/whitespace/statement_indentation.rst index fd7733e58cd..b87a46755ed 100644 --- a/doc/rules/whitespace/statement_indentation.rst +++ b/doc/rules/whitespace/statement_indentation.rst @@ -7,6 +7,15 @@ Each statement must be indented. Configuration ------------- +``not_for_comments`` +~~~~~~~~~~~~~~~~~~~~ + +Leave commented lines alone. + +Allowed types: ``bool`` + +Default value: ``false`` + ``stick_comment_to_next_continuous_control_statement`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -85,6 +94,25 @@ With configuration: ``['stick_comment_to_next_continuous_control_statement' => t + // this is treated as comment of `elseif(3)` block, as it is a comment in the final block } +Example #4 +~~~~~~~~~~ + +With configuration: ``['not_for_comments' => true]``. + +.. code-block:: diff + + --- Original + +++ New + true] ), + new CodeSample( + ' true] + ), ] ); } From 081423bea61ffa179e7a73df63b7395d9849972d Mon Sep 17 00:00:00 2001 From: Rudie Dirkx Date: Tue, 23 Apr 2024 19:15:56 +0200 Subject: [PATCH 3/3] Instead do allow_zero_indented_comments --- doc/rules/whitespace/statement_indentation.rst | 17 +++++++++-------- .../Whitespace/StatementIndentationFixer.php | 15 ++++++++------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/doc/rules/whitespace/statement_indentation.rst b/doc/rules/whitespace/statement_indentation.rst index b87a46755ed..f54b411e50f 100644 --- a/doc/rules/whitespace/statement_indentation.rst +++ b/doc/rules/whitespace/statement_indentation.rst @@ -7,10 +7,10 @@ Each statement must be indented. Configuration ------------- -``not_for_comments`` -~~~~~~~~~~~~~~~~~~~~ +``allow_zero_indented_comments`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Leave commented lines alone. +Leave lines that start with a // comment alone. Allowed types: ``bool`` @@ -97,7 +97,7 @@ With configuration: ``['stick_comment_to_next_continuous_control_statement' => t Example #4 ~~~~~~~~~~ -With configuration: ``['not_for_comments' => true]``. +With configuration: ``['allow_zero_indented_comments' => true]``. .. code-block:: diff @@ -105,10 +105,11 @@ With configuration: ``['not_for_comments' => true]``. +++ New true] + ['allow_zero_indented_comments' => true] ), ] ); @@ -128,7 +128,7 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn ->setAllowedTypes(['bool']) ->setDefault(false) ->getOption(), - (new FixerOptionBuilder('not_for_comments', 'Leave commented lines alone.')) + (new FixerOptionBuilder('allow_zero_indented_comments', 'Leave lines that start with a // comment alone.')) ->setAllowedTypes(['bool']) ->setDefault(false) ->getOption(), @@ -453,9 +453,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void } } - if (true === $this->configuration['not_for_comments']) { + if (true === $this->configuration['allow_zero_indented_comments']) { $lineIsCommented = $tokens[$firstNonWhitespaceTokenIndex]->isGivenKind([T_COMMENT]); - if ($lineIsCommented) { + $prevTokenEndsWithNewline = Preg::match('/\R$/', $tokens[$firstNonWhitespaceTokenIndex - 1]->getContent()); + if ($lineIsCommented && $prevTokenEndsWithNewline) { continue; } }