Skip to content

Inconsistent behavior of PHP_CodeSniffer_File::findEndOfStatement #1203

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

Closed
nkovacs opened this issue Oct 28, 2016 · 3 comments
Closed

Inconsistent behavior of PHP_CodeSniffer_File::findEndOfStatement #1203

nkovacs opened this issue Oct 28, 2016 · 3 comments

Comments

@nkovacs
Copy link
Contributor

nkovacs commented Oct 28, 2016

PHP_CodeSniffer_File::findEndOfStatement sometimes returns end token, but sometimes it returns the last not empty token before that.

E.g. if $start is new:

  • in [new Datetime] it returns ].
  • in array(new Datetime) it returnsDatetime.
  • in new Datetime; it returns ;.

The difference between array() and [] is probably a mistake, because [] was added later in 4df216f, but I don't understand why there's a difference at all.
The examples above are from a sniff that adds () to new. If findEndOfStatement consistently returned the last non-empty statement (like it does for array(new Datetime)), I could insert the () after that.

@gsherwood
Copy link
Member

First, the array() and [] difference is almost certainly a bug. Thanks for letting me know about that.

For the method itself, the goal is to find the end of the statement, which is typically a semicolon. But when you are within a different scope, like an array, there is no semicolon to return as the ending. If it returns the ] or ) then it is returning a token that actually belongs to the level above. So it returns the last token that is within that single statement.

Hope that makes sense.

@gsherwood
Copy link
Member

I've fixed the method to ensure the two array formats return the same result.

@nkovacs
Copy link
Contributor Author

nkovacs commented Oct 31, 2016

The problem is that here:

foo(new Bar, new Baz);

findEndOfStatement returns ,, not Bar, and Baz. This doesn't make sense.

I had to duplicate $endTokens in my sniff and check if the token returned is one of those, then find the previous non-whitespace token if it is.

I also don't understand why it returns on => and , and :. Those aren't statements, they're expressions, and it looks like findEndOfStatement actually finds the end of an expression, but it's used in a limited way in PHPCS (I guess mostly for skipping over nested expressions), so the inconsistency didn't cause any problems.

My sniff on the other hand finds all object creation expressions, and since they could be in most of the positions that findEndOfStatement deals with, the inconsistent behavior caused problems.

E.g. the first version did this:

foo(new Bar(), new ()Baz);

gsherwood added a commit that referenced this issue Mar 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants