Skip to content

🐛 Fix DocCommentTageSpacing rule #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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