Skip to content

Commit

Permalink
Merge branch 'feature/47' into develop
Browse files Browse the repository at this point in the history
Close #47
  • Loading branch information
michalbundyra committed Nov 10, 2019
2 parents 17a9c77 + 151ccf1 commit 0afc1e4
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ All notable changes to this project will be documented in this file, in reverse

- [#45](https://github.com/webimpress/coding-standard/pull/45) adds `Classes\ConstBeforeProperty` sniff to require constant definitions in classes and interfaces before properties and methods

- [#47](https://github.com/webimpress/coding-standard/pull/47) adds `Commenting\TagName` sniff which checks if PHPDoc tags have additional characters at the end of the name.
By default `:` and `;` are disallowed and removed by fixer, but the list of disallowed characters can be configured by option `disallowedEndChars`

### Changed

- [#42](https://github.com/webimpress/coding-standard/pull/42) changes `NamingConventions\ValidVariableName` to require variable names be in strict camelCase. It means two capital letters next to each other are not allowed.
Expand Down
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 0afc1e4

Please sign in to comment.