From 7a830260028f6757121de7f07da8e53e0298c68e Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 29 Apr 2019 17:56:42 +0200 Subject: [PATCH] :bug: Fix DocCommentTageSpacing rule --- .../WhiteSpace/DocCommentTagSpacingSniff.php | 49 ++++++++++--------- .../DocCommentTagSpacingUnitTest.inc | 10 +++- .../DocCommentTagSpacingUnitTest.inc.fixed | 10 +++- .../DocCommentTagSpacingUnitTest.php | 8 +-- 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php b/SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php index b23a61e..51e7605 100644 --- a/SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php +++ b/SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php @@ -34,36 +34,41 @@ public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - if (!isset($tokens[($stackPtr - 1)]) || T_DOC_COMMENT_WHITESPACE !== $tokens[($stackPtr - 1)]['code']) { - $error = 'There should be a space before a doc comment tag "%s"'; - $fix = $phpcsFile->addFixableError( - $error, - ($stackPtr - 1), - 'DocCommentTagSpacing', - array($tokens[$stackPtr]['content']) - ); + if (isset($tokens[($stackPtr - 1)]) + && $tokens[$stackPtr]['line'] === $tokens[($stackPtr - 1)]['line'] + ) { + if (T_DOC_COMMENT_WHITESPACE !== $tokens[($stackPtr - 1)]['code']) { + $error = 'There should be a space before a doc comment tag "%s"'; + $fix = $phpcsFile->addFixableError( + $error, + ($stackPtr - 1), + 'DocCommentTagSpacing', + [$tokens[$stackPtr]['content']] + ); - if (true === $fix) { - $phpcsFile->fixer->addContentBefore($stackPtr, ' '); - } - } elseif (1 !== $tokens[($stackPtr - 1)]['length']) { - $error = 'There should be only one space before a doc comment tag "%s"'; - $fix = $phpcsFile->addFixableError( - $error, - ($stackPtr + 1), - 'DocCommentTagSpacing', - array($tokens[$stackPtr]['content']) - ); + if (true === $fix) { + $phpcsFile->fixer->addContentBefore($stackPtr, ' '); + } + } elseif (1 !== $tokens[($stackPtr - 1)]['length']) { + $error = 'There should be only one space before a doc comment tag "%s"'; + $fix = $phpcsFile->addFixableError( + $error, + ($stackPtr + 1), + 'DocCommentTagSpacing', + [$tokens[$stackPtr]['content']] + ); - if (true === $fix) { - $phpcsFile->fixer->replaceToken($stackPtr - 1, ' '); + if (true === $fix) { + $phpcsFile->fixer->replaceToken($stackPtr - 1, ' '); + } } } // No need to check for space after a doc comment tag if (isset($tokens[($stackPtr + 1)]) + && $tokens[$stackPtr]['line'] === $tokens[($stackPtr + 1)]['line'] && T_DOC_COMMENT_WHITESPACE === $tokens[($stackPtr + 1)]['code'] - && 1 !== $tokens[($stackPtr + 1)]['length'] + && 1 < $tokens[($stackPtr + 1)]['length'] ) { $error = 'There should be only one space after a doc comment tag "%s"'; $fix = $phpcsFile->addFixableError( diff --git a/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.inc b/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.inc index 6045822..3a5493d 100644 --- a/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.inc +++ b/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.inc @@ -13,7 +13,15 @@ class DocCommentTagSpacingUnitTest * @see otherFunctions() * @see anotherFunctions() */ - public $variableName = array(); + public $variableName = array(); + + /** + * @var string + * + * @JMS\Type("string") + * @JMS\SerializedName("libelle") + */ + private $label; /** * T_VARIABLE check, var in string and in function. diff --git a/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.inc.fixed b/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.inc.fixed index 7f03d06..40a27eb 100644 --- a/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.inc.fixed +++ b/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.inc.fixed @@ -13,7 +13,15 @@ class DocCommentTagSpacingUnitTest * @see otherFunctions() * @see anotherFunctions() */ - public $variableName = array(); + public $variableName = array(); + + /** + * @var string + * + * @JMS\Type("string") + * @JMS\SerializedName("libelle") + */ + private $label; /** * T_VARIABLE check, var in string and in function. diff --git a/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.php b/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.php index ee13648..a740580 100644 --- a/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.php +++ b/SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.php @@ -25,10 +25,10 @@ public function getErrorList() 11 => 1, 13 => 1, 14 => 1, - 21 => 1, - 22 => 1, - 24 => 1, - 28 => 1, + 29 => 1, + 30 => 1, + 32 => 1, + 36 => 1, ); }