Skip to content

Commit 24539cb

Browse files
✨ Add fixer for docComment
1 parent 4fced60 commit 24539cb

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,36 @@ public function process(File $phpcsFile, $stackPtr)
6161
if (false === $short) {
6262
// No content at all.
6363
$error = 'Doc comment is empty';
64-
$phpcsFile->addError($error, $stackPtr, 'Empty');
64+
65+
$next = $phpcsFile->findNext(T_WHITESPACE, $commentEnd + 1, null, true);
66+
$hasSameLineNext = $next && $tokens[$next]['line'] === $tokens[$commentEnd]['line'];
67+
$previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
68+
$hasSameLinePrevious = $tokens[$previous]['line'] === $tokens[$stackPtr]['line'];
69+
70+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty');
71+
72+
if (true === $fix) {
73+
$phpcsFile->fixer->beginChangeset();
74+
75+
$end = $hasSameLineNext ? $next : $commentEnd + 1;
76+
for ($i = $stackPtr; $i < $end; $i++) {
77+
$phpcsFile->fixer->replaceToken($i, '');
78+
}
79+
80+
if (!$hasSameLineNext) {
81+
$previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
82+
for ($i = $stackPtr - 1; $i > $previous; $i--) {
83+
if ($tokens[$i]['line'] < $tokens[$stackPtr]['line']) {
84+
$phpcsFile->fixer->replaceToken($i, '');
85+
break;
86+
}
87+
88+
$phpcsFile->fixer->replaceToken($i, '');
89+
}
90+
}
91+
92+
$phpcsFile->fixer->endChangeset();
93+
}
6594

6695
return;
6796
}

SymfonyCustom/Tests/Commenting/DocCommentUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ class Test
3232

3333
/**
3434
* Short description. */
35+
36+
private $a; /** */
37+
38+
private $b; /** */ private $c;
39+
40+
/** */ private $d;
41+
42+
private $e; /**
43+
*
44+
*/ private $f;
3545
}

SymfonyCustom/Tests/Commenting/DocCommentUnitTest.inc.fixed

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,8 @@ class Test
66
* Short description.
77
*/
88

9-
/**
10-
*
11-
*/
129

13-
/**
14-
*/
1510

16-
/** */
1711

1812
/**
1913
* Short description.
@@ -32,4 +26,12 @@ class Test
3226
/**
3327
* Short description.
3428
*/
29+
30+
private $a;
31+
32+
private $b; private $c;
33+
34+
private $d;
35+
36+
private $e; private $f;
3537
}

SymfonyCustom/Tests/Commenting/DocCommentUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public function getErrorList()
2929
26 => 1,
3030
28 => 1,
3131
34 => 1,
32+
36 => 1,
33+
38 => 1,
34+
40 => 1,
35+
42 => 1,
3236
];
3337
}
3438

0 commit comments

Comments
 (0)