1111
1212use NilPortugues \SqlQueryFormatter \Helper \Indent ;
1313use NilPortugues \SqlQueryFormatter \Helper \Tokenizer ;
14+ use NilPortugues \SqlQueryFormatter \Helper \WhiteSpace ;
1415
1516/**
1617 * Lightweight Formatter heavily based on https://github.com/jdorn/sql-formatter.
2021 */
2122class Formatter
2223{
24+ /**
25+ * @var Tokenizer
26+ */
27+ protected $ tokenizer ;
2328 /**
2429 * @var string
2530 */
2631 private $ tab = " " ;
27-
2832 /**
2933 * @var int
3034 */
3135 private $ inlineCount = 0 ;
32-
3336 /**
3437 * @var bool
3538 */
3639 private $ newline = false ;
37-
3840 /**
3941 * @var bool
4042 */
4143 private $ inlineParentheses = false ;
42-
4344 /**
4445 * @var bool
4546 */
4647 private $ clauseLimit = false ;
47-
4848 /**
4949 * @var string
5050 */
5151 private $ formattedSql = '' ;
52-
5352 /**
54- * @var Helper\ Indent
53+ * @var Indent
5554 */
5655 private $ indentation ;
5756
58- /**
59- *
60- */
61- public function __construct ()
62- {
63- $ this ->tokenizer = new Tokenizer ();
64- $ this ->indentation = new Indent ();
65- }
66-
6757 /**
6858 * Returns a SQL string in a readable human-friendly format.
6959 *
@@ -73,11 +63,11 @@ public function __construct()
7363 */
7464 public function format ($ sql )
7565 {
76- $ this ->formattedSql = '' ;
77- $ tab = "\t" ;
66+ $ this ->reset () ;
67+ $ tab = "\t" ;
7868
7969 $ originalTokens = $ this ->tokenizer ->tokenize ($ sql );
80- $ tokens = $ this -> removeTokenWhitespace ($ originalTokens );
70+ $ tokens = WhiteSpace:: removeTokenWhitespace ($ originalTokens );
8171
8272 foreach ($ tokens as $ i => $ token ) {
8373 $ queryValue = $ token [Tokenizer::TOKEN_VALUE ];
@@ -120,7 +110,7 @@ public function format($sql)
120110 }
121111 $ this ->writeNewLineForLongInlineValues ($ length );
122112
123- if ($ this -> isPrecedingCurrentTokenOfTokenTypeWhiteSpace ($ originalTokens , $ token )) {
113+ if (WhiteSpace:: isPrecedingCurrentTokenOfTokenTypeWhiteSpace ($ originalTokens , $ token )) {
124114 $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
125115 }
126116
@@ -133,7 +123,7 @@ public function format($sql)
133123 $ this ->indentation ->decreaseSpecialIndentIfCurrentIndentTypeIsSpecial ($ this );
134124 $ this ->writeNewLineBecauseOfTopLevelReservedWord ($ addedNewline , $ tab );
135125
136- if ($ this -> tokenHasExtraWhiteSpaces ($ token )) {
126+ if (WhiteSpace:: tokenHasExtraWhiteSpaces ($ token )) {
137127 $ queryValue = preg_replace ('/\s+/ ' , ' ' , $ queryValue );
138128 }
139129 $ this ->tokenHasLimitClause ($ token );
@@ -144,7 +134,7 @@ public function format($sql)
144134 } elseif ($ this ->isTokenTypeReservedNewLine ($ token )) {
145135 $ this ->writeNewLineBeforeReservedWord ($ addedNewline , $ tab );
146136
147- if ($ this -> tokenHasExtraWhiteSpaces ($ token )) {
137+ if (WhiteSpace:: tokenHasExtraWhiteSpaces ($ token )) {
148138 $ queryValue = preg_replace ('/\s+/ ' , ' ' , $ queryValue );
149139 }
150140 }
@@ -153,47 +143,36 @@ public function format($sql)
153143 $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
154144 }
155145
156- if ($ this -> tokenHasExtraWhiteSpaceLeft ($ token )) {
146+ if (WhiteSpace:: tokenHasExtraWhiteSpaceLeft ($ token )) {
157147 $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
158148 }
159149
160150 $ this ->formattedSql .= $ queryValue . ' ' ;
161151
162- if ($ this -> tokenHasExtraWhiteSpaceRight ($ token )) {
152+ if (WhiteSpace:: tokenHasExtraWhiteSpaceRight ($ token )) {
163153 $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
164154 }
165155
166156 if ($ this ->tokenIsMinusSign ($ token , $ tokens , $ i )) {
167157 $ previousTokenType = $ tokens [$ i - 1 ][Tokenizer::TOKEN_TYPE ];
168158
169- if ($ this -> tokenIsNumberAndHasExtraWhiteSpaceRight ($ previousTokenType )) {
159+ if (WhiteSpace:: tokenIsNumberAndHasExtraWhiteSpaceRight ($ previousTokenType )) {
170160 $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
171161 }
172162 }
173163 }
174164
175- return trim (str_replace (
176- array ("\t" , " \n" ),
177- array ($ this ->tab , "\n" ), $ this ->formattedSql )
178- ) . "\n" ;
165+ return trim (str_replace (["\t" , " \n" ], [$ this ->tab , "\n" ], $ this ->formattedSql )) . "\n" ;
179166 }
180167
181168 /**
182- * @param $originalTokens
183169 *
184- * @return array
185170 */
186- private function removeTokenWhitespace ( array & $ originalTokens )
171+ public function reset ( )
187172 {
188- $ tokens = array ();
189- foreach ($ originalTokens as $ i => &$ token ) {
190- if ($ token [Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_WHITESPACE ) {
191- $ token ['i ' ] = $ i ;
192- $ tokens [] = $ token ;
193- }
194- }
195-
196- return $ tokens ;
173+ $ this ->tokenizer = new Tokenizer ();
174+ $ this ->indentation = new Indent ();
175+ $ this ->formattedSql = '' ;
197176 }
198177
199178 /**
@@ -228,9 +207,9 @@ private function stringHasCommentToken($token)
228207 }
229208
230209 /**
231- * @param $token
210+ * @param $token
232211 * @param string $tab
233- * @param $queryValue
212+ * @param $queryValue
234213 *
235214 * @return string
236215 */
@@ -261,7 +240,7 @@ private function stringIsClosingParentheses($token)
261240
262241 /**
263242 * @param string $tab
264- * @param $queryValue
243+ * @param $queryValue
265244 */
266245 private function writeInlineParenthesesBlock ($ tab , $ queryValue )
267246 {
@@ -272,7 +251,7 @@ private function writeInlineParenthesesBlock($tab, $queryValue)
272251 array_shift ($ indentTypes );
273252 $ this ->indentation ->setIndentTypes ($ indentTypes );
274253
275- $ this ->indentation ->setIndentLvl ($ this ->indentation ->getIndentLvl ()- 1 );
254+ $ this ->indentation ->setIndentLvl ($ this ->indentation ->getIndentLvl () - 1 );
276255
277256 $ this ->formattedSql .= "\n" . str_repeat ($ tab , $ this ->indentation ->getIndentLvl ());
278257 }
@@ -348,21 +327,10 @@ private function writeNewLineForLongInlineValues($length)
348327 if ($ this ->inlineParentheses && $ length > 30 ) {
349328 $ this ->indentation ->setIncreaseBlockIndent (true );
350329 $ this ->indentation ->setInlineIndented (true );
351- $ this ->newline = true ;
330+ $ this ->newline = true ;
352331 }
353332 }
354333
355- /**
356- * @param $originalTokens
357- * @param $token
358- *
359- * @return bool
360- */
361- private function isPrecedingCurrentTokenOfTokenTypeWhiteSpace ($ originalTokens , $ token )
362- {
363- return isset ($ originalTokens [$ token ['i ' ] - 1 ])
364- && $ originalTokens [$ token ['i ' ] - 1 ][Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_WHITESPACE ;
365- }
366334
367335 /**
368336 * Adds a new line break for an opening parentheses for a non-inline expression.
@@ -371,13 +339,13 @@ private function addNewLineAfterOpeningParentheses()
371339 {
372340 if (!$ this ->inlineParentheses ) {
373341 $ this ->indentation ->setIncreaseBlockIndent (true );
374- $ this ->newline = true ;
342+ $ this ->newline = true ;
375343 }
376344 }
377345
378346 /**
379347 * @param boolean $addedNewline
380- * @param string $tab
348+ * @param string $tab
381349 */
382350 private function addNewLineBeforeClosingParentheses ($ addedNewline , $ tab )
383351 {
@@ -398,7 +366,7 @@ private function isTokenTypeReservedTopLevel($token)
398366
399367 /**
400368 * @param boolean $addedNewline
401- * @param string $tab
369+ * @param string $tab
402370 */
403371 private function writeNewLineBecauseOfTopLevelReservedWord ($ addedNewline , $ tab )
404372 {
@@ -410,18 +378,6 @@ private function writeNewLineBecauseOfTopLevelReservedWord($addedNewline, $tab)
410378 $ this ->newline = true ;
411379 }
412380
413- /**
414- * @param $token
415- *
416- * @return bool
417- */
418- private function tokenHasExtraWhiteSpaces ($ token )
419- {
420- return strpos ($ token [Tokenizer::TOKEN_VALUE ], ' ' ) !== false
421- || strpos ($ token [Tokenizer::TOKEN_VALUE ], "\n" ) !== false
422- || strpos ($ token [Tokenizer::TOKEN_VALUE ], "\t" ) !== false ;
423- }
424-
425381 /**
426382 * @param $token
427383 */
@@ -472,7 +428,7 @@ private function isTokenTypeReservedNewLine($token)
472428
473429 /**
474430 * @param boolean $addedNewline
475- * @param string $tab
431+ * @param string $tab
476432 */
477433 private function writeNewLineBeforeReservedWord ($ addedNewline , $ tab )
478434 {
@@ -499,31 +455,6 @@ private function tokenHasMultipleBoundaryCharactersTogether($token, &$tokens, $i
499455 && $ originalTokens [$ token ['i ' ] - 1 ][Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_WHITESPACE ;
500456 }
501457
502- /**
503- * @param $token
504- *
505- * @return bool
506- */
507- private function tokenHasExtraWhiteSpaceLeft ($ token )
508- {
509- return
510- $ token [Tokenizer::TOKEN_VALUE ] === '. '
511- || $ token [Tokenizer::TOKEN_VALUE ] === ', '
512- || $ token [Tokenizer::TOKEN_VALUE ] === '; ' ;
513- }
514-
515- /**
516- * @param $token
517- *
518- * @return bool
519- */
520- private function tokenHasExtraWhiteSpaceRight ($ token )
521- {
522- return
523- $ token [Tokenizer::TOKEN_VALUE ] === '( '
524- || $ token [Tokenizer::TOKEN_VALUE ] === '. ' ;
525- }
526-
527458 /**
528459 * @param $token
529460 * @param $tokens
@@ -540,17 +471,11 @@ protected function tokenIsMinusSign($token, &$tokens, $i)
540471 }
541472
542473 /**
543- * @param $tokenType
544- *
545- * @return bool
474+ * @return string
546475 */
547- private function tokenIsNumberAndHasExtraWhiteSpaceRight ( $ tokenType )
476+ public function getFormattedSql ( )
548477 {
549- return
550- $ tokenType !== Tokenizer::TOKEN_TYPE_QUOTE
551- && $ tokenType !== Tokenizer::TOKEN_TYPE_BACK_TICK_QUOTE
552- && $ tokenType !== Tokenizer::TOKEN_TYPE_WORD
553- && $ tokenType !== Tokenizer::TOKEN_TYPE_NUMBER ;
478+ return $ this ->formattedSql ;
554479 }
555480
556481 /**
@@ -563,12 +488,4 @@ public function setFormattedSql($formattedSql)
563488 $ this ->formattedSql = $ formattedSql ;
564489 return $ this ;
565490 }
566-
567- /**
568- * @return string
569- */
570- public function getFormattedSql ()
571- {
572- return $ this ->formattedSql ;
573- }
574491}
0 commit comments