@@ -84,11 +84,7 @@ public function format($sql)
84
84
85
85
foreach ($ tokens as $ i => $ token ) {
86
86
$ queryValue = $ token [Tokenizer::TOKEN_VALUE ];
87
-
88
- $ this ->indentation
89
- ->increaseSpecialIndent ()
90
- ->increaseBlockIndent ();
91
-
87
+ $ this ->indentation ->increaseSpecialIndent ()->increaseBlockIndent ();
92
88
$ addedNewline = $ this ->newLine ->addNewLineBreak ($ tab );
93
89
94
90
if ($ this ->comment ->stringHasCommentToken ($ token )) {
@@ -105,82 +101,40 @@ public function format($sql)
105
101
$ this ->inlineCount += strlen ($ token [Tokenizer::TOKEN_VALUE ]);
106
102
}
107
103
108
- if ($ this ->parentheses ->stringIsOpeningParentheses ($ token )) {
109
- $ length = 0 ;
110
- for ($ j = 1 ; $ j <= 250 ; $ j ++) {
111
- if (isset ($ tokens [$ i + $ j ])) {
112
- $ next = $ tokens [$ i + $ j ];
113
- if ($ this ->parentheses ->stringIsClosingParentheses ($ next )) {
114
- $ this ->parentheses ->writeNewInlineParentheses ();
115
- break ;
116
- }
117
-
118
- if ($ this ->parentheses ->invalidParenthesesTokenValue ($ next )
119
- || $ this ->parentheses ->invalidParenthesesTokenType ($ next )
120
- ) {
121
- break ;
122
- }
123
-
124
- $ length += strlen ($ next [Tokenizer::TOKEN_VALUE ]);
125
- }
126
- }
127
- $ this ->newLine ->writeNewLineForLongInlineValues ($ length );
104
+ switch ($ token ) {
105
+ case $ this ->parentheses ->stringIsOpeningParentheses ($ token ):
106
+ $ tokens = $ this ->formatOpeningParenthesis ($ token , $ i , $ tokens , $ originalTokens );
107
+ break ;
128
108
129
- if (WhiteSpace::isPrecedingCurrentTokenOfTokenTypeWhiteSpace ($ originalTokens , $ token )) {
130
- $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
131
- }
109
+ case $ this ->parentheses ->stringIsClosingParentheses ($ token ):
110
+ $ this ->indentation ->decreaseIndentLevelUntilIndentTypeIsSpecial ($ this );
111
+ $ this ->newLine ->addNewLineBeforeToken ($ addedNewline , $ tab );
112
+ break ;
132
113
133
- $ this ->newLine ->addNewLineAfterOpeningParentheses ();
134
- } elseif ($ this ->parentheses ->stringIsClosingParentheses ($ token )) {
135
- $ this ->indentation ->decreaseIndentLevelUntilIndentTypeIsSpecial ($ this );
136
- $ this ->newLine ->addNewLineBeforeToken ($ addedNewline , $ tab );
137
- } elseif (Token::isTokenTypeReservedTopLevel ($ token )) {
138
- $ this ->indentation
139
- ->setIncreaseSpecialIndent (true )
140
- ->decreaseSpecialIndentIfCurrentIndentTypeIsSpecial ();
141
-
142
- $ this ->newLine ->writeNewLineBecauseOfTopLevelReservedWord ($ addedNewline , $ tab );
143
-
144
- if (WhiteSpace::tokenHasExtraWhiteSpaces ($ token )) {
145
- $ queryValue = preg_replace ('/\s+/ ' , ' ' , $ queryValue );
146
- }
147
- Token::tokenHasLimitClause ($ token , $ this ->parentheses , $ this );
148
- } elseif ($ this ->stringIsEndOfLimitClause ($ token )) {
149
- $ this ->clauseLimit = false ;
150
- } elseif (
151
- $ token [Tokenizer::TOKEN_VALUE ] === ', '
152
- && false === $ this ->parentheses ->getInlineParentheses ()
153
- ) {
154
- $ this ->newLine ->writeNewLineBecauseOfComma ();
155
- } elseif ($ this ->newLine ->isTokenTypeReservedNewLine ($ token )) {
156
- $ this ->newLine ->addNewLineBeforeToken ($ addedNewline , $ tab );
157
-
158
- if (WhiteSpace::tokenHasExtraWhiteSpaces ($ token )) {
159
- $ queryValue = preg_replace ('/\s+/ ' , ' ' , $ queryValue );
160
- }
161
- }
114
+ case $ this ->stringIsEndOfLimitClause ($ token ):
115
+ $ this ->clauseLimit = false ;
116
+ break ;
162
117
163
- if (Token:: tokenHasMultipleBoundaryCharactersTogether ( $ token, $ tokens , $ i , $ originalTokens )) {
164
- $ this -> formattedSql = rtrim ( $ this ->formattedSql , ' ' );
165
- }
118
+ case $ token[Tokenizer:: TOKEN_VALUE ] === ' , ' && false === $ this -> parentheses -> getInlineParentheses ():
119
+ $ this ->newLine -> writeNewLineBecauseOfComma ( );
120
+ break ;
166
121
167
- if (WhiteSpace:: tokenHasExtraWhiteSpaceLeft ($ token )) {
168
- $ this -> formattedSql = rtrim ( $ this ->formattedSql , ' ' );
169
- }
122
+ case Token:: isTokenTypeReservedTopLevel ($ token ):
123
+ $ queryValue = $ this ->formatTokenTypeReservedTopLevel ( $ addedNewline , $ tab , $ token , $ queryValue );
124
+ break ;
170
125
171
- $ this ->formattedSql .= $ queryValue . ' ' ;
126
+ case $ this ->newLine ->isTokenTypeReservedNewLine ($ token ):
127
+ $ this ->newLine ->addNewLineBeforeToken ($ addedNewline , $ tab );
172
128
173
- if (WhiteSpace::tokenHasExtraWhiteSpaceRight ($ token )) {
174
- $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
129
+ if (WhiteSpace::tokenHasExtraWhiteSpaces ($ token )) {
130
+ $ queryValue = preg_replace ('/\s+/ ' , ' ' , $ queryValue );
131
+ }
132
+ break ;
175
133
}
176
134
177
- if (Token::tokenIsMinusSign ($ token , $ tokens , $ i )) {
178
- $ previousTokenType = $ tokens [$ i - 1 ][Tokenizer::TOKEN_TYPE ];
179
-
180
- if (WhiteSpace::tokenIsNumberAndHasExtraWhiteSpaceRight ($ previousTokenType )) {
181
- $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
182
- }
183
- }
135
+ $ this ->formatBoundaryCharacterToken ($ token , $ i , $ tokens , $ originalTokens );
136
+ $ this ->formatWhiteSpaceToken ($ token , $ queryValue );
137
+ $ this ->formatDashToken ($ token , $ i , $ tokens );
184
138
}
185
139
186
140
return trim (str_replace (["\t" , " \n" ], [$ this ->tab , "\n" ], $ this ->formattedSql )) . "\n" ;
@@ -200,6 +154,44 @@ public function reset()
200
154
$ this ->formattedSql = '' ;
201
155
}
202
156
157
+ /**
158
+ * @param $token
159
+ * @param $i
160
+ * @param array $tokens
161
+ * @param array $originalTokens
162
+ *
163
+ * @return array
164
+ */
165
+ protected function formatOpeningParenthesis ($ token , $ i , array &$ tokens , array &$ originalTokens )
166
+ {
167
+ $ length = 0 ;
168
+ for ($ j = 1 ; $ j <= 250 ; $ j ++) {
169
+ if (isset ($ tokens [$ i + $ j ])) {
170
+ $ next = $ tokens [$ i + $ j ];
171
+ if ($ this ->parentheses ->stringIsClosingParentheses ($ next )) {
172
+ $ this ->parentheses ->writeNewInlineParentheses ();
173
+ break ;
174
+ }
175
+
176
+ if ($ this ->parentheses ->invalidParenthesesTokenValue ($ next )
177
+ || $ this ->parentheses ->invalidParenthesesTokenType ($ next )
178
+ ) {
179
+ break ;
180
+ }
181
+
182
+ $ length += strlen ($ next [Tokenizer::TOKEN_VALUE ]);
183
+ }
184
+ }
185
+ $ this ->newLine ->writeNewLineForLongInlineValues ($ length );
186
+
187
+ if (WhiteSpace::isPrecedingCurrentTokenOfTokenTypeWhiteSpace ($ originalTokens , $ token )) {
188
+ $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
189
+ }
190
+
191
+ $ this ->newLine ->addNewLineAfterOpeningParentheses ();
192
+
193
+ return $ tokens ;
194
+ }
203
195
204
196
/**
205
197
* @param $token
@@ -214,6 +206,75 @@ protected function stringIsEndOfLimitClause($token)
214
206
&& $ token [Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_WHITESPACE ;
215
207
}
216
208
209
+ /**
210
+ * @param $addedNewline
211
+ * @param $tab
212
+ * @param $token
213
+ * @param $queryValue
214
+ *
215
+ * @return mixed
216
+ */
217
+ protected function formatTokenTypeReservedTopLevel ($ addedNewline , $ tab , $ token , $ queryValue )
218
+ {
219
+ $ this ->indentation
220
+ ->setIncreaseSpecialIndent (true )
221
+ ->decreaseSpecialIndentIfCurrentIndentTypeIsSpecial ();
222
+
223
+ $ this ->newLine ->writeNewLineBecauseOfTopLevelReservedWord ($ addedNewline , $ tab );
224
+
225
+ if (WhiteSpace::tokenHasExtraWhiteSpaces ($ token )) {
226
+ $ queryValue = preg_replace ('/\s+/ ' , ' ' , $ queryValue );
227
+ }
228
+ Token::tokenHasLimitClause ($ token , $ this ->parentheses , $ this );
229
+ return $ queryValue ;
230
+ }
231
+
232
+ /**
233
+ * @param $token
234
+ * @param $i
235
+ * @param array $tokens
236
+ * @param array $originalTokens
237
+ */
238
+ protected function formatBoundaryCharacterToken ($ token , $ i , array &$ tokens , array &$ originalTokens )
239
+ {
240
+ if (Token::tokenHasMultipleBoundaryCharactersTogether ($ token , $ tokens , $ i , $ originalTokens )) {
241
+ $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
242
+ }
243
+ }
244
+
245
+ /**
246
+ * @param $token
247
+ * @param $queryValue
248
+ */
249
+ protected function formatWhiteSpaceToken ($ token , $ queryValue )
250
+ {
251
+ if (WhiteSpace::tokenHasExtraWhiteSpaceLeft ($ token )) {
252
+ $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
253
+ }
254
+
255
+ $ this ->formattedSql .= $ queryValue . ' ' ;
256
+
257
+ if (WhiteSpace::tokenHasExtraWhiteSpaceRight ($ token )) {
258
+ $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
259
+ }
260
+ }
261
+
262
+ /**
263
+ * @param $token
264
+ * @param $i
265
+ * @param array $tokens
266
+ */
267
+ protected function formatDashToken ($ token , $ i , array &$ tokens )
268
+ {
269
+ if (Token::tokenIsMinusSign ($ token , $ tokens , $ i )) {
270
+ $ previousTokenType = $ tokens [$ i - 1 ][Tokenizer::TOKEN_TYPE ];
271
+
272
+ if (WhiteSpace::tokenIsNumberAndHasExtraWhiteSpaceRight ($ previousTokenType )) {
273
+ $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
274
+ }
275
+ }
276
+ }
277
+
217
278
/**
218
279
* @return string
219
280
*/
0 commit comments