Skip to content

🐛 Fix for custom tag #47

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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class DocCommentGroupSameTypeSniff implements Sniff
'@author',
'@category',
'@copyright',
'@covers',
'@dataProvider',
'@deprecated',
'@example',
'@filesource',
Expand Down
64 changes: 53 additions & 11 deletions SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,44 @@
*/
class DocCommentTagSpacingSniff implements Sniff
{
/**
* A list of PHPDoc tags that are checked.
*
* @var array
*/
public $tags = array(
'@api',
'@author',
'@category',
'@copyright',
'@covers',
'@dataProvider',
'@deprecated',
'@example',
'@filesource',
'@global',
'@ignore',
'@internal',
'@license',
'@link',
'@method',
'@package',
'@param',
'@property',
'@property-read',
'@property-write',
'@return',
'@see',
'@since',
'@source',
'@subpackage',
'@throws',
'@todo',
'@uses',
'@var',
'@version',
);

/**
* Returns an array of tokens this test wants to listen for.
*
Expand Down Expand Up @@ -49,22 +87,26 @@ public function process(File $phpcsFile, $stackPtr)
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']]
);
} elseif (1 < $tokens[($stackPtr - 1)]['length']) {
$isCustomTag = !in_array($tokens[$stackPtr]['content'], $this->tags);

if (true === $fix) {
$phpcsFile->fixer->replaceToken($stackPtr - 1, ' ');
// Custom tags are not checked cause there is annotation with array params
if (!$isCustomTag) {
$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, ' ');
}
}
}
}

// 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']
Expand Down
10 changes: 10 additions & 0 deletions SymfonyCustom/Tests/WhiteSpace/DocCommentTagSpacingUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class DocCommentTagSpacingUnitTest
*
* @JMS\Type("string")
* @JMS\SerializedName("libelle")
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(
* name="email",
* column=@ORM\Column(type="string", name="email", length=255, unique=false, nullable=true)
* ),
* @ORM\AttributeOverride(
* name="emailCanonical",
* column=@ORM\Column(type="string", name="email_canonical", length=255, unique=false, nullable=true)
* )
* })
*/
private $label;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class DocCommentTagSpacingUnitTest
*
* @JMS\Type("string")
* @JMS\SerializedName("libelle")
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(
* name="email",
* column=@ORM\Column(type="string", name="email", length=255, unique=false, nullable=true)
* ),
* @ORM\AttributeOverride(
* name="emailCanonical",
* column=@ORM\Column(type="string", name="email_canonical", length=255, unique=false, nullable=true)
* )
* })
*/
private $label;

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,
29 => 1,
30 => 1,
32 => 1,
36 => 1,
39 => 1,
40 => 1,
42 => 1,
46 => 1,
);
}

Expand Down