Skip to content

Commit

Permalink
Feature: Commenting\TagName sniff
Browse files Browse the repository at this point in the history
Check tags if there are any disallowed characters at the end of the name.
By defualt configured with `:` and `;` but can be set to any charactes
by configuration option `disallowedEndChars`.
  • Loading branch information
michalbundyra committed Nov 5, 2019
1 parent df93d48 commit 59850c9
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/WebimpressCodingStandard/Sniffs/Commenting/TagNameSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace WebimpressCodingStandard\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

use function rtrim;

use const T_DOC_COMMENT_TAG;

class TagNameSniff implements Sniff
{
/**
* @var string Characters disallowed at the end of the PHPDoc tag name
*/
public $disallowedEndChars = ':;';

/**
* @return int[]
*/
public function register() : array
{
return [T_DOC_COMMENT_TAG];
}

/**
* @param int $stackPtr
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

$content = $tokens[$stackPtr]['content'];
$newContent = rtrim($content, $this->disallowedEndChars);

if ($content !== $newContent) {
$error = 'Invalid tag name found %s, expected %s';
$data = [
$content,
$newContent,
];

$fix = $phpcsFile->addFixableError($error, $stackPtr, 'DisallowedEndChars', $data);
if ($fix) {
$phpcsFile->fixer->replaceToken($stackPtr, $newContent);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* After comma should be exactly one comma.
* There is allowed more than one space after comma only in when this is multidimensional array.
*
* @todo: maybe we need fix part for multidimensional array, now it's checking for something like:
* @todo maybe we need fix part for multidimensional array, now it's checking for something like:
* [
* [1, 3423, 342, 4324],
* [4432, 43, 4, 32],
Expand Down
21 changes: 21 additions & 0 deletions test/Sniffs/Commenting/TagNameUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* @todo: hello
* @todo: hi
* @todo; hola
* @todo;;;
* @todo:;::
*/

/**
* @var; int $var
*/

/**
* @myCustomTag:;
*/

/**
* @tag : space
*/
21 changes: 21 additions & 0 deletions test/Sniffs/Commenting/TagNameUnitTest.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* @todo hello
* @todo hi
* @todo hola
* @todo
* @todo
*/

/**
* @var int $var
*/

/**
* @myCustomTag
*/

/**
* @tag : space
*/
28 changes: 28 additions & 0 deletions test/Sniffs/Commenting/TagNameUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace WebimpressCodingStandardTest\Sniffs\Commenting;

use WebimpressCodingStandardTest\Sniffs\AbstractTestCase;

class TagNameUnitTest extends AbstractTestCase
{
protected function getErrorList(string $testFile = '') : array
{
return [
4 => 1,
5 => 1,
6 => 1,
7 => 1,
8 => 1,
12 => 1,
16 => 1,
];
}

protected function getWarningList(string $testFile = '') : array
{
return [];
}
}

0 comments on commit 59850c9

Please sign in to comment.