Skip to content

Commit bac3bfe

Browse files
committed
extract constants from compress method
A first start to somewhat shorted that ultralong method
1 parent 41ee83c commit bac3bfe

File tree

1 file changed

+56
-48
lines changed

1 file changed

+56
-48
lines changed

src/JSStrip.php

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,62 @@
1515
class JSStrip
1616
{
1717

18+
const REGEX_STARTERS = [
19+
'(', '=', '<', '>', '?', '[', '{', ',', ';', ':', '!', '&', '|', '+', '-', '%', '~', '^',
20+
'return', 'yield', 'else', 'throw', 'await'
21+
];
22+
const WHITESPACE_CHARS = [" ", "\t", "\n", "\r", "\0", "\x0B"];
23+
24+
/** items that don't need spaces next to them */
25+
const CHARS = "^&|!+\-*\/%=\?:;,{}()<>% \t\n\r'\"`[]~^";
26+
1827
/**
19-
* @param string $s
28+
* items which need a space if the sign before and after whitespace is equal.
29+
* E.g. '+ ++' may not be compressed to '+++' --> syntax error.
30+
*/
31+
const OPS = "+-/";
32+
33+
/**
34+
* Compress the given code
35+
*
36+
* @param string $source The JavaScript code to compress
2037
* @return string
2138
*/
22-
function compress($s)
39+
function compress($source)
2340
{
24-
$s = ltrim($s); // strip all initial whitespace
25-
$s .= "\n";
41+
$source = ltrim($source); // strip all initial whitespace
42+
$source .= "\n";
2643
$i = 0; // char index for input string
2744
$j = 0; // char forward index for input string
2845
$line = 0; // line number of file (close to it anyways)
29-
$slen = strlen($s); // size of input string
46+
$slen = strlen($source); // size of input string
3047
$lch = ''; // last char added
3148
$result = ''; // we store the final result here
3249

33-
// items that don't need spaces next to them
34-
$chars = "^&|!+\-*\/%=\?:;,{}()<>% \t\n\r'\"`[]~^";
35-
36-
// items which need a space if the sign before and after whitespace is equal.
37-
// E.g. '+ ++' may not be compressed to '+++' --> syntax error.
38-
$ops = "+-/";
39-
40-
$regex_starters = array("(", "=", "<", ">", "?", "[", "{", ",", ";", ":", "!", "&", "|", "+", "-", "%", "~", "^", "return", "yield", "else", "throw", "await");
41-
$whitespaces_chars = array(" ", "\t", "\n", "\r", "\0", "\x0B");
4250

4351
while ($i < $slen) {
4452
// skip all "boring" characters. This is either
4553
// reserved word (e.g. "for", "else", "if") or a
4654
// variable/object/method (e.g. "foo.color")
47-
while ($i < $slen && (strpos($chars, $s[$i]) === false)) {
48-
$result .= $s[$i];
55+
while ($i < $slen && (strpos(self::CHARS, $source[$i]) === false)) {
56+
$result .= $source[$i];
4957
$i = $i + 1;
5058
}
5159

52-
$ch = $s[$i];
60+
$ch = $source[$i];
5361
// multiline comments (keeping IE conditionals)
54-
if ($ch == '/' && $s[$i + 1] == '*' && $s[$i + 2] != '@') {
55-
$endC = strpos($s, '*/', $i + 2);
62+
if ($ch == '/' && $source[$i + 1] == '*' && $source[$i + 2] != '@') {
63+
$endC = strpos($source, '*/', $i + 2);
5664
if ($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR);
5765

5866
// check if this is a NOCOMPRESS comment
59-
if (substr($s, $i, $endC + 2 - $i) == '/* BEGIN NOCOMPRESS */') {
67+
if (substr($source, $i, $endC + 2 - $i) == '/* BEGIN NOCOMPRESS */') {
6068
// take nested NOCOMPRESS comments into account
6169
$depth = 0;
6270
$nextNC = $endC;
6371
do {
64-
$beginNC = strpos($s, '/* BEGIN NOCOMPRESS */', $nextNC + 2);
65-
$endNC = strpos($s, '/* END NOCOMPRESS */', $nextNC + 2);
72+
$beginNC = strpos($source, '/* BEGIN NOCOMPRESS */', $nextNC + 2);
73+
$endNC = strpos($source, '/* END NOCOMPRESS */', $nextNC + 2);
6674

6775
if ($endNC === false) trigger_error('Found invalid NOCOMPRESS comment', E_USER_ERROR);
6876
if ($beginNC !== false && $beginNC < $endNC) {
@@ -75,7 +83,7 @@ function compress($s)
7583
} while ($depth >= 0);
7684

7785
// verbatim copy contents, trimming but putting it on its own line
78-
$result .= "\n" . trim(substr($s, $i + 22, $endNC - ($i + 22))) . "\n"; // BEGIN comment = 22 chars
86+
$result .= "\n" . trim(substr($source, $i + 22, $endNC - ($i + 22))) . "\n"; // BEGIN comment = 22 chars
7987
$i = $endNC + 20; // END comment = 20 chars
8088
} else {
8189
$i = $endC + 2;
@@ -84,8 +92,8 @@ function compress($s)
8492
}
8593

8694
// singleline
87-
if ($ch == '/' && $s[$i + 1] == '/') {
88-
$endC = strpos($s, "\n", $i + 2);
95+
if ($ch == '/' && $source[$i + 1] == '/') {
96+
$endC = strpos($source, "\n", $i + 2);
8997
if ($endC === false) trigger_error('Invalid comment', E_USER_ERROR);
9098
$i = $endC;
9199
continue;
@@ -95,32 +103,32 @@ function compress($s)
95103
if ($ch == '/') {
96104
// rewind, skip white space
97105
$j = 1;
98-
while (in_array($s[$i - $j], $whitespaces_chars)) {
106+
while (in_array($source[$i - $j], self::WHITESPACE_CHARS)) {
99107
$j = $j + 1;
100108
}
101109
if (current(array_filter(
102-
$regex_starters,
103-
function ($e) use ($s, $i, $j) {
110+
self::REGEX_STARTERS,
111+
function ($e) use ($source, $i, $j) {
104112
$len = strlen($e);
105113
$idx = $i - $j + 1 - $len;
106-
return substr($s, $idx, $len) === $e;
114+
return substr($source, $idx, $len) === $e;
107115
}
108116
))) {
109117
// yes, this is an re
110118
// now move forward and find the end of it
111119
$j = 1;
112120
// we set this flag when inside a character class definition, enclosed by brackets [] where '/' does not terminate the re
113121
$ccd = false;
114-
while ($ccd || $s[$i + $j] != '/') {
115-
if ($s[$i + $j] == '\\') $j = $j + 2;
122+
while ($ccd || $source[$i + $j] != '/') {
123+
if ($source[$i + $j] == '\\') $j = $j + 2;
116124
else {
117125
$j++;
118126
// check if we entered/exited a character class definition and set flag accordingly
119-
if ($s[$i + $j - 1] == '[') $ccd = true;
120-
else if ($s[$i + $j - 1] == ']') $ccd = false;
127+
if ($source[$i + $j - 1] == '[') $ccd = true;
128+
else if ($source[$i + $j - 1] == ']') $ccd = false;
121129
}
122130
}
123-
$result .= substr($s, $i, $j + 1);
131+
$result .= substr($source, $i, $j + 1);
124132
$i = $i + $j + 1;
125133
continue;
126134
}
@@ -129,14 +137,14 @@ function ($e) use ($s, $i, $j) {
129137
// double quote strings
130138
if ($ch == '"') {
131139
$j = 1;
132-
while (($i + $j < $slen) && $s[$i + $j] != '"') {
133-
if ($s[$i + $j] == '\\' && ($s[$i + $j + 1] == '"' || $s[$i + $j + 1] == '\\')) {
140+
while (($i + $j < $slen) && $source[$i + $j] != '"') {
141+
if ($source[$i + $j] == '\\' && ($source[$i + $j + 1] == '"' || $source[$i + $j + 1] == '\\')) {
134142
$j += 2;
135143
} else {
136144
$j += 1;
137145
}
138146
}
139-
$string = substr($s, $i, $j + 1);
147+
$string = substr($source, $i, $j + 1);
140148
// remove multiline markers:
141149
$string = str_replace("\\\n", '', $string);
142150
$result .= $string;
@@ -147,14 +155,14 @@ function ($e) use ($s, $i, $j) {
147155
// single quote strings
148156
if ($ch == "'") {
149157
$j = 1;
150-
while (($i + $j < $slen) && $s[$i + $j] != "'") {
151-
if ($s[$i + $j] == '\\' && ($s[$i + $j + 1] == "'" || $s[$i + $j + 1] == '\\')) {
158+
while (($i + $j < $slen) && $source[$i + $j] != "'") {
159+
if ($source[$i + $j] == '\\' && ($source[$i + $j + 1] == "'" || $source[$i + $j + 1] == '\\')) {
152160
$j += 2;
153161
} else {
154162
$j += 1;
155163
}
156164
}
157-
$string = substr($s, $i, $j + 1);
165+
$string = substr($source, $i, $j + 1);
158166
// remove multiline markers:
159167
$string = str_replace("\\\n", '', $string);
160168
$result .= $string;
@@ -165,14 +173,14 @@ function ($e) use ($s, $i, $j) {
165173
// backtick strings
166174
if ($ch == "`") {
167175
$j = 1;
168-
while (($i + $j < $slen) && $s[$i + $j] != "`") {
169-
if ($s[$i + $j] == '\\' && ($s[$i + $j + 1] == "`" || $s[$i + $j + 1] == '\\')) {
176+
while (($i + $j < $slen) && $source[$i + $j] != "`") {
177+
if ($source[$i + $j] == '\\' && ($source[$i + $j + 1] == "`" || $source[$i + $j + 1] == '\\')) {
170178
$j += 2;
171179
} else {
172180
$j += 1;
173181
}
174182
}
175-
$string = substr($s, $i, $j + 1);
183+
$string = substr($source, $i, $j + 1);
176184
// remove multiline markers:
177185
$string = str_replace("\\\n", '', $string);
178186
$result .= $string;
@@ -186,18 +194,18 @@ function ($e) use ($s, $i, $j) {
186194

187195
// Only consider deleting whitespace if the signs before and after
188196
// are not equal and are not an operator which may not follow itself.
189-
if ($i + 1 < $slen && ((!$lch || $s[$i + 1] == ' ')
190-
|| $lch != $s[$i + 1]
191-
|| strpos($ops, $s[$i + 1]) === false)) {
197+
if ($i + 1 < $slen && ((!$lch || $source[$i + 1] == ' ')
198+
|| $lch != $source[$i + 1]
199+
|| strpos(self::OPS, $source[$i + 1]) === false)) {
192200
// leading spaces
193-
if ($i + 1 < $slen && (strpos($chars, $s[$i + 1]) !== false)) {
201+
if ($i + 1 < $slen && (strpos(self::CHARS, $source[$i + 1]) !== false)) {
194202
$i = $i + 1;
195203
continue;
196204
}
197205
// trailing spaces
198206
// if this ch is space AND the last char processed
199207
// is special, then skip the space
200-
if ($lch && (strpos($chars, $lch) !== false)) {
208+
if ($lch && (strpos(self::CHARS, $lch) !== false)) {
201209
$i = $i + 1;
202210
continue;
203211
}

0 commit comments

Comments
 (0)