Skip to content

Commit e704f6b

Browse files
committed
Merge branch 'master' into motranslator
2 parents eb2b165 + 082a1fa commit e704f6b

5 files changed

Lines changed: 69 additions & 39 deletions

File tree

src/Statement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,11 @@ public function validateClauseOrder($parser, $list)
479479

480480
// Handle ordering of Multiple Joins in a query
481481
if ($clauseStartIdx != -1) {
482-
if ($joinStart == 0 && stripos($clauseType, 'JOIN')) {
482+
if ($joinStart == 0 && stripos($clauseType, 'JOIN') !== false) {
483483
$joinStart = 1;
484-
} elseif ($joinStart == 1 && ! stripos($clauseType, 'JOIN')) {
484+
} elseif ($joinStart == 1 && stripos($clauseType, 'JOIN') === false) {
485485
$joinStart = 2;
486-
} elseif ($joinStart == 2 && stripos($clauseType, 'JOIN')) {
486+
} elseif ($joinStart == 2 && stripos($clauseType, 'JOIN') !== false) {
487487
$error = 1;
488488
}
489489
}

src/UtfString.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ public function __construct($str)
7676
$this->str = $str;
7777
$this->byteIdx = 0;
7878
$this->charIdx = 0;
79-
// TODO: `strlen($str)` might return a wrong length when function
80-
// overloading is enabled.
81-
// https://php.net/manual/ro/mbstring.overload.php
82-
$this->byteLen = strlen($str);
79+
$this->byteLen = mb_strlen($str, '8bit');
8380
$this->charLen = mb_strlen($str, 'UTF-8');
8481
}
8582

src/Utils/Formatter.php

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ private function getMergedOptions(array $options)
7171
$options
7272
);
7373

74-
$options['formats'] = self::mergeFormats($this->getDefaultFormats(), @$options['formats'] ?: array());
74+
if (isset($options['formats'])) {
75+
$options['formats'] = self::mergeFormats($this->getDefaultFormats(), $options['formats']);
76+
} else {
77+
$options['formats'] = $this->getDefaultFormats();
78+
}
7579

7680
if (is_null($options['line_ending'])) {
7781
$options['line_ending'] = $options['type'] === 'html' ? '<br/>' : "\n";
@@ -208,44 +212,39 @@ protected function getDefaultFormats()
208212
private static function mergeFormats(array $formats, array $newFormats)
209213
{
210214
$added = array();
215+
$integers = array('flags', 'type');
216+
$strings = array('html', 'cli', 'function');
211217

218+
/* Sanitize the array so that we do not have to care later */
219+
foreach ($newFormats as $j => $new) {
220+
foreach ($integers as $name) {
221+
if (! isset($new[$name])) {
222+
$newFormats[$j][$name] = 0;
223+
}
224+
}
225+
foreach ($strings as $name) {
226+
if (! isset($new[$name])) {
227+
$newFormats[$j][$name] = '';
228+
}
229+
}
230+
}
231+
232+
/* Process changes to existing formats */
212233
foreach ($formats as $i => $original) {
213234
foreach ($newFormats as $j => $new) {
214-
if (isset($new['type'])
215-
&& $new['type'] === $original['type']
216-
&& (
217-
(
218-
isset($new['flags'])
219-
&& $original['flags'] === $new['flags']
220-
)
221-
|| (
222-
!isset($new['flags'])
223-
&& $original['flags'] == 0
224-
)
225-
)
235+
if ($new['type'] === $original['type']
236+
&& $original['flags'] === $new['flags']
226237
) {
227-
$formats[$i] = array(
228-
'type' => $original['type'],
229-
'flags' => isset($new['flags']) ? $new['flags'] : 0,
230-
'html' => isset($new['html']) ? $new['html'] : '',
231-
'cli' => isset($new['cli']) ? $new['cli'] : '',
232-
'function' => isset($new['function']) ? $new['function'] : '',
233-
);
234-
238+
$formats[$i] = $new;
235239
$added[] = $j;
236240
}
237241
}
238242
}
239243

244+
/* Add not already handled formats */
240245
foreach ($newFormats as $j => $new) {
241-
if (!in_array($j, $added) && isset($new['type'])) {
242-
$formats[] = array(
243-
'type' => $new['type'],
244-
'flags' => isset($new['flags']) ? $new['flags'] : 0,
245-
'html' => isset($new['html']) ? $new['html'] : '',
246-
'cli' => isset($new['cli']) ? $new['cli'] : '',
247-
'function' => isset($new['function']) ? $new['function'] : '',
248-
);
246+
if (! in_array($j, $added)) {
247+
$formats[] = $new;
249248
}
250249
}
251250

tests/Components/Array2dTest.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,52 @@ public function testParseErr3()
4646
{
4747
$parser = new Parser();
4848
Array2d::parse($parser, $this->getTokensList(')'));
49-
Array2d::parse($parser, $this->getTokensList('TABLE'));
50-
// TODO: Assert errors.
49+
$this->assertEquals(
50+
1,
51+
count($parser->errors)
52+
);
53+
$this->assertEquals(
54+
'An opening bracket followed by a set of values was expected.',
55+
$parser->errors[0]->getMessage()
56+
);
5157
}
5258

5359
public function testParseErr4()
60+
{
61+
$parser = new Parser();
62+
Array2d::parse($parser, $this->getTokensList('TABLE'));
63+
$this->assertEquals(
64+
1,
65+
count($parser->errors)
66+
);
67+
$this->assertEquals(
68+
'An opening bracket followed by a set of values was expected.',
69+
$parser->errors[0]->getMessage()
70+
);
71+
}
72+
73+
public function testParseErr5()
5474
{
5575
$parser = new Parser();
5676
Array2d::parse($parser, $this->getTokensList('(1, 2),'));
77+
$this->assertEquals(
78+
1,
79+
count($parser->errors)
80+
);
5781
$this->assertEquals(
5882
"An opening bracket followed by a set of values was expected.",
5983
$parser->errors[0]->getMessage()
6084
);
6185
}
6286

63-
public function testParseErr5()
87+
public function testParseErr6()
6488
{
6589
$parser = new Parser();
6690
Array2d::parse($parser, $this->getTokensList('(1, 2),(3)'));
91+
$this->assertEquals(
92+
1,
93+
count($parser->errors)
94+
);
6795
$this->assertEquals(
6896
"2 values were expected, but found 1.",
6997
$parser->errors[0]->getMessage()

tests/Utils/FormatterTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public function mergeFormats()
5959
array(
6060
'type' => 0,
6161
'flags' => 0,
62+
'html' => '',
63+
'cli' => '',
64+
'function' => '',
6265
),
6366
),
6467
array( // overriding
@@ -69,6 +72,9 @@ public function mergeFormats()
6972
array(
7073
'type' => 0,
7174
'flags' => 0,
75+
'html' => '',
76+
'cli' => '',
77+
'function' => '',
7278
),
7379
),
7480
),

0 commit comments

Comments
 (0)