private function _parse($contents = null)
{
if ($contents === null && empty($this->_tokens) === false) {
// File has already been parsed.
return;
}
$stdin = false;
$cliValues = $this->phpcs->cli->getCommandLineValues();
if (empty($cliValues['files']) === true) {
$stdin = true;
}
// Determine the tokenizer from the file extension.
$fileParts = explode('.', $this->_file);
$extension = array_pop($fileParts);
if (isset($this->phpcs->allowedFileExtensions[$extension]) === true) {
$tokenizerClass = 'PHP_CodeSniffer_Tokenizers_' . $this->phpcs->allowedFileExtensions[$extension];
$this->tokenizerType = $this->phpcs->allowedFileExtensions[$extension];
} else {
if (isset($this->phpcs->defaultFileExtensions[$extension]) === true) {
$tokenizerClass = 'PHP_CodeSniffer_Tokenizers_' . $this->phpcs->defaultFileExtensions[$extension];
$this->tokenizerType = $this->phpcs->defaultFileExtensions[$extension];
} else {
// Revert to default.
$tokenizerClass = 'PHP_CodeSniffer_Tokenizers_' . $this->tokenizerType;
}
}
$tokenizer = new $tokenizerClass();
$this->tokenizer = $tokenizer;
if ($contents === null) {
$contents = file_get_contents($this->_file);
}
try {
$tabWidth = null;
$encoding = null;
if (defined('PHP_CODESNIFFER_IN_TESTS') === true) {
$cliValues = $this->phpcs->cli->getCommandLineValues();
if (isset($cliValues['tabWidth']) === true) {
$tabWidth = $cliValues['tabWidth'];
}
if (isset($cliValues['encoding']) === true) {
$encoding = $cliValues['encoding'];
}
}
$this->_tokens = self::tokenizeString($contents, $tokenizer, $this->eolChar, $tabWidth, $encoding);
} catch (PHP_CodeSniffer_Exception $e) {
$this->addWarning($e->getMessage(), null, 'Internal.Tokenizer.Exception');
if (PHP_CODESNIFFER_VERBOSITY > 0 || PHP_CODESNIFFER_CBF === true && $stdin === false) {
echo "[{$this->tokenizerType} => tokenizer error]... ";
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL;
}
}
return;
}
//end try
$this->numTokens = count($this->_tokens);
// Check for mixed line endings as these can cause tokenizer errors and we
// should let the user know that the results they get may be incorrect.
// This is done by removing all backslashes, removing the newline char we
// detected, then converting newlines chars into text. If any backslashes
// are left at the end, we have additional newline chars in use.
$contents = str_replace('\\', '', $contents);
$contents = str_replace($this->eolChar, '', $contents);
$contents = str_replace("\n", '\\n', $contents);
$contents = str_replace("\r", '\\r', $contents);
if (strpos($contents, '\\') !== false) {
$error = 'File has mixed line endings; this may cause incorrect results';
$this->addWarning($error, 0, 'Internal.LineEndings.Mixed');
}
if (PHP_CODESNIFFER_VERBOSITY > 0 || PHP_CODESNIFFER_CBF === true && $stdin === false) {
if ($this->numTokens === 0) {
$numLines = 0;
} else {
$numLines = $this->_tokens[$this->numTokens - 1]['line'];
}
echo "[{$this->tokenizerType} => {$this->numTokens} tokens in {$numLines} lines]... ";
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL;
}
}
}