Skip to content

Commit

Permalink
Merge pull request #46 from VincentLanglet/fixDocCommentTagSpacing
Browse files Browse the repository at this point in the history
🐛 Fix DocCommentTageSpacing rule
  • Loading branch information
VincentLanglet authored Apr 29, 2019
2 parents 137a61d + 7a83026 commit f72416d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
49 changes: 27 additions & 22 deletions SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 9 additions & 1 deletion SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down

0 comments on commit f72416d

Please sign in to comment.