Returns the position of the last non-whitespace token in a statement.
// Find the end of a PHP statement $file = new PHP_CodeSniffer_File('myfile.php'); $tokens = $file->getTokens(); $startPosition = 0; $endPosition = $file->findEndOfStatement($startPosition); $statementTokens = array_slice($tokens, $startPosition, $endPosition - $startPosition + 1); $statementString = $file->getTokensAsString($statementTokens); echo $statementString; // Outputs the PHP statement
// Loop through a file and extract all PHP statements $file = new PHP_CodeSniffer_File('myfile.php'); $tokens = $file->getTokens(); $currentPosition = 0; while ($currentPosition < count($tokens)) { $endPosition = $file->findEndOfStatement($currentPosition); $statementTokens = array_slice($tokens, $currentPosition, $endPosition - $currentPosition + 1); $statementString = $file->getTokensAsString($statementTokens); echo $statementString; // Outputs the PHP statement $currentPosition = $endPosition + 1; }Based on the code examples, PHP_CodeSniffer is the package library being used.