99 */
1010namespace NilPortugues \SqlQueryFormatter ;
1111
12+ use NilPortugues \SqlQueryFormatter \Helper \Comment ;
1213use NilPortugues \SqlQueryFormatter \Helper \Indent ;
1314use NilPortugues \SqlQueryFormatter \Helper \NewLine ;
1415use NilPortugues \SqlQueryFormatter \Helper \Parentheses ;
@@ -60,6 +61,11 @@ class Formatter
6061 */
6162 protected $ indentation ;
6263
64+ /**
65+ * @var Comment
66+ */
67+ protected $ comment ;
68+
6369 /**
6470 * Returns a SQL string in a readable human-friendly format.
6571 *
@@ -83,8 +89,8 @@ public function format($sql)
8389
8490 $ addedNewline = $ this ->newLine ->addNewLineBreak ($ tab );
8591
86- if ($ this ->stringHasCommentToken ($ token )) {
87- $ this ->formattedSql = $ this ->writeCommentBlock ($ token , $ tab , $ queryValue );
92+ if ($ this ->comment -> stringHasCommentToken ($ token )) {
93+ $ this ->formattedSql = $ this ->comment -> writeCommentBlock ($ token , $ tab , $ queryValue );
8894 continue ;
8995 }
9096
@@ -127,8 +133,10 @@ public function format($sql)
127133 $ this ->indentation ->decreaseIndentLevelUntilIndentTypeIsSpecial ($ this );
128134 $ this ->newLine ->addNewLineBeforeClosingParentheses ($ addedNewline , $ tab );
129135 } elseif ($ this ->isTokenTypeReservedTopLevel ($ token )) {
130- $ this ->indentation ->setIncreaseSpecialIndent (true );
131- $ this ->indentation ->decreaseSpecialIndentIfCurrentIndentTypeIsSpecial ($ this );
136+ $ this ->indentation
137+ ->setIncreaseSpecialIndent (true )
138+ ->decreaseSpecialIndentIfCurrentIndentTypeIsSpecial ($ this );
139+
132140 $ this ->newLine ->writeNewLineBecauseOfTopLevelReservedWord ($ addedNewline , $ tab );
133141
134142 if (WhiteSpace::tokenHasExtraWhiteSpaces ($ token )) {
@@ -185,44 +193,12 @@ public function reset()
185193 $ this ->indentation = new Indent ();
186194 $ this ->parentheses = new Parentheses ($ this , $ this ->indentation );
187195 $ this ->newLine = new NewLine ($ this , $ this ->indentation , $ this ->parentheses );
196+ $ this ->comment = new Comment ($ this , $ this ->indentation , $ this ->newLine );
188197
189198 $ this ->formattedSql = '' ;
190199 }
191200
192201
193- /**
194- * @param $token
195- *
196- * @return bool
197- */
198- protected function stringHasCommentToken ($ token )
199- {
200- return $ token [Tokenizer::TOKEN_TYPE ] === Tokenizer::TOKEN_TYPE_COMMENT
201- || $ token [Tokenizer::TOKEN_TYPE ] === Tokenizer::TOKEN_TYPE_BLOCK_COMMENT ;
202- }
203-
204- /**
205- * @param $token
206- * @param string $tab
207- * @param $queryValue
208- *
209- * @return string
210- */
211- protected function writeCommentBlock ($ token , $ tab , $ queryValue )
212- {
213- if ($ token [Tokenizer::TOKEN_TYPE ] === Tokenizer::TOKEN_TYPE_BLOCK_COMMENT ) {
214- $ indent = str_repeat ($ tab , $ this ->indentation ->getIndentLvl ());
215-
216- $ this ->formattedSql .= "\n" . $ indent ;
217- $ queryValue = str_replace ("\n" , "\n" . $ indent , $ queryValue );
218- }
219-
220- $ this ->formattedSql .= $ queryValue ;
221- $ this ->newLine ->setNewline (true );
222-
223- return $ this ->formattedSql ;
224- }
225-
226202 /**
227203 * @param $token
228204 *
@@ -238,7 +214,7 @@ protected function isTokenTypeReservedTopLevel($token)
238214 */
239215 protected function tokenHasLimitClause ($ token )
240216 {
241- if ($ token [Tokenizer::TOKEN_VALUE ] === ' LIMIT ' && false === $ this ->parentheses ->getInlineParentheses ()) {
217+ if (' LIMIT ' === $ token [Tokenizer::TOKEN_VALUE ] && false === $ this ->parentheses ->getInlineParentheses ()) {
242218 $ this ->clauseLimit = true ;
243219 }
244220 }
@@ -250,11 +226,10 @@ protected function tokenHasLimitClause($token)
250226 */
251227 protected function stringIsEndOfLimitClause ($ token )
252228 {
253- return
254- $ this ->clauseLimit
255- && $ token [Tokenizer::TOKEN_VALUE ] !== ", "
256- && $ token [Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_NUMBER
257- && $ token [Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_WHITESPACE ;
229+ return $ this ->clauseLimit
230+ && $ token [Tokenizer::TOKEN_VALUE ] !== ", "
231+ && $ token [Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_NUMBER
232+ && $ token [Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_WHITESPACE ;
258233 }
259234
260235
@@ -268,12 +243,11 @@ protected function stringIsEndOfLimitClause($token)
268243 */
269244 protected function tokenHasMultipleBoundaryCharactersTogether ($ token , &$ tokens , $ i , &$ originalTokens )
270245 {
271- return
272- $ token [Tokenizer::TOKEN_TYPE ] === Tokenizer::TOKEN_TYPE_BOUNDARY
273- && isset ($ tokens [$ i - 1 ])
274- && $ tokens [$ i - 1 ][Tokenizer::TOKEN_TYPE ] === Tokenizer::TOKEN_TYPE_BOUNDARY
275- && isset ($ originalTokens [$ token ['i ' ] - 1 ])
276- && $ originalTokens [$ token ['i ' ] - 1 ][Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_WHITESPACE ;
246+ return $ token [Tokenizer::TOKEN_TYPE ] === Tokenizer::TOKEN_TYPE_BOUNDARY
247+ && isset ($ tokens [$ i - 1 ])
248+ && $ tokens [$ i - 1 ][Tokenizer::TOKEN_TYPE ] === Tokenizer::TOKEN_TYPE_BOUNDARY
249+ && isset ($ originalTokens [$ token ['i ' ] - 1 ])
250+ && $ originalTokens [$ token ['i ' ] - 1 ][Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_WHITESPACE ;
277251 }
278252
279253 /**
@@ -285,7 +259,7 @@ protected function tokenHasMultipleBoundaryCharactersTogether($token, &$tokens,
285259 */
286260 protected function tokenIsMinusSign ($ token , &$ tokens , $ i )
287261 {
288- return $ token [Tokenizer::TOKEN_VALUE ] === ' - '
262+ return ' - ' === $ token [Tokenizer::TOKEN_VALUE ]
289263 && isset ($ tokens [$ i + 1 ])
290264 && $ tokens [$ i + 1 ][Tokenizer::TOKEN_TYPE ] === Tokenizer::TOKEN_TYPE_NUMBER
291265 && isset ($ tokens [$ i - 1 ]);
0 commit comments