From 083041b965aec8d240f5b61dca4a2bf880ed2edb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Tue, 5 Nov 2019 17:46:47 +0000 Subject: [PATCH] Feature: Commenting\TagWithType ability to configure null to be at the 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. --- src/WebimpressCodingStandard/Helper/MethodsTrait.php | 4 ++-- .../Sniffs/Commenting/TagWithTypeSniff.php | 9 ++++++++- test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc | 8 ++++++++ test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed | 8 ++++++++ test/Sniffs/Commenting/TagWithTypeUnitTest.php | 1 + 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/WebimpressCodingStandard/Helper/MethodsTrait.php b/src/WebimpressCodingStandard/Helper/MethodsTrait.php index eb207c73..be59577e 100644 --- a/src/WebimpressCodingStandard/Helper/MethodsTrait.php +++ b/src/WebimpressCodingStandard/Helper/MethodsTrait.php @@ -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') { diff --git a/src/WebimpressCodingStandard/Sniffs/Commenting/TagWithTypeSniff.php b/src/WebimpressCodingStandard/Sniffs/Commenting/TagWithTypeSniff.php index f5695b3d..5d788995 100644 --- a/src/WebimpressCodingStandard/Sniffs/Commenting/TagWithTypeSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/Commenting/TagWithTypeSniff.php @@ -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; @@ -45,6 +46,11 @@ class TagWithTypeSniff implements Sniff '@var', ]; + /** + * @var string Allowed values: "first", "last" + */ + public $nullPosition = 'first'; + /** * @var null|string */ @@ -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); } } diff --git a/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc b/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc index be933ebf..32500190 100644 --- a/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc +++ b/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc @@ -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; diff --git a/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed b/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed index 3ed0e9b8..2c47d3e5 100644 --- a/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed +++ b/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed @@ -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; diff --git a/test/Sniffs/Commenting/TagWithTypeUnitTest.php b/test/Sniffs/Commenting/TagWithTypeUnitTest.php index f4137c34..545919c5 100644 --- a/test/Sniffs/Commenting/TagWithTypeUnitTest.php +++ b/test/Sniffs/Commenting/TagWithTypeUnitTest.php @@ -181,6 +181,7 @@ protected function getErrorList(string $testFile = '') : array 295 => 1, 299 => 1, 304 => 1, + 314 => 1, ]; }