From 4a449d71b00d59e9ed5cfecf04db52b1a87f6011 Mon Sep 17 00:00:00 2001 From: Nicolas Dermine Date: Wed, 17 Jun 2020 16:34:44 +0200 Subject: [PATCH 1/2] fix "strpos empty needle" warning fixes issue #320 a warning was issued in `PositionCalculator#findPositionWithinString` when calling `strpos` on an empty value: ``` Warning: strpos(): Empty needle in src/PHPSQLParser/positions/PositionCalculator.php on line 138 ``` => return `false` earlier in that case, to then throw an exception --- .../positions/PositionCalculator.php | 3 + tests/cases/parser/issue320Test.php | 60 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/cases/parser/issue320Test.php diff --git a/src/PHPSQLParser/positions/PositionCalculator.php b/src/PHPSQLParser/positions/PositionCalculator.php index 53eb57f6..0808b96a 100644 --- a/src/PHPSQLParser/positions/PositionCalculator.php +++ b/src/PHPSQLParser/positions/PositionCalculator.php @@ -127,6 +127,9 @@ public function setPositionsWithinSQL($sql, $parsed) { } protected function findPositionWithinString($sql, $value, $expr_type) { + if ($value === '') { + return false; + } $offset = 0; $ok = false; diff --git a/tests/cases/parser/issue320Test.php b/tests/cases/parser/issue320Test.php new file mode 100644 index 00000000..29bcefe5 --- /dev/null +++ b/tests/cases/parser/issue320Test.php @@ -0,0 +1,60 @@ + + * @copyright 2010-2014 Justin Swanhart and André Rothe + * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) + * @version SVN: $Id$ + */ +namespace PHPSQLParser\Test\Parser; +use PHPSQLParser\PHPSQLParser; +use PHPSQLParser\PHPSQLCreator; + +class issue320Test extends \PHPUnit_Framework_TestCase +{ + public function test_no_warning_is_issued_when_help_table_is_used() + { + $parser = new PHPSQLParser(); + $sql = 'SELECT * FROM help'; + + // there currently is an exception because `HELP` is a keyword + // but this query seems valid at least in mysql and mssql + // so ideally PHPSQLParser would be able to parse it + $this->setExpectedException( + \PHPSQLParser\exceptions\UnableToCalculatePositionException::class + ); + + $parser->parse($sql, true); + } +} From 85904d136f05763aaa3a5f15c78c9d29b6795d31 Mon Sep 17 00:00:00 2001 From: Nicolas Dermine Date: Sun, 4 Oct 2020 13:48:42 +0200 Subject: [PATCH 2/2] adapt test to make it compatible with PHP 5.4 PHP Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /home/travis/build/greenlion/PHP-SQL-Parser/tests/cases/parser/issue320Test.php on line 55 --- tests/cases/parser/issue320Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/parser/issue320Test.php b/tests/cases/parser/issue320Test.php index 29bcefe5..1f3b274e 100644 --- a/tests/cases/parser/issue320Test.php +++ b/tests/cases/parser/issue320Test.php @@ -52,7 +52,7 @@ public function test_no_warning_is_issued_when_help_table_is_used() // but this query seems valid at least in mysql and mssql // so ideally PHPSQLParser would be able to parse it $this->setExpectedException( - \PHPSQLParser\exceptions\UnableToCalculatePositionException::class + '\PHPSQLParser\exceptions\UnableToCalculatePositionException' ); $parser->parse($sql, true);