From 24539cb500c952fdf765de9fd066e6abb48f1f60 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 23 May 2019 00:54:11 +0200 Subject: [PATCH] :sparkles: Add fixer for docComment --- .../Sniffs/Commenting/DocCommentSniff.php | 31 ++++++++++++++++++- .../Tests/Commenting/DocCommentUnitTest.inc | 10 ++++++ .../Commenting/DocCommentUnitTest.inc.fixed | 14 +++++---- .../Tests/Commenting/DocCommentUnitTest.php | 4 +++ 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php b/SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php index f27468a..455ee5f 100644 --- a/SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php +++ b/SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php @@ -61,7 +61,36 @@ public function process(File $phpcsFile, $stackPtr) if (false === $short) { // No content at all. $error = 'Doc comment is empty'; - $phpcsFile->addError($error, $stackPtr, 'Empty'); + + $next = $phpcsFile->findNext(T_WHITESPACE, $commentEnd + 1, null, true); + $hasSameLineNext = $next && $tokens[$next]['line'] === $tokens[$commentEnd]['line']; + $previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true); + $hasSameLinePrevious = $tokens[$previous]['line'] === $tokens[$stackPtr]['line']; + + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty'); + + if (true === $fix) { + $phpcsFile->fixer->beginChangeset(); + + $end = $hasSameLineNext ? $next : $commentEnd + 1; + for ($i = $stackPtr; $i < $end; $i++) { + $phpcsFile->fixer->replaceToken($i, ''); + } + + if (!$hasSameLineNext) { + $previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true); + for ($i = $stackPtr - 1; $i > $previous; $i--) { + if ($tokens[$i]['line'] < $tokens[$stackPtr]['line']) { + $phpcsFile->fixer->replaceToken($i, ''); + break; + } + + $phpcsFile->fixer->replaceToken($i, ''); + } + } + + $phpcsFile->fixer->endChangeset(); + } return; } diff --git a/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.inc b/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.inc index 853a177..f19e37c 100644 --- a/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.inc +++ b/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.inc @@ -32,4 +32,14 @@ class Test /** * Short description. */ + + private $a; /** */ + + private $b; /** */ private $c; + + /** */ private $d; + + private $e; /** + * + */ private $f; } diff --git a/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.inc.fixed b/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.inc.fixed index 43ebc8f..2749dec 100644 --- a/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.inc.fixed +++ b/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.inc.fixed @@ -6,14 +6,8 @@ class Test * Short description. */ - /** - * - */ - /** - */ - /** */ /** * Short description. @@ -32,4 +26,12 @@ class Test /** * Short description. */ + + private $a; + + private $b; private $c; + + private $d; + + private $e; private $f; } diff --git a/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.php b/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.php index c5d535e..87982b3 100644 --- a/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.php +++ b/SymfonyCustom/Tests/Commenting/DocCommentUnitTest.php @@ -29,6 +29,10 @@ public function getErrorList() 26 => 1, 28 => 1, 34 => 1, + 36 => 1, + 38 => 1, + 40 => 1, + 42 => 1, ]; }