Skip to content

Commit af0acf7

Browse files
committed
Small refactors
1 parent 783e105 commit af0acf7

File tree

2 files changed

+73
-43
lines changed

2 files changed

+73
-43
lines changed

src/Tokenizer/Parser/UserDefined.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,35 @@ protected static function getUserDefinedVariableString(&$string)
5555
Tokenizer::TOKEN_TYPE => Tokenizer::TOKEN_TYPE_VARIABLE
5656
];
5757

58+
self::setTokenValueStartingWithAtSymbolAndWrapped($returnData, $string);
59+
self::setTokenValueStartingWithAtSymbol($returnData, $string);
60+
61+
return $returnData;
62+
}
63+
64+
/**
65+
* @param array $returnData
66+
* @param string $string
67+
*/
68+
protected static function setTokenValueStartingWithAtSymbolAndWrapped(array &$returnData, $string)
69+
{
5870
if ($string[1] === '"' || $string[1] === '\'' || $string[1] === '`') {
5971
$returnData[Tokenizer::TOKEN_VALUE] = '@' . Quoted::wrapStringWithQuotes(substr($string, 1));
60-
return $returnData;
6172
}
73+
}
6274

63-
$matches = [];
64-
preg_match('/^(@[a-zA-Z0-9\._\$]+)/', $string, $matches);
65-
if ($matches) {
66-
$returnData[Tokenizer::TOKEN_VALUE] = $matches[1];
75+
/**
76+
* @param array $returnData
77+
* @param string $string
78+
*/
79+
protected static function setTokenValueStartingWithAtSymbol(array &$returnData, $string)
80+
{
81+
if (null === $returnData[Tokenizer::TOKEN_VALUE]) {
82+
$matches = [];
83+
preg_match('/^(@[a-zA-Z0-9\._\$]+)/', $string, $matches);
84+
if ($matches) {
85+
$returnData[Tokenizer::TOKEN_VALUE] = $matches[1];
86+
}
6787
}
68-
69-
return $returnData;
7088
}
7189
}

src/Tokenizer/Tokenizer.php

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class Tokenizer
7777
*/
7878
protected $tokenCache = [];
7979

80-
8180
/**
8281
* @var array
8382
*/
@@ -115,6 +114,7 @@ protected function initRegex($variable)
115114
* Each token is an associative array with type and value.
116115
*
117116
* @param string $string
117+
*
118118
* @return array
119119
*/
120120
public function tokenize($string)
@@ -131,17 +131,11 @@ public function tokenize($string)
131131
break;
132132
}
133133

134-
$oldStringLength = $currentStringLength;
135-
136-
$cacheKey = $this->useTokenCache($string, $currentStringLength);
137-
if (!empty($cacheKey) && isset($this->tokenCache[$cacheKey])) {
138-
$token = $this->getNextTokenFromCache($cacheKey);
139-
} else {
140-
$token = $this->getNextTokenFromString($string, $token, $cacheKey);
141-
}
142-
134+
$token = $this->getToken($string, $currentStringLength, $token);
143135
$tokens[] = $token;
144136
$tokenLength = strlen($token[self::TOKEN_VALUE]);
137+
138+
$oldStringLength = $currentStringLength;
145139
$currentStringLength -= $tokenLength;
146140

147141
$string = substr($string, $tokenLength);
@@ -198,25 +192,6 @@ protected function getNextTokenFromString($string, $token, $cacheKey)
198192
return $token;
199193
}
200194

201-
/**
202-
* @param array $nextToken
203-
*
204-
* @return $this
205-
*/
206-
public function setNextToken($nextToken)
207-
{
208-
$this->nextToken = $nextToken;
209-
return $this;
210-
}
211-
212-
/**
213-
* @return array
214-
*/
215-
public function getNextToken()
216-
{
217-
return $this->nextToken;
218-
}
219-
220195
/**
221196
* Return the next token and token type in a SQL string.
222197
* Quoted strings, comments, reserved words, whitespace, and punctuation are all their own tokens.
@@ -228,7 +203,7 @@ public function getNextToken()
228203
*/
229204
protected function parseNextToken($string, $previous = null)
230205
{
231-
$matches = [];
206+
$matches = [];
232207
$this->nextToken = [];
233208

234209
WhiteSpace::isWhiteSpace($this, $string, $matches);
@@ -245,15 +220,22 @@ protected function parseNextToken($string, $previous = null)
245220
}
246221

247222
/**
248-
* Helper function for building regular expressions for reserved words and boundary characters
249-
*
250-
* @param string $string
223+
* @return array
224+
*/
225+
public function getNextToken()
226+
{
227+
return $this->nextToken;
228+
}
229+
230+
/**
231+
* @param array $nextToken
251232
*
252-
* @return string
233+
* @return $this
253234
*/
254-
protected function quoteRegex($string)
235+
public function setNextToken($nextToken)
255236
{
256-
return preg_quote($string, '/');
237+
$this->nextToken = $nextToken;
238+
return $this;
257239
}
258240

259241
/**
@@ -295,4 +277,34 @@ public function getRegexReservedTopLevel()
295277
{
296278
return $this->regexReservedTopLevel;
297279
}
280+
281+
/**
282+
* Helper function for building regular expressions for reserved words and boundary characters
283+
*
284+
* @param string $string
285+
*
286+
* @return string
287+
*/
288+
protected function quoteRegex($string)
289+
{
290+
return preg_quote($string, '/');
291+
}
292+
293+
/**
294+
* @param $string
295+
* @param $currentStringLength
296+
* @param $token
297+
*
298+
* @return array|mixed
299+
*/
300+
protected function getToken($string, $currentStringLength, $token)
301+
{
302+
$cacheKey = $this->useTokenCache($string, $currentStringLength);
303+
if (!empty($cacheKey) && isset($this->tokenCache[$cacheKey])) {
304+
$token = $this->getNextTokenFromCache($cacheKey);
305+
} else {
306+
$token = $this->getNextTokenFromString($string, $token, $cacheKey);
307+
}
308+
return $token;
309+
}
298310
}

0 commit comments

Comments
 (0)