Skip to content

Commit 55c2c6c

Browse files
committed
PHP7 fixes & added backslash to PHP internal functions
1 parent b2a726b commit 55c2c6c

20 files changed

+260
-281
lines changed

.travis.yml

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
language: php
2-
cache:
3-
directories:
4-
- vendor
52
php:
6-
- 5.6
7-
- 5.5
8-
- 5.4
9-
- nightly
3+
- "5.5"
4+
- "5.6"
5+
- "7.0"
6+
- "hhvm"
107

11-
matrix:
12-
allow_failures:
13-
- php: nightly
14-
158
before_script:
16-
- alias composer="php -d zend.enable_gc=0 /usr/bin/composer"
17-
- composer require --dev satooshi/php-coveralls:dev-master
189
- composer install
19-
20-
after_script:
21-
- export COVERALLS_RUN_LOCALLY=1
22-
- php bin/coveralls -v
2310

2411
script:
2512
- bin/phpunit --coverage-text
26-
- bin/phpunit-randomizer --order rand
2713

2814
matrix:
2915
allow_failures:
30-
- php: nightly
16+
- php: "hhvm"

composer.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@
2626
},
2727
"require":
2828
{
29-
"php": ">=5.4"
29+
"php": ">=5.5"
3030
},
31-
"require-dev":
32-
{
33-
"phpunit/phpunit": "~4.2",
34-
"fiunchinho/phpunit-randomizer": "1.0.*@dev",
35-
"fabpot/php-cs-fixer": "dev-master"
31+
"require-dev": {
32+
"phpunit/phpunit": "5.*",
33+
"fabpot/php-cs-fixer": "~1.9",
34+
"nilportugues/php_backslasher": "~0.2"
3635
},
3736
"config":
3837
{

src/Formatter.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
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+
1011
namespace NilPortugues\Sql\QueryFormatter;
1112

1213
use NilPortugues\Sql\QueryFormatter\Helper\Comment;
@@ -21,7 +22,6 @@
2122
* Lightweight Formatter heavily based on https://github.com/jdorn/sql-formatter.
2223
*
2324
* Class Formatter
24-
* @package NilPortugues\Sql\QueryFormatter
2525
*/
2626
class 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
}

src/Helper/Comment.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Author: Nil Portugués Calderó <[email protected]>
44
* Date: 12/22/14
5-
* Time: 10:09 PM
5+
* Time: 10:09 PM.
66
*
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
@@ -14,8 +14,7 @@
1414
use NilPortugues\Sql\QueryFormatter\Tokenizer\Tokenizer;
1515

1616
/**
17-
* Class Comment
18-
* @package NilPortugues\Sql\QueryFormatter\Helper
17+
* Class Comment.
1918
*/
2019
class Comment
2120
{
@@ -67,10 +66,10 @@ public function stringHasCommentToken($token)
6766
public function writeCommentBlock($token, $tab, $queryValue)
6867
{
6968
if ($token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_BLOCK_COMMENT) {
70-
$indent = str_repeat($tab, $this->indentation->getIndentLvl());
69+
$indent = \str_repeat($tab, $this->indentation->getIndentLvl());
7170

72-
$this->formatter->appendToFormattedSql("\n" . $indent);
73-
$queryValue = str_replace("\n", "\n" . $indent, $queryValue);
71+
$this->formatter->appendToFormattedSql("\n".$indent);
72+
$queryValue = \str_replace("\n", "\n".$indent, $queryValue);
7473
}
7574

7675
$this->formatter->appendToFormattedSql($queryValue);

0 commit comments

Comments
 (0)