Skip to content

Commit e59b554

Browse files
committed
Comments to separate file
1 parent ebe84cf commit e59b554

File tree

2 files changed

+85
-50
lines changed

2 files changed

+85
-50
lines changed

src/Formatter.php

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace NilPortugues\SqlQueryFormatter;
1111

12+
use NilPortugues\SqlQueryFormatter\Helper\Comment;
1213
use NilPortugues\SqlQueryFormatter\Helper\Indent;
1314
use NilPortugues\SqlQueryFormatter\Helper\NewLine;
1415
use 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]);

src/Helper/Comment.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,71 @@
1010

1111
namespace NilPortugues\SqlQueryFormatter\Helper;
1212

13+
use NilPortugues\SqlQueryFormatter\Formatter;
14+
1315
/**
1416
* Class Comment
1517
* @package NilPortugues\SqlQueryFormatter\Helper
1618
*/
1719
class Comment
1820
{
21+
/**
22+
* @var \NilPortugues\SqlQueryFormatter\Formatter
23+
*/
24+
protected $formatter;
25+
26+
/**
27+
* @var Indent
28+
*/
29+
protected $indentation;
30+
31+
/**
32+
* @var NewLine
33+
*/
34+
protected $newLine;
35+
36+
/**
37+
* @param Formatter $formatter
38+
* @param Indent $indentation
39+
* @param NewLine $newLine
40+
*/
41+
public function __construct(Formatter $formatter, Indent $indentation, NewLine $newLine)
42+
{
43+
$this->formatter = $formatter;
44+
$this->indentation = $indentation;
45+
$this->newLine = $newLine;
46+
}
47+
48+
/**
49+
* @param $token
50+
*
51+
* @return bool
52+
*/
53+
public function stringHasCommentToken($token)
54+
{
55+
return $token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_COMMENT
56+
|| $token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_BLOCK_COMMENT;
57+
}
58+
59+
/**
60+
* @param $token
61+
* @param string $tab
62+
* @param $queryValue
63+
*
64+
* @return string
65+
*/
66+
public function writeCommentBlock($token, $tab, $queryValue)
67+
{
68+
if ($token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_BLOCK_COMMENT) {
69+
$indent = str_repeat($tab, $this->indentation->getIndentLvl());
70+
71+
$this->formatter->appendToFormattedSql("\n" . $indent);
72+
$queryValue = str_replace("\n", "\n" . $indent, $queryValue);
73+
}
74+
75+
$this->formatter->appendToFormattedSql($queryValue);
76+
$this->newLine->setNewline(true);
77+
78+
return $this->formatter->getFormattedSql();
79+
}
1980
}

0 commit comments

Comments
 (0)