Skip to content

Commit db07acb

Browse files
committed
Separation of concerns for Tokenizer
1 parent e59b554 commit db07acb

File tree

17 files changed

+837
-626
lines changed

17 files changed

+837
-626
lines changed

src/Formatter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use NilPortugues\SqlQueryFormatter\Helper\Indent;
1414
use NilPortugues\SqlQueryFormatter\Helper\NewLine;
1515
use NilPortugues\SqlQueryFormatter\Helper\Parentheses;
16-
use NilPortugues\SqlQueryFormatter\Helper\Tokenizer;
1716
use NilPortugues\SqlQueryFormatter\Helper\WhiteSpace;
17+
use NilPortugues\SqlQueryFormatter\Tokenizer\Tokenizer;
1818

1919
/**
2020
* Lightweight Formatter heavily based on https://github.com/jdorn/sql-formatter.
@@ -84,8 +84,9 @@ public function format($sql)
8484
foreach ($tokens as $i => $token) {
8585
$queryValue = $token[Tokenizer::TOKEN_VALUE];
8686

87-
$this->indentation->increaseSpecialIndent($this);
88-
$this->indentation->increaseBlockIndent($this);
87+
$this->indentation
88+
->increaseSpecialIndent()
89+
->increaseBlockIndent();
8990

9091
$addedNewline = $this->newLine->addNewLineBreak($tab);
9192

src/Helper/Comment.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace NilPortugues\SqlQueryFormatter\Helper;
1212

1313
use NilPortugues\SqlQueryFormatter\Formatter;
14+
use NilPortugues\SqlQueryFormatter\Tokenizer\Tokenizer;
1415

1516
/**
1617
* Class Comment

src/Helper/Indent.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Indent
4646

4747
/**
4848
* Increase the Special Indent if increaseSpecialIndent is true after the current iteration.
49+
*
50+
* @return $this
4951
*/
5052
public function increaseSpecialIndent()
5153
{
@@ -54,10 +56,13 @@ public function increaseSpecialIndent()
5456
$this->increaseSpecialIndent = false;
5557
array_unshift($this->indentTypes, 'special');
5658
}
59+
return $this;
5760
}
5861

5962
/**
6063
* Increase the Block Indent if increaseBlockIndent is true after the current iteration.
64+
*
65+
* @return $this
6166
*/
6267
public function increaseBlockIndent()
6368
{
@@ -66,26 +71,32 @@ public function increaseBlockIndent()
6671
$this->increaseBlockIndent = false;
6772
array_unshift($this->indentTypes, 'block');
6873
}
74+
return $this;
6975
}
7076

7177
/**
7278
* Closing parentheses decrease the block indent level.
79+
*
80+
* @param Formatter $formatter
81+
*
82+
* @return $this
7383
*/
7484
public function decreaseIndentLevelUntilIndentTypeIsSpecial(Formatter $formatter)
7585
{
7686
$formatter->setFormattedSql(rtrim($formatter->getFormattedSql(), ' '));
7787
$this->indentLvl--;
7888

7989
while ($j = array_shift($this->indentTypes)) {
80-
if ($j === 'special') {
81-
$this->indentLvl--;
82-
} else {
90+
if ('special' !== $j) {
8391
break;
8492
}
93+
$this->indentLvl--;
8594
}
95+
return $this;
8696
}
8797

8898
/**
99+
* @return $this
89100
*/
90101
public function decreaseSpecialIndentIfCurrentIndentTypeIsSpecial()
91102
{
@@ -95,6 +106,7 @@ public function decreaseSpecialIndentIfCurrentIndentTypeIsSpecial()
95106
$this->indentLvl--;
96107
array_shift($this->indentTypes);
97108
}
109+
return $this;
98110
}
99111

100112
/**

src/Helper/NewLine.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace NilPortugues\SqlQueryFormatter\Helper;
1212

1313
use NilPortugues\SqlQueryFormatter\Formatter;
14+
use NilPortugues\SqlQueryFormatter\Tokenizer\Tokenizer;
1415

1516
/**
1617
* Class NewLine
@@ -148,8 +149,8 @@ public function writeNewLineBecauseOfComma()
148149
$this->newline = true;
149150

150151
if (true === $this->formatter->getClauseLimit()) {
151-
$this->newline = false;
152-
$this->clauseLimit = false;
152+
$this->newline = false;
153+
$this->formatter->setClauseLimit(false);
153154
}
154155
}
155156

@@ -174,6 +175,14 @@ public function writeNewLineBeforeReservedWord($addedNewline, $tab)
174175
}
175176
}
176177

178+
/**
179+
* @return boolean
180+
*/
181+
public function getNewline()
182+
{
183+
return $this->newline;
184+
}
185+
177186
/**
178187
* @param boolean $newline
179188
*
@@ -184,12 +193,4 @@ public function setNewline($newline)
184193
$this->newline = $newline;
185194
return $this;
186195
}
187-
188-
/**
189-
* @return boolean
190-
*/
191-
public function getNewline()
192-
{
193-
return $this->newline;
194-
}
195196
}

src/Helper/Parentheses.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace NilPortugues\SqlQueryFormatter\Helper;
1212

1313
use NilPortugues\SqlQueryFormatter\Formatter;
14+
use NilPortugues\SqlQueryFormatter\Tokenizer\Tokenizer;
1415

1516
/**
1617
* Class Parentheses

0 commit comments

Comments
 (0)