Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Commenting\TagWithType ability to configure null to be at the end #48

Merged
merged 1 commit into from
Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/WebimpressCodingStandard/Helper/MethodsTrait.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ protected function getErrorList(string $testFile = '') : array
295 => 1,
299 => 1,
304 => 1,
314 => 1,
];
}

Expand Down