1
1
<?php
2
+ declare (strict_types = 1 );
2
3
namespace hexydec \html ;
3
4
4
5
class cssmin {
5
6
6
- protected static $ tokens = Array (
7
+ protected static $ tokens = [
7
8
'whitespace ' => '\s++ ' ,
8
9
'comment ' => '\\/ \\*[\d\D]*? \\* \\/ ' ,
9
10
'quotes ' => '(?<! \\\\)("(?:[^" \\\\]++| \\\\.)*+"| \'(?:[^ \'\\\\]++| \\\\.)*+ \') ' ,
@@ -19,9 +20,9 @@ class cssmin {
19
20
'colon ' => ': ' ,
20
21
'semicolon ' => '; ' ,
21
22
'string ' => '!?[^\[\]{}\(\):;,>+=~\^$!"\/ \n\r\t]++ '
22
- ) ;
23
+ ] ;
23
24
24
- protected static $ config = Array (
25
+ protected static $ config = [
25
26
'removesemicolon ' => true ,
26
27
'removezerounits ' => true ,
27
28
'removeleadingzero ' => true ,
@@ -32,9 +33,9 @@ class cssmin {
32
33
'email ' => false ,
33
34
'maxline ' => false ,
34
35
'output ' => 'minify '
35
- ) ;
36
+ ] ;
36
37
37
- public static function minify (string $ code , array $ config = Array ()) {
38
+ public static function minify (string $ code , array $ config = []) : string {
38
39
$ config = array_merge (self ::$ config , $ config );
39
40
40
41
// set email options
@@ -66,12 +67,12 @@ public static function minify(string $code, array $config = Array()) {
66
67
}
67
68
68
69
protected static function parse (array &$ tokens ) {
69
- $ rules = Array () ;
70
+ $ rules = [] ;
70
71
$ select = true ;
71
72
$ comment = null ;
72
73
$ media = false ;
73
- $ selectors = Array () ;
74
- $ properties = Array () ;
74
+ $ selectors = [] ;
75
+ $ properties = [] ;
75
76
$ token = current ($ tokens );
76
77
do {
77
78
@@ -84,11 +85,11 @@ protected static function parse(array &$tokens) {
84
85
return $ rules ;
85
86
} elseif (in_array ($ token ['type ' ], ['string ' , 'colon ' ])) {
86
87
if ($ token ['value ' ] == '@media ' ) {
87
- $ rules [] = Array (
88
+ $ rules [] = [
88
89
'media ' => self ::parseMediaQuery ($ tokens ),
89
90
'rules ' => self ::parse ($ tokens ),
90
91
'comment ' => $ comment
91
- ) ;
92
+ ] ;
92
93
$ comment = false ;
93
94
} else {
94
95
// prev($tokens);
@@ -106,28 +107,28 @@ protected static function parse(array &$tokens) {
106
107
while (($ token = next ($ tokens )) !== false ) {
107
108
if ($ token ['type ' ] == 'colon ' ) {
108
109
$ important = false ;
109
- $ properties [] = Array (
110
+ $ properties [] = [
110
111
'property ' => $ prop ,
111
112
'value ' => self ::parsePropertyValue ($ tokens , $ important , $ propcomment ),
112
113
'important ' => $ important ,
113
114
'semicolon ' => '; ' ,
114
115
'comment ' => $ propcomment
115
- ) ;
116
+ ] ;
116
117
break ;
117
118
}
118
119
}
119
120
120
121
// end rule
121
122
} elseif ($ token ['type ' ] == 'curlyclose ' ) {
122
123
if ($ selectors && $ properties ) {
123
- $ rules [] = Array (
124
+ $ rules [] = [
124
125
'selectors ' => $ selectors ,
125
126
'properties ' => $ properties ,
126
127
'comment ' => $ comment
127
- ) ;
128
+ ] ;
128
129
}
129
- $ selectors = Array () ;
130
- $ properties = Array () ;
130
+ $ selectors = [] ;
131
+ $ properties = [] ;
131
132
$ select = true ;
132
133
$ comment = false ;
133
134
}
@@ -136,13 +137,13 @@ protected static function parse(array &$tokens) {
136
137
}
137
138
138
139
protected static function parseMediaQuery (array &$ tokens ) {
139
- $ media = Array () ;
140
- $ default = $ rule = Array (
140
+ $ media = [] ;
141
+ $ default = $ rule = [
141
142
'media ' => false ,
142
143
'only ' => false ,
143
144
'not ' => false ,
144
- 'properties ' => Array ()
145
- ) ;
145
+ 'properties ' => []
146
+ ] ;
146
147
while (($ token = next ($ tokens )) !== false ) {
147
148
switch ($ token ['type ' ]) {
148
149
case 'string ' :
@@ -197,7 +198,7 @@ protected static function parseMediaQuery(array &$tokens) {
197
198
}
198
199
199
200
protected static function parseSelectors (array &$ tokens ) {
200
- $ selector = Array () ;
201
+ $ selector = [] ;
201
202
$ join = false ;
202
203
$ token = current ($ tokens );
203
204
do {
@@ -208,10 +209,10 @@ protected static function parseSelectors(array &$tokens) {
208
209
}
209
210
break ;
210
211
case 'string ' :
211
- $ selector [] = Array (
212
+ $ selector [] = [
212
213
'selector ' => $ token ['value ' ],
213
214
'join ' => $ join
214
- ) ;
215
+ ] ;
215
216
$ join = false ;
216
217
break ;
217
218
case 'colon ' :
@@ -230,7 +231,7 @@ protected static function parseSelectors(array &$tokens) {
230
231
}
231
232
232
233
// capture selector
233
- } elseif (!in_array ($ token ['type ' ], Array ( 'whitespace ' , 'comma ' , 'curlyopen ' ) )) {
234
+ } elseif (!in_array ($ token ['type ' ], [ 'whitespace ' , 'comma ' , 'curlyopen ' ] )) {
234
235
$ parts .= $ token ['value ' ];
235
236
236
237
// stop here
@@ -241,10 +242,10 @@ protected static function parseSelectors(array &$tokens) {
241
242
}
242
243
243
244
// save selector
244
- $ selector [] = Array (
245
+ $ selector [] = [
245
246
'selector ' => $ parts ,
246
247
'join ' => $ join
247
- ) ;
248
+ ] ;
248
249
$ join = false ;
249
250
break ;
250
251
case 'squareopen ' :
@@ -259,10 +260,10 @@ protected static function parseSelectors(array &$tokens) {
259
260
}
260
261
}
261
262
}
262
- $ selector [] = Array (
263
+ $ selector [] = [
263
264
'selector ' => '[ ' .$ parts .'] ' ,
264
265
'join ' => $ join
265
- ) ;
266
+ ] ;
266
267
$ join = false ;
267
268
break ;
268
269
case 'curlyopen ' :
@@ -277,19 +278,19 @@ protected static function parseSelectors(array &$tokens) {
277
278
}
278
279
279
280
protected static function parsePropertyValue (array &$ tokens , bool &$ important = false , string &$ comment = null ) {
280
- $ properties = Array () ;
281
- $ values = Array () ;
281
+ $ properties = [] ;
282
+ $ values = [] ;
282
283
$ important = false ;
283
284
$ comment = null ;
284
285
while (($ token = next ($ tokens )) !== false ) {
285
286
if ($ token ['type ' ] == 'comma ' ) {
286
287
$ properties [] = $ values ;
287
- $ values = Array () ;
288
+ $ values = [] ;
288
289
} elseif ($ token ['value ' ] == '!important ' ) {
289
290
$ important = true ;
290
291
} elseif ($ token ['type ' ] == 'bracketopen ' ) {
291
292
$ values [] = self ::parsePropertyValue ($ tokens );
292
- } elseif (in_array ($ token ['type ' ], Array ( 'semicolon ' , 'bracketclose ' ) )) {
293
+ } elseif (in_array ($ token ['type ' ], [ 'semicolon ' , 'bracketclose ' ] )) {
293
294
while (($ token = next ($ tokens )) !== false ) {
294
295
if ($ token ['type ' ] == 'comment ' ) {
295
296
$ comment = $ token ['value ' ];
@@ -460,8 +461,8 @@ protected static function compile(array $ast, array $config, int $indent = 0) {
460
461
return rtrim ($ css );
461
462
}
462
463
463
- protected static function compileProperty (array $ value , int $ b , string $ join = ' ' ) {
464
- $ properties = Array () ;
464
+ protected static function compileProperty (array $ value , bool $ b = false , string $ join = ' ' ) {
465
+ $ properties = [] ;
465
466
foreach ($ value AS $ group ) {
466
467
$ compiled = '' ;
467
468
foreach ($ group AS $ item ) {
0 commit comments