Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #6274 SingleLineCommentSpacingFixer - Introduction (SpacePossum)
This PR was merged into the master branch. Discussion ---------- SingleLineCommentSpacingFixer - Introduction Commits ------- 701b30e SingleLineCommentSpacingFixer - Introduction
- Loading branch information
Showing
11 changed files
with
331 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
==================================== | ||
Rule ``single_line_comment_spacing`` | ||
==================================== | ||
|
||
Single-line comments must have proper spacing. | ||
|
||
Examples | ||
-------- | ||
|
||
Example #1 | ||
~~~~~~~~~~ | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
<?php | ||
-//comment 1 | ||
-#comment 2 | ||
-/*comment 3*/ | ||
+// comment 1 | ||
+# comment 2 | ||
+/* comment 3 */ | ||
Rule sets | ||
--------- | ||
|
||
The rule is part of the following rule sets: | ||
|
||
@PhpCsFixer | ||
Using the `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ rule set will enable the ``single_line_comment_spacing`` rule. | ||
|
||
@Symfony | ||
Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``single_line_comment_spacing`` rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Fixer\Comment; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\Preg; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
final class SingleLineCommentSpacingFixer extends AbstractFixer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'Single-line comments must have proper spacing.', | ||
[ | ||
new CodeSample( | ||
'<?php | ||
//comment 1 | ||
#comment 2 | ||
/*comment 3*/ | ||
' | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Must run after PhpdocToCommentFixer. | ||
*/ | ||
public function getPriority(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return $tokens->isTokenKindFound(T_COMMENT); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
for ($index = \count($tokens) - 1; 0 <= $index; --$index) { | ||
$token = $tokens[$index]; | ||
|
||
if (!$token->isGivenKind(T_COMMENT)) { | ||
continue; | ||
} | ||
|
||
$content = $token->getContent(); | ||
$contentLength = \strlen($content); | ||
|
||
if ('/' === $content[0]) { | ||
if ($contentLength < 3) { | ||
continue; // cheap check for "//" | ||
} | ||
|
||
if ('*' === $content[1]) { // slash asterisk comment | ||
if ($contentLength < 5 || '*' === $content[2] || str_contains($content, "\n")) { | ||
continue; // cheap check for "/**/", comment that looks like a PHPDoc, or multi line comment | ||
} | ||
|
||
$newContent = rtrim(substr($content, 0, -2)).' '.substr($content, -2); | ||
$newContent = $this->fixCommentLeadingSpace($newContent, '/*'); | ||
} else { // double slash comment | ||
$newContent = $this->fixCommentLeadingSpace($content, '//'); | ||
} | ||
} else { // hash comment | ||
if ($contentLength < 2 || '[' === $content[1]) { // cheap check for "#" or annotation (like) comment | ||
continue; | ||
} | ||
|
||
$newContent = $this->fixCommentLeadingSpace($content, '#'); | ||
} | ||
|
||
if ($newContent !== $content) { | ||
$tokens[$index] = new Token([T_COMMENT, $newContent]); | ||
} | ||
} | ||
} | ||
|
||
// fix space between comment open and leading text | ||
private function fixCommentLeadingSpace(string $content, string $prefix): string | ||
{ | ||
if (0 !== Preg::match(sprintf('@^%s\h+.*$@', preg_quote($prefix, '@')), $content)) { | ||
return $content; | ||
} | ||
|
||
$position = \strlen($prefix); | ||
|
||
return substr($content, 0, $position).' '.substr($content, $position); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
tests/Fixer/Comment/SingleLineCommentSpacingFixerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Tests\Fixer\Comment; | ||
|
||
use PhpCsFixer\Tests\Test\AbstractFixerTestCase; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \PhpCsFixer\Fixer\Comment\SingleLineCommentSpacingFixer | ||
*/ | ||
final class SingleLineCommentSpacingFixerTest extends AbstractFixerTestCase | ||
{ | ||
/** | ||
* @dataProvider provideTestCases | ||
*/ | ||
public function testFix(string $expected, ?string $input = null): void | ||
{ | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
public function provideTestCases(): iterable | ||
{ | ||
yield 'comment list' => [ | ||
'<?php | ||
// following: | ||
// 1 : | ||
// 2 : | ||
# Test: | ||
# - abc | ||
# - fgh | ||
# Title: | ||
# | abc1 | ||
# | xyz | ||
// Point: | ||
// * first point | ||
// * some other point | ||
// Matrix: | ||
// [1,2] | ||
// [3,4] | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php /* XYZ */', | ||
'<?php /* XYZ */', | ||
]; | ||
|
||
yield [ | ||
'<?php // /', | ||
'<?php ///', | ||
]; | ||
|
||
yield [ | ||
'<?php // //', | ||
'<?php ////', | ||
]; | ||
|
||
yield 'hash open slash asterisk close' => [ | ||
'<?php # A*/', | ||
'<?php #A*/', | ||
]; | ||
|
||
yield [ | ||
"<?php | ||
// a | ||
# b | ||
/* ABC */ | ||
// \t d | ||
#\te | ||
/* f */ | ||
", | ||
"<?php | ||
//a | ||
#b | ||
/*ABC*/ | ||
// \t d | ||
#\te | ||
/* f */ | ||
", | ||
]; | ||
|
||
yield 'do not fix multi line comments' => [ | ||
'<?php | ||
/* | ||
*/ | ||
/*A | ||
B*/ | ||
', | ||
]; | ||
|
||
yield 'empty double slash' => [ | ||
'<?php //', | ||
]; | ||
|
||
yield 'empty hash' => [ | ||
'<?php #', | ||
]; | ||
|
||
yield [ | ||
'<?php /**/', | ||
]; | ||
|
||
yield [ | ||
'<?php /***/', | ||
]; | ||
|
||
yield 'do not fix PHPDocs' => [ | ||
"<?php /**\n*/ /**\nX1*/ /** Y1 */", | ||
]; | ||
|
||
yield 'do not fix comments looking like PHPDocs' => [ | ||
'<?php /**/ /**X1*/ /** Y1 */', | ||
]; | ||
|
||
yield 'do not fix annotation' => [ | ||
'<?php | ||
namespace PhpCsFixer\Tests\Fixer\Basic; | ||
new | ||
#[Foo] | ||
class extends stdClass {}; | ||
', | ||
]; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Fixtures/Integration/priority/phpdoc_to_comment,single_line_comment_spacing.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--TEST-- | ||
Integration of fixers: phpdoc_to_comment,single_line_comment_spacing. | ||
--RULESET-- | ||
{"phpdoc_to_comment": true, "single_line_comment_spacing": true} | ||
--EXPECT-- | ||
<?php | ||
|
||
$first = true;// needed because by default first docblock is never fixed. | ||
|
||
/* Test */ | ||
echo 1; | ||
|
||
--INPUT-- | ||
<?php | ||
|
||
$first = true;// needed because by default first docblock is never fixed. | ||
|
||
/** Test*/ | ||
echo 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters