-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
df93d48
commit 59850c9
Showing
5 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/WebimpressCodingStandard/Sniffs/Commenting/TagNameSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |