From ccd07454cbd9f89a87e5704790468c23f6a318a1 Mon Sep 17 00:00:00 2001 From: Nabab Date: Fri, 22 Jun 2018 13:10:55 +0200 Subject: [PATCH 1/2] Prevent Undefined offset error For some reason the argument `$tokens` comes sometime as a numeric array starting with an index greater than 0. In this case the loop with `$tokenNumber` starting at 0 will not be able to read the token and will throw an error `Undefined offset` --- src/PHPSQLParser/processors/SQLProcessor.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/PHPSQLParser/processors/SQLProcessor.php b/src/PHPSQLParser/processors/SQLProcessor.php index 1fc80eba..805fa195 100644 --- a/src/PHPSQLParser/processors/SQLProcessor.php +++ b/src/PHPSQLParser/processors/SQLProcessor.php @@ -61,7 +61,10 @@ public function process($tokens) { $skip_next = 0; $out = false; - $tokenCount = count($tokens); + // $tokens may come as a numeric indexed array starting with an index greater than 0 (or as a boolean) + if ( $tokenCount = count($tokens) ){ + $tokens = array_values($tokens); + } for ($tokenNumber = 0; $tokenNumber < $tokenCount; ++$tokenNumber) { $token = $tokens[$tokenNumber]; From 6519c0326b4890b070a204d7f2a7c34bdfaac428 Mon Sep 17 00:00:00 2001 From: Nabab Date: Fri, 22 Jun 2018 18:05:34 +0200 Subject: [PATCH 2/2] There was still an error, my bad I hadn't realized count(false) returned 1 --- src/PHPSQLParser/processors/SQLProcessor.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PHPSQLParser/processors/SQLProcessor.php b/src/PHPSQLParser/processors/SQLProcessor.php index 805fa195..4f6e8fa9 100644 --- a/src/PHPSQLParser/processors/SQLProcessor.php +++ b/src/PHPSQLParser/processors/SQLProcessor.php @@ -62,7 +62,8 @@ public function process($tokens) { $out = false; // $tokens may come as a numeric indexed array starting with an index greater than 0 (or as a boolean) - if ( $tokenCount = count($tokens) ){ + $tokenCount = count($tokens); + if ( is_array($tokens) ){ $tokens = array_values($tokens); } for ($tokenNumber = 0; $tokenNumber < $tokenCount; ++$tokenNumber) {