Skip to content
Merged
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
65 changes: 22 additions & 43 deletions src/SqlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,13 +722,6 @@ final class SqlFormatter
*/
public $tab = ' ';

/**
* This flag tells us if SqlFormatted has been initialized
*
* @var bool
*/
private $init;

// Regular expressions for tokenizing

/** @var string */
Expand Down Expand Up @@ -766,39 +759,6 @@ final class SqlFormatter

public function __construct(?Highlighter $highlighter = null)
{
if ($highlighter === null) {
$this->highlighter = $this->isCli() ? new CliHighlighter() : new HtmlHighlighter();

return;
}

$this->highlighter = $highlighter;
}

/**
* Get stats about the token cache
*
* @return mixed[] An array containing the keys 'hits', 'misses', 'entries', and 'size' in bytes
*/
public function getCacheStats() : array
{
return [
'hits'=>$this->cacheHits,
'misses'=>$this->cacheMisses,
'entries'=>count($this->tokenCache),
'size'=>strlen(serialize($this->tokenCache)),
];
}

/**
* Stuff that only needs to be done once. Builds regular expressions and sorts the reserved words.
*/
private function init() : void
{
if ($this->init) {
return;
}

// Sort reserved word list from longest word to shortest, 3x faster than usort
$reservedMap = array_combine($this->reserved, array_map('strlen', $this->reserved));
assert($reservedMap !== false);
Expand All @@ -825,7 +785,28 @@ private function init() : void

$this->regexFunction = '(' . implode('|', $this->quoteRegex($this->functions)) . ')';

$this->init = true;
if ($highlighter === null) {
$this->highlighter = $this->isCli() ? new CliHighlighter() : new HtmlHighlighter();

return;
}

$this->highlighter = $highlighter;
}

/**
* Get stats about the token cache
*
* @return mixed[] An array containing the keys 'hits', 'misses', 'entries', and 'size' in bytes
*/
public function getCacheStats() : array
{
return [
'hits'=>$this->cacheHits,
'misses'=>$this->cacheMisses,
'entries'=>count($this->tokenCache),
'size'=>strlen(serialize($this->tokenCache)),
];
}

/**
Expand Down Expand Up @@ -1018,8 +999,6 @@ private function getQuotedString(string $string) : string
*/
private function tokenize(string $string) : array
{
$this->init();

$tokens = [];

// Used for debugging if there is an error while tokenizing the string
Expand Down