Skip to content

Commit 8f37126

Browse files
committed
Small refactors
1 parent b097709 commit 8f37126

File tree

2 files changed

+40
-49
lines changed

2 files changed

+40
-49
lines changed

src/Tokenizer/Parser/Reserved.php

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,31 @@
1919
final class Reserved
2020
{
2121
/**
22-
* @param Tokenizer $tokenizer
23-
* @param string $string
24-
* @param array $matches
22+
* @var array
23+
*/
24+
protected static $regex = [
25+
Tokenizer::TOKEN_TYPE_RESERVED_TOP_LEVEL => 'getRegexReservedTopLevel',
26+
Tokenizer::TOKEN_TYPE_RESERVED_NEWLINE => 'getRegexReservedNewLine',
27+
Tokenizer::TOKEN_TYPE_RESERVED => 'getRegexReserved'
28+
];
29+
30+
/**
31+
* @param Tokenizer $tokenizer
32+
* @param string $string
2533
* @param array|null $previous
2634
*
2735
* @return array
2836
*/
29-
public static function isReserved(Tokenizer $tokenizer, $string, array &$matches, $previous)
37+
public static function isReserved(Tokenizer $tokenizer, $string, $previous)
3038
{
3139
$tokenData = [];
3240

3341
if (!$tokenizer->getNextToken() && self::isReservedPrecededByDotCharacter($previous)) {
34-
self::getReservedString(
35-
$tokenData,
36-
Tokenizer::TOKEN_TYPE_RESERVED_TOP_LEVEL,
37-
$string,
38-
$matches,
39-
$tokenizer->getRegexReservedTopLevel(),
40-
$tokenizer->getRegexBoundaries()
41-
);
42+
$upperCase = strtoupper($string);
4243

43-
self::getReservedString(
44-
$tokenData,
45-
Tokenizer::TOKEN_TYPE_RESERVED_NEWLINE,
46-
strtoupper($string),
47-
$matches,
48-
$tokenizer->getRegexReservedNewLine(),
49-
$tokenizer->getRegexBoundaries()
50-
);
51-
52-
self::getReservedString(
53-
$tokenData,
54-
Tokenizer::TOKEN_TYPE_RESERVED,
55-
$string,
56-
$matches,
57-
$tokenizer->getRegexReserved(),
58-
$tokenizer->getRegexBoundaries()
59-
);
44+
self::getReservedString($tokenData, Tokenizer::TOKEN_TYPE_RESERVED_TOP_LEVEL, $string, $tokenizer);
45+
self::getReservedString($tokenData, Tokenizer::TOKEN_TYPE_RESERVED_NEWLINE, $upperCase, $tokenizer);
46+
self::getReservedString($tokenData, Tokenizer::TOKEN_TYPE_RESERVED, $string, $tokenizer);
6047

6148
$tokenizer->setNextToken($tokenData);
6249
}
@@ -74,6 +61,27 @@ protected static function isReservedPrecededByDotCharacter($previous)
7461
return !$previous || !isset($previous[Tokenizer::TOKEN_VALUE]) || $previous[Tokenizer::TOKEN_VALUE] !== '.';
7562
}
7663

64+
/**
65+
* @param array $tokenData
66+
* @param $type
67+
* @param $string
68+
* @param Tokenizer $tokenizer
69+
*/
70+
protected static function getReservedString(array &$tokenData, $type, $string, Tokenizer $tokenizer)
71+
{
72+
$matches = [];
73+
$method = self::$regex[$type];
74+
75+
if (empty($tokenData) && self::isReservedString(
76+
$string,
77+
$matches,
78+
$tokenizer->$method(),
79+
$tokenizer->getRegexBoundaries()
80+
)
81+
) {
82+
$tokenData = self::getStringTypeArray($type, $string, $matches);
83+
}
84+
}
7785

7886
/**
7987
* @param string $upper
@@ -94,8 +102,8 @@ protected static function isReservedString($upper, array &$matches, $regexReserv
94102

95103
/**
96104
* @param string $type
97-
* @param string $string
98-
* @param array $matches
105+
* @param string $string
106+
* @param array $matches
99107
*
100108
* @return array
101109
*/
@@ -106,21 +114,4 @@ protected static function getStringTypeArray($type, $string, array &$matches)
106114
Tokenizer::TOKEN_VALUE => substr($string, 0, strlen($matches[1]))
107115
];
108116
}
109-
110-
/**
111-
* @param array $tokenData
112-
* @param string string
113-
* @param string $string
114-
* @param array $matches
115-
* @param string string
116-
* @param string string
117-
*
118-
* @return array
119-
*/
120-
protected static function getReservedString(array &$tokenData, $type, $string, array &$matches, $regex, $boundaries)
121-
{
122-
if (empty($tokenData) && self::isReservedString($string, $matches, $regex, $boundaries)) {
123-
$tokenData = self::getStringTypeArray($type, $string, $matches);
124-
}
125-
}
126117
}

src/Tokenizer/Tokenizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ protected function parseNextToken($string, $previous = null)
270270
UserDefined::isUserDefinedVariable($this, $string);
271271
Numeral::isNumeral($this, $string, $matches);
272272
Boundary::isBoundary($this, $string, $matches);
273-
Reserved::isReserved($this, $string, $matches, $previous);
273+
Reserved::isReserved($this, $string, $previous);
274274
String::isFunction($this, $string, $matches);
275275
String::getNonReservedString($this, $string, $matches);
276276

0 commit comments

Comments
 (0)