Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add statement_indentation config not_for_comments #7963

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions doc/rules/whitespace/statement_indentation.rst
Expand Up @@ -7,6 +7,15 @@ Each statement must be indented.
Configuration
-------------

``not_for_comments``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``not_for_comments``
``skip_single_line_comments``

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like allow_zero_indented_comments better, because that's what it does. I have the occasional two lines of // \PHPStan\dumpType() at line start.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it. Make sure it is only for single line comments starting with //.

~~~~~~~~~~~~~~~~~~~~

Leave commented lines alone.

Allowed types: ``bool``

Default value: ``false``

``stick_comment_to_next_continuous_control_statement``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -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
<?php
if ($foo) {
// Comment stays where it is
-echo "foo"; // Code is indented
+ echo "foo"; // Code is indented
// Comment stays where it is
} else {
$aaa = 1;
}

Rule sets
---------

Expand Down
23 changes: 23 additions & 0 deletions src/Fixer/Whitespace/StatementIndentationFixer.php
Expand Up @@ -89,6 +89,18 @@
',
['stick_comment_to_next_continuous_control_statement' => true]
),
new CodeSample(
'<?php
if ($foo) {
// Comment stays where it is
echo "foo"; // Code is indented
// Comment stays where it is
} else {
$aaa = 1;
}
',
['not_for_comments' => true]

Check warning on line 102 in src/Fixer/Whitespace/StatementIndentationFixer.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 mutation tests

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ } else { $aaa = 1; } -', ['not_for_comments' => true])]); +', [])]); } /** * {@inheritdoc}

Check warning on line 102 in src/Fixer/Whitespace/StatementIndentationFixer.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 mutation tests

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ } else { $aaa = 1; } -', ['not_for_comments' => true])]); +', ['not_for_comments' => false])]); } /** * {@inheritdoc}
),
]
);
}
Expand Down Expand Up @@ -116,6 +128,10 @@
->setAllowedTypes(['bool'])
->setDefault(false)
->getOption(),
(new FixerOptionBuilder('not_for_comments', 'Leave commented lines alone.'))
->setAllowedTypes(['bool'])

Check warning on line 132 in src/Fixer/Whitespace/StatementIndentationFixer.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 mutation tests

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ } protected function createConfigurationDefinition() : FixerConfigurationResolverInterface { - return new FixerConfigurationResolver([(new FixerOptionBuilder('stick_comment_to_next_continuous_control_statement', 'Last comment of code block counts as comment for next block.'))->setAllowedTypes(['bool'])->setDefault(false)->getOption(), (new FixerOptionBuilder('not_for_comments', 'Leave commented lines alone.'))->setAllowedTypes(['bool'])->setDefault(false)->getOption()]); + return new FixerConfigurationResolver([(new FixerOptionBuilder('stick_comment_to_next_continuous_control_statement', 'Last comment of code block counts as comment for next block.'))->setAllowedTypes(['bool'])->setDefault(false)->getOption(), (new FixerOptionBuilder('not_for_comments', 'Leave commented lines alone.'))->setAllowedTypes([])->setDefault(false)->getOption()]); } protected function applyFix(\SplFileInfo $file, Tokens $tokens) : void {
->setDefault(false)
->getOption(),
]);
}

Expand Down Expand Up @@ -437,6 +453,13 @@
}
}

if (true === $this->configuration['not_for_comments']) {
$lineIsCommented = $tokens[$firstNonWhitespaceTokenIndex]->isGivenKind([T_COMMENT]);

Check warning on line 457 in src/Fixer/Whitespace/StatementIndentationFixer.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 mutation tests

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ } } if (true === $this->configuration['not_for_comments']) { - $lineIsCommented = $tokens[$firstNonWhitespaceTokenIndex]->isGivenKind([T_COMMENT]); + $lineIsCommented = $tokens[$firstNonWhitespaceTokenIndex]->isGivenKind([]); if ($lineIsCommented) { continue; }
if ($lineIsCommented) {

Check warning on line 458 in src/Fixer/Whitespace/StatementIndentationFixer.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 mutation tests

Escaped Mutant for Mutator "IfNegation": --- Original +++ New @@ @@ } if (true === $this->configuration['not_for_comments']) { $lineIsCommented = $tokens[$firstNonWhitespaceTokenIndex]->isGivenKind([T_COMMENT]); - if ($lineIsCommented) { + if (!$lineIsCommented) { continue; } }
continue;
}
}

$endIndex = $scopes[$currentScope]['end_index'];

if (!$scopes[$currentScope]['end_index_inclusive']) {
Expand Down