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

Generic.CodeAnalysis.EmptyStatement Add option to allow only comments in statements #2240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 15 additions & 2 deletions src/Standards/Generic/Sniffs/CodeAnalysis/EmptyStatementSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
class EmptyStatementSniff implements Sniff
{

/**
* Whether to allow statements that contain only comments.
*
* @var boolean
*/
public $allowComments = false;


/**
* Registers the tokens that this sniff wants to listen for.
Expand Down Expand Up @@ -74,10 +81,16 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

if ($this->allowComments === true) {
$emptyTokens = ([T_WHITESPACE => T_WHITESPACE] + Tokens::$phpcsCommentTokens);
} else {
$emptyTokens = Tokens::$emptyTokens;
}

$next = $phpcsFile->findNext(
Tokens::$emptyTokens,
$emptyTokens,
($token['scope_opener'] + 1),
($token['scope_closer'] - 1),
$token['scope_closer'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't expecting to have to change this, but skips the last token otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a known issue, see #1319

true
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,18 @@ try {
// TODO: Handle this exception later :-)
}

if (true) {} elseif (false) {}
if (true) {} elseif (false) {}

// phpcs:set Generic.CodeAnalysis.EmptyStatement allowComments true

if ($foo) {
// Just a comment
} elseif ($bar) {
/*
* Yet another comment
*/
} elseif ($baz) {
// phpcs:set Generic.CodeAnalysis.EmptyStatement somethingMadeUp true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent confusion between real test related PHPCS annotations and annotations which are part of the test, it's better to use something like //phpcs:ignore Standard.Category.SniffName -- for reasons here.

} else {

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably reset the changed property to the default at the end of this test to prevent issues with future test cases being added.

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function getErrorList()
64 => 1,
68 => 1,
72 => 2,
82 => 1,
84 => 1,
];

}//end getErrorList()
Expand Down