Skip to content

Commit 5936239

Browse files
committed
Automatically remove useless comments
When issues are detected and classified as Magento2.Commenting.ClassAndInterfacePHPDocFormatting.ForbiddenTags or Magento2.Commenting.ClassAndInterfacePHPDocFormatting.InvalidDescription, these can be fixed by removing the offending comment.
1 parent ffd4818 commit 5936239

6 files changed

+398
-3
lines changed

Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,29 @@ public function process(File $phpcsFile, $stackPtr)
6262
return;
6363
}
6464

65+
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
66+
6567
if ($this->PHPDocFormattingValidator->providesMeaning($namePtr, $commentStartPtr, $tokens) !== true) {
66-
$phpcsFile->addWarning(
68+
$fix = $phpcsFile->addFixableWarning(
6769
sprintf(
6870
'%s description must contain meaningful information beyond what its name provides or be removed.',
6971
ucfirst($tokens[$stackPtr]['content'])
7072
),
7173
$stackPtr,
7274
'InvalidDescription'
7375
);
76+
77+
if ($fix) {
78+
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
79+
$phpcsFile->fixer->replaceToken($i, '');
80+
}
81+
82+
if ($tokens[$commentStartPtr - 1]['code'] === T_WHITESPACE
83+
&& $tokens[$commentCloserPtr + 1]['code'] === T_WHITESPACE
84+
) {
85+
$phpcsFile->fixer->replaceToken($commentCloserPtr + 1, '');
86+
}
87+
}
7488
}
7589

7690
if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStartPtr, $tokens) !== true) {
@@ -104,11 +118,35 @@ private function validateTags(File $phpcsFile, $commentStartPtr, $tokens)
104118
}
105119

106120
if (in_array($tokens[$i]['content'], $this->forbiddenTags) === true) {
107-
$phpcsFile->addWarning(
121+
$fix = $phpcsFile->addFixableWarning(
108122
sprintf('Tag %s MUST NOT be used.', $tokens[$i]['content']),
109123
$i,
110124
'ForbiddenTags'
111125
);
126+
127+
if ($fix) {
128+
for ($j = $i - 1; $j > $commentStartPtr; $j--) {
129+
if (!in_array($tokens[$j]['code'], [T_DOC_COMMENT_STAR, T_DOC_COMMENT_WHITESPACE], true)) {
130+
break;
131+
}
132+
133+
if ($tokens[$j]['code'] === T_DOC_COMMENT_WHITESPACE && $tokens[$j]['content'] === "\n") {
134+
break;
135+
}
136+
137+
$phpcsFile->fixer->replaceToken($j, '');
138+
}
139+
140+
$phpcsFile->fixer->replaceToken($i, '');
141+
142+
for ($j = $i + 1; $j < $commentCloserPtr; $j++) {
143+
$phpcsFile->fixer->replaceToken($j, '');
144+
145+
if ($tokens[$j]['code'] === T_DOC_COMMENT_WHITESPACE && $tokens[$j]['content'] === "\n") {
146+
break;
147+
}
148+
}
149+
}
112150
}
113151
}
114152

Magento2/Tests/Commenting/ClassAndInterfacePHPDocFormattingUnitTest.1.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,10 @@ class AlsoDeprecatedButHandler
179179

180180
}
181181

182+
/**
183+
* @package this tag should not be used
184+
*/
185+
class OnlyUselessCommentContent
186+
{
187+
188+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
/**
4+
* Handler for PHP errors/warnings/notices that converts them to exceptions.
5+
*/
6+
class ErrorHandler
7+
{
8+
9+
}
10+
11+
class NotAnErrorHandler
12+
{
13+
14+
}
15+
16+
class FaultyHandler
17+
{
18+
19+
}
20+
21+
class SomeHandler
22+
{
23+
24+
}
25+
26+
class YetAnotherHandler
27+
{
28+
29+
}
30+
31+
class GreenHandler
32+
{
33+
34+
}
35+
36+
class EmptyHandler
37+
{
38+
39+
}
40+
41+
/**
42+
* Handler for PHP errors/warnings/notices that converts them to exceptions.
43+
*
44+
* @api is ok here
45+
* @deprecated can be used in this context
46+
* @see is ok here
47+
* @author is actually ok
48+
*/
49+
class ExampleHandler
50+
{
51+
52+
}
53+
54+
/**
55+
* @api
56+
* @since 100.0.2
57+
*/
58+
class ApiHandler
59+
{
60+
61+
}
62+
63+
/**
64+
* @api
65+
*/
66+
class AsyncApiHandler
67+
{
68+
69+
}
70+
71+
/**
72+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
73+
*/
74+
class GroupRepositoryHandler
75+
{
76+
77+
}
78+
79+
/**
80+
* @deprecated
81+
*/
82+
class DeprecatedHandler
83+
{
84+
85+
}
86+
87+
/**
88+
* @deprecated Should not be used
89+
*/
90+
class AncientHandler
91+
{
92+
93+
}
94+
95+
/**
96+
* @deprecated
97+
* @see
98+
*/
99+
class AgedHandler
100+
{
101+
102+
}
103+
104+
/**
105+
* @deprecated Should not be used
106+
* @see
107+
*/
108+
class ArhaicHandler
109+
{
110+
111+
}
112+
113+
/**
114+
* @deprecated Should not be used
115+
* @see Magento\Framework\NewHandler
116+
*/
117+
class OldHandler
118+
{
119+
120+
}
121+
122+
/**
123+
* @see Magento\Framework\NewHandler
124+
*/
125+
class SomethingHandler
126+
{
127+
128+
}
129+
130+
/**
131+
* @see
132+
*/
133+
class DoNotCareHandler
134+
{
135+
136+
}
137+
138+
/**
139+
* @deprecated
140+
* @see Magento\Framework\NewHandler
141+
*/
142+
class OldHandler
143+
{
144+
145+
}
146+
147+
/**
148+
* @deprecated This class will be removed in version 1.0.0 without replacement
149+
*/
150+
class DeprecatedButHandler
151+
{
152+
153+
}
154+
155+
/**
156+
* @deprecated It's also deprecated - This class will be removed in version 1.0.0 without replacement
157+
*/
158+
class AlsoDeprecatedButHandler
159+
{
160+
161+
}
162+
163+
class OnlyUselessCommentContent
164+
{
165+
166+
}

Magento2/Tests/Commenting/ClassAndInterfacePHPDocFormattingUnitTest.2.inc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ interface DoNotCareHandler
154154

155155
}
156156

157+
/**
158+
* @deprecated
159+
* @see Magento\Framework\NewHandler
160+
*/
161+
interface OldHandler
162+
{
163+
164+
}
165+
157166
/**
158167
* @deprecated This interface will be removed in version 1.0.0 without replacement
159168
*/
@@ -169,3 +178,11 @@ interface AlsoDeprecatedButHandler
169178
{
170179

171180
}
181+
182+
/**
183+
* @package this tag should not be used
184+
*/
185+
interface OnlyUselessCommentContent
186+
{
187+
188+
}

0 commit comments

Comments
 (0)