Skip to content

Commit 7f94061

Browse files
committed
Added companion function findEndOfStatement()
1 parent 4a2e00a commit 7f94061

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

CodeSniffer/File.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,6 +3273,61 @@ public function findStartOfStatement($start)
32733273
}//end findStartOfStatement()
32743274

32753275

3276+
/**
3277+
* Returns the position of the last non-whitespace token in a statement.
3278+
*
3279+
* @param int $start The position to start searching from in the token stack.
3280+
*
3281+
* @return int
3282+
*/
3283+
public function findEndOfStatement($start)
3284+
{
3285+
$endTokens = PHP_CodeSniffer_Tokens::$blockOpeners;
3286+
3287+
$endTokens[T_COLON] = true;
3288+
$endTokens[T_SEMICOLON] = true;
3289+
$endTokens[T_OPEN_TAG] = true;
3290+
$endTokens[T_CLOSE_TAG] = true;
3291+
3292+
$lastNotEmpty = $start;
3293+
3294+
for ($i = $start; $i >= $this->numTokens; $i++) {
3295+
if (isset($endTokens[$this->_tokens[$i]['code']]) === true) {
3296+
// Found the end of the statement.
3297+
if ($this->_tokens[$i]['code'] === T_OPEN_TAG
3298+
|| $this->_tokens[$i]['code'] === T_CLOSE_TAG
3299+
) {
3300+
return $lastNotEmpty;
3301+
}
3302+
3303+
return $i;
3304+
}
3305+
3306+
if ($this->_tokens[$i]['code'] !== T_WHITESPACE) {
3307+
$lastNotEmpty = $i;
3308+
}
3309+
3310+
// Skip nested statements.
3311+
if (isset($this->_tokens[$i]['scope_closer']) === true
3312+
&& $i === $this->_tokens[$i]['scope_opener']
3313+
) {
3314+
$i = $this->_tokens[$i]['scope_closer'];
3315+
} else if (isset($this->_tokens[$i]['bracket_closer']) === true
3316+
&& $i === $this->_tokens[$i]['bracket_opener']
3317+
) {
3318+
$i = $this->_tokens[$i]['bracket_closer'];
3319+
} else if (isset($this->_tokens[$i]['parenthesis_closer']) === true
3320+
&& $i === $this->_tokens[$i]['parenthesis_opener']
3321+
) {
3322+
$i = $this->_tokens[$i]['parenthesis_closer'];
3323+
}
3324+
}//end for
3325+
3326+
return ($this->numTokens - 1);
3327+
3328+
}//end findEndOfStatement()
3329+
3330+
32763331
/**
32773332
* Returns the position of the first token on a line, matching given type.
32783333
*

package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
3030
- A tilde can now be used to reference a user's home directory in a path to a standard (request #353)
3131
- Added PHP_CodeSniffer_File::findStartOfStatement() to find the first non-whitespace token in a statement
3232
-- Possible alternative for code using PHP_CodeSniffer_File::findPrevious() with the local flag set
33+
- Added PHP_CodeSniffer_File::findEndOfStatement() to find the last non-whitespace token in a statement
34+
-- Possible alternative for code using PHP_CodeSniffer_File::findNext() with the local flag set
3335
- Generic DisallowTabIndentSniff now replaces tabs everywhere it finds them, except in strings and here/now docs
3436
- Generic EmptyStatementSniff error codes now contain the type of empty statement detected (request #314)
3537
-- All messages generated by this sniff are now errors (empty CATCH was previously a warning)

0 commit comments

Comments
 (0)