22/**
33 * Author: Nil Portugués Calderó <[email protected] > 44 * Date: 6/26/14
5- * Time: 12:10 AM
5+ * Time: 12:10 AM.
66 *
77 * For the full copyright and license information, please view the LICENSE
88 * file that was distributed with this source code.
99 */
10+
1011namespace NilPortugues \Sql \QueryFormatter ;
1112
1213use NilPortugues \Sql \QueryFormatter \Helper \Comment ;
2122 * Lightweight Formatter heavily based on https://github.com/jdorn/sql-formatter.
2223 *
2324 * Class Formatter
24- * @package NilPortugues\Sql\QueryFormatter
2525 */
2626class Formatter
2727{
@@ -43,7 +43,7 @@ class Formatter
4343 /**
4444 * @var string
4545 */
46- protected $ tab = " " ;
46+ protected $ tab = ' ' ;
4747 /**
4848 * @var int
4949 */
@@ -79,8 +79,8 @@ public function format($sql)
7979 $ this ->reset ();
8080 $ tab = "\t" ;
8181
82- $ originalTokens = $ this ->tokenizer ->tokenize ((string )$ sql );
83- $ tokens = WhiteSpace::removeTokenWhitespace ($ originalTokens );
82+ $ originalTokens = $ this ->tokenizer ->tokenize ((string ) $ sql );
83+ $ tokens = WhiteSpace::removeTokenWhitespace ($ originalTokens );
8484
8585 foreach ($ tokens as $ i => $ token ) {
8686 $ queryValue = $ token [Tokenizer::TOKEN_VALUE ];
@@ -98,7 +98,7 @@ public function format($sql)
9898 continue ;
9999 }
100100 $ this ->newLine ->writeNewLineForLongCommaInlineValues ($ token );
101- $ this ->inlineCount += strlen ($ token [Tokenizer::TOKEN_VALUE ]);
101+ $ this ->inlineCount += \ strlen ($ token [Tokenizer::TOKEN_VALUE ]);
102102 }
103103
104104 switch ($ token ) {
@@ -127,7 +127,7 @@ public function format($sql)
127127 $ this ->newLine ->addNewLineBeforeToken ($ addedNewline , $ tab );
128128
129129 if (WhiteSpace::tokenHasExtraWhiteSpaces ($ token )) {
130- $ queryValue = preg_replace ('/\s+/ ' , ' ' , $ queryValue );
130+ $ queryValue = \ preg_replace ('/\s+/ ' , ' ' , $ queryValue );
131131 }
132132 break ;
133133 }
@@ -137,19 +137,19 @@ public function format($sql)
137137 $ this ->formatDashToken ($ token , $ i , $ tokens );
138138 }
139139
140- return trim (str_replace (["\t" , " \n" ], [$ this ->tab , "\n" ], $ this ->formattedSql )) . "\n" ;
140+ return \ trim (\ str_replace (["\t" , " \n" ], [$ this ->tab , "\n" ], $ this ->formattedSql )). "\n" ;
141141 }
142142
143143 /**
144144 *
145145 */
146146 public function reset ()
147147 {
148- $ this ->tokenizer = new Tokenizer ();
148+ $ this ->tokenizer = new Tokenizer ();
149149 $ this ->indentation = new Indent ();
150150 $ this ->parentheses = new Parentheses ($ this , $ this ->indentation );
151- $ this ->newLine = new NewLine ($ this , $ this ->indentation , $ this ->parentheses );
152- $ this ->comment = new Comment ($ this , $ this ->indentation , $ this ->newLine );
151+ $ this ->newLine = new NewLine ($ this , $ this ->indentation , $ this ->parentheses );
152+ $ this ->comment = new Comment ($ this , $ this ->indentation , $ this ->newLine );
153153
154154 $ this ->formattedSql = '' ;
155155 }
@@ -165,7 +165,7 @@ public function reset()
165165 protected function formatOpeningParenthesis ($ token , $ i , array &$ tokens , array &$ originalTokens )
166166 {
167167 $ length = 0 ;
168- for ($ j = 1 ; $ j <= 250 ; $ j ++ ) {
168+ for ($ j = 1 ; $ j <= 250 ; ++ $ j ) {
169169 if (isset ($ tokens [$ i + $ j ])) {
170170 $ next = $ tokens [$ i + $ j ];
171171 if ($ this ->parentheses ->stringIsClosingParentheses ($ next )) {
@@ -179,13 +179,13 @@ protected function formatOpeningParenthesis($token, $i, array &$tokens, array &$
179179 break ;
180180 }
181181
182- $ length += strlen ($ next [Tokenizer::TOKEN_VALUE ]);
182+ $ length += \ strlen ($ next [Tokenizer::TOKEN_VALUE ]);
183183 }
184184 }
185185 $ this ->newLine ->writeNewLineForLongInlineValues ($ length );
186186
187187 if (WhiteSpace::isPrecedingCurrentTokenOfTokenTypeWhiteSpace ($ originalTokens , $ token )) {
188- $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
188+ $ this ->formattedSql = \ rtrim ($ this ->formattedSql , ' ' );
189189 }
190190
191191 $ this ->newLine ->addNewLineAfterOpeningParentheses ();
@@ -201,13 +201,13 @@ protected function formatOpeningParenthesis($token, $i, array &$tokens, array &$
201201 protected function stringIsEndOfLimitClause ($ token )
202202 {
203203 return $ this ->clauseLimit
204- && $ token [Tokenizer::TOKEN_VALUE ] !== " , "
204+ && $ token [Tokenizer::TOKEN_VALUE ] !== ' , '
205205 && $ token [Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_NUMBER
206206 && $ token [Tokenizer::TOKEN_TYPE ] !== Tokenizer::TOKEN_TYPE_WHITESPACE ;
207207 }
208208
209209 /**
210- * @param boolean $addedNewline
210+ * @param bool $addedNewline
211211 * @param string $tab
212212 * @param $token
213213 * @param $queryValue
@@ -223,9 +223,10 @@ protected function formatTokenTypeReservedTopLevel($addedNewline, $tab, $token,
223223 $ this ->newLine ->writeNewLineBecauseOfTopLevelReservedWord ($ addedNewline , $ tab );
224224
225225 if (WhiteSpace::tokenHasExtraWhiteSpaces ($ token )) {
226- $ queryValue = preg_replace ('/\s+/ ' , ' ' , $ queryValue );
226+ $ queryValue = \ preg_replace ('/\s+/ ' , ' ' , $ queryValue );
227227 }
228228 Token::tokenHasLimitClause ($ token , $ this ->parentheses , $ this );
229+
229230 return $ queryValue ;
230231 }
231232
@@ -238,7 +239,7 @@ protected function formatTokenTypeReservedTopLevel($addedNewline, $tab, $token,
238239 protected function formatBoundaryCharacterToken ($ token , $ i , array &$ tokens , array &$ originalTokens )
239240 {
240241 if (Token::tokenHasMultipleBoundaryCharactersTogether ($ token , $ tokens , $ i , $ originalTokens )) {
241- $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
242+ $ this ->formattedSql = \ rtrim ($ this ->formattedSql , ' ' );
242243 }
243244 }
244245
@@ -249,13 +250,13 @@ protected function formatBoundaryCharacterToken($token, $i, array &$tokens, arra
249250 protected function formatWhiteSpaceToken ($ token , $ queryValue )
250251 {
251252 if (WhiteSpace::tokenHasExtraWhiteSpaceLeft ($ token )) {
252- $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
253+ $ this ->formattedSql = \ rtrim ($ this ->formattedSql , ' ' );
253254 }
254255
255- $ this ->formattedSql .= $ queryValue . ' ' ;
256+ $ this ->formattedSql .= $ queryValue. ' ' ;
256257
257258 if (WhiteSpace::tokenHasExtraWhiteSpaceRight ($ token )) {
258- $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
259+ $ this ->formattedSql = \ rtrim ($ this ->formattedSql , ' ' );
259260 }
260261 }
261262
@@ -270,7 +271,7 @@ protected function formatDashToken($token, $i, array &$tokens)
270271 $ previousTokenType = $ tokens [$ i - 1 ][Tokenizer::TOKEN_TYPE ];
271272
272273 if (WhiteSpace::tokenIsNumberAndHasExtraWhiteSpaceRight ($ previousTokenType )) {
273- $ this ->formattedSql = rtrim ($ this ->formattedSql , ' ' );
274+ $ this ->formattedSql = \ rtrim ($ this ->formattedSql , ' ' );
274275 }
275276 }
276277 }
@@ -291,6 +292,7 @@ public function getFormattedSql()
291292 public function setFormattedSql ($ formattedSql )
292293 {
293294 $ this ->formattedSql = $ formattedSql ;
295+
294296 return $ this ;
295297 }
296298
@@ -302,6 +304,7 @@ public function setFormattedSql($formattedSql)
302304 public function appendToFormattedSql ($ string )
303305 {
304306 $ this ->formattedSql .= $ string ;
307+
305308 return $ this ;
306309 }
307310
@@ -321,25 +324,27 @@ public function getInlineCount()
321324 public function setInlineCount ($ inlineCount )
322325 {
323326 $ this ->inlineCount = $ inlineCount ;
327+
324328 return $ this ;
325329 }
326330
327331 /**
328- * @return boolean
332+ * @return bool
329333 */
330334 public function getClauseLimit ()
331335 {
332336 return $ this ->clauseLimit ;
333337 }
334338
335339 /**
336- * @param boolean $clauseLimit
340+ * @param bool $clauseLimit
337341 *
338342 * @return $this
339343 */
340344 public function setClauseLimit ($ clauseLimit )
341345 {
342346 $ this ->clauseLimit = $ clauseLimit ;
347+
343348 return $ this ;
344349 }
345350}
0 commit comments