Skip to content

Commit 65159dd

Browse files
committed
Move initialization to the constructor
Since this class no longer uses statism, we have state and we can initialize it in the constructor.
1 parent fc5b0a7 commit 65159dd

File tree

1 file changed

+22
-43
lines changed

1 file changed

+22
-43
lines changed

src/SqlFormatter.php

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -722,13 +722,6 @@ final class SqlFormatter
722722
*/
723723
public $tab = ' ';
724724

725-
/**
726-
* This flag tells us if SqlFormatted has been initialized
727-
*
728-
* @var bool
729-
*/
730-
private $init;
731-
732725
// Regular expressions for tokenizing
733726

734727
/** @var string */
@@ -766,39 +759,6 @@ final class SqlFormatter
766759

767760
public function __construct(?Highlighter $highlighter = null)
768761
{
769-
if ($highlighter === null) {
770-
$this->highlighter = $this->isCli() ? new CliHighlighter() : new HtmlHighlighter();
771-
772-
return;
773-
}
774-
775-
$this->highlighter = $highlighter;
776-
}
777-
778-
/**
779-
* Get stats about the token cache
780-
*
781-
* @return mixed[] An array containing the keys 'hits', 'misses', 'entries', and 'size' in bytes
782-
*/
783-
public function getCacheStats() : array
784-
{
785-
return [
786-
'hits'=>$this->cacheHits,
787-
'misses'=>$this->cacheMisses,
788-
'entries'=>count($this->tokenCache),
789-
'size'=>strlen(serialize($this->tokenCache)),
790-
];
791-
}
792-
793-
/**
794-
* Stuff that only needs to be done once. Builds regular expressions and sorts the reserved words.
795-
*/
796-
private function init() : void
797-
{
798-
if ($this->init) {
799-
return;
800-
}
801-
802762
// Sort reserved word list from longest word to shortest, 3x faster than usort
803763
$reservedMap = array_combine($this->reserved, array_map('strlen', $this->reserved));
804764
assert($reservedMap !== false);
@@ -825,7 +785,28 @@ private function init() : void
825785

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

828-
$this->init = true;
788+
if ($highlighter === null) {
789+
$this->highlighter = $this->isCli() ? new CliHighlighter() : new HtmlHighlighter();
790+
791+
return;
792+
}
793+
794+
$this->highlighter = $highlighter;
795+
}
796+
797+
/**
798+
* Get stats about the token cache
799+
*
800+
* @return mixed[] An array containing the keys 'hits', 'misses', 'entries', and 'size' in bytes
801+
*/
802+
public function getCacheStats() : array
803+
{
804+
return [
805+
'hits'=>$this->cacheHits,
806+
'misses'=>$this->cacheMisses,
807+
'entries'=>count($this->tokenCache),
808+
'size'=>strlen(serialize($this->tokenCache)),
809+
];
829810
}
830811

831812
/**
@@ -1018,8 +999,6 @@ private function getQuotedString(string $string) : string
1018999
*/
10191000
private function tokenize(string $string) : array
10201001
{
1021-
$this->init();
1022-
10231002
$tokens = [];
10241003

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

0 commit comments

Comments
 (0)