Skip to content

Commit

Permalink
Feature: Commenting\TagWithType ability to configure null to be at th…
Browse files Browse the repository at this point in the history
…e end

Added configuration option `nullPosition` with default value `first`.
The other allowed value is `last` so then `null` is going to be at the
end of types list.
  • Loading branch information
michalbundyra committed Nov 5, 2019
1 parent df93d48 commit 083041b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/WebimpressCodingStandard/Helper/MethodsTrait.php
Expand Up @@ -149,11 +149,11 @@ public function sortTypes(string $a, string $b) : int
$b = strtolower(str_replace('\\', ':', $b));

if ($a === 'null' || strpos($a, 'null[') === 0) {
return -1;
return $this->nullPosition === 'last' ? 1 : -1;
}

if ($b === 'null' || strpos($b, 'null[') === 0) {
return 1;
return $this->nullPosition === 'last' ? -1 : 1;
}

if ($a === 'true' || $a === 'false') {
Expand Down
Expand Up @@ -27,6 +27,7 @@
use function ucfirst;
use function usort;

use const T_DOC_COMMENT_CLOSE_TAG;
use const T_DOC_COMMENT_OPEN_TAG;
use const T_DOC_COMMENT_STRING;
use const T_DOC_COMMENT_TAG;
Expand All @@ -45,6 +46,11 @@ class TagWithTypeSniff implements Sniff
'@var',
];

/**
* @var string Allowed values: "first", "last"
*/
public $nullPosition = 'first';

/**
* @var null|string
*/
Expand Down Expand Up @@ -481,7 +487,8 @@ private function checkTypes(File $phpcsFile, string $tag, int $tagPtr) : void
$fix = $phpcsFile->addFixableError($error, $tagPtr + 2, 'InvalidOrder', $data);

if ($fix) {
$content = trim($content . ' ' . $this->description);
$content = trim($content . ' ' . $this->description)
. ($phpcsFile->getTokens()[$tagPtr + 3]['code'] === T_DOC_COMMENT_CLOSE_TAG ? ' ' : '');
$phpcsFile->fixer->replaceToken($tagPtr + 2, $content);
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc
Expand Up @@ -305,3 +305,11 @@ class VarTag extends VarTagParent {
*/
public $a;
}

// @phpcs:set WebimpressCodingStandard.Commenting.TagWithType nullPosition last

/** @var string|null $var */
$var = 'abc';

/** @var null|int $bar */
$bar = $var ? 12 : null;
8 changes: 8 additions & 0 deletions test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed
Expand Up @@ -305,3 +305,11 @@ class VarTag extends VarTagParent {
*/
public $a;
}

// @phpcs:set WebimpressCodingStandard.Commenting.TagWithType nullPosition last

/** @var string|null $var */
$var = 'abc';

/** @var int|null $bar */
$bar = $var ? 12 : null;
1 change: 1 addition & 0 deletions test/Sniffs/Commenting/TagWithTypeUnitTest.php
Expand Up @@ -181,6 +181,7 @@ protected function getErrorList(string $testFile = '') : array
295 => 1,
299 => 1,
304 => 1,
314 => 1,
];
}

Expand Down

0 comments on commit 083041b

Please sign in to comment.