diff --git a/doc/rules/whitespace/statement_indentation.rst b/doc/rules/whitespace/statement_indentation.rst index fd7733e58cd..f54b411e50f 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 ------------- +``allow_zero_indented_comments`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Leave lines that start with a // comment alone. + +Allowed types: ``bool`` + +Default value: ``false`` + ``stick_comment_to_next_continuous_control_statement`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -85,6 +94,26 @@ 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: ``['allow_zero_indented_comments' => true]``. + +.. code-block:: diff + + --- Original + +++ New + true] ), + new CodeSample( + ' true] + ), ] ); } @@ -116,6 +128,10 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn ->setAllowedTypes(['bool']) ->setDefault(false) ->getOption(), + (new FixerOptionBuilder('allow_zero_indented_comments', 'Leave lines that start with a // comment alone.')) + ->setAllowedTypes(['bool']) + ->setDefault(false) + ->getOption(), ]); } @@ -437,6 +453,14 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void } } + if (true === $this->configuration['allow_zero_indented_comments']) { + $lineIsCommented = $tokens[$firstNonWhitespaceTokenIndex]->isGivenKind([T_COMMENT]); + $prevTokenEndsWithNewline = Preg::match('/\R$/', $tokens[$firstNonWhitespaceTokenIndex - 1]->getContent()); + if ($lineIsCommented && $prevTokenEndsWithNewline) { + continue; + } + } + $endIndex = $scopes[$currentScope]['end_index']; if (!$scopes[$currentScope]['end_index_inclusive']) {