Skip to content

Commit

Permalink
Support nested quotes (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-viguier committed May 16, 2024
1 parent d20f0bc commit 7079671
Show file tree
Hide file tree
Showing 8 changed files with 6,021 additions and 7,373 deletions.
897 changes: 693 additions & 204 deletions .phpstan-dba-mysqli.cache

Large diffs are not rendered by default.

1,006 changes: 617 additions & 389 deletions .phpstan-dba-pdo-mysql.cache

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/QueryReflection/QueryReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ final class QueryReflection
// see https://github.com/php/php-src/blob/01b3fc03c30c6cb85038250bb5640be3a09c6a32/ext/pdo/pdo_sql_parser.re#L48
private const NAMED_PATTERN = ':[a-zA-Z0-9_]+';

private const REGEX_UNNAMED_PLACEHOLDER = '{(["\'])([^"\']*\1)|(' . self::UNNAMED_PATTERN . ')}';
private const REGEX_UNNAMED_PLACEHOLDER = '{(["\'])((?:(?!\1).)*\1)|(' . self::UNNAMED_PATTERN . ')}';

private const REGEX_NAMED_PLACEHOLDER = '{(["\'])([^"\']*\1)|(' . self::NAMED_PATTERN . ')}';
private const REGEX_NAMED_PLACEHOLDER = '{(["\'])((?:(?!\1).)*\1)|(' . self::NAMED_PATTERN . ')}';

/**
* @var QueryReflector|null
Expand Down
4 changes: 4 additions & 0 deletions tests/rules/SyntaxErrorInQueryFunctionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public function testMysqliExecuteQuery(): void
self::markTestSkipped('Test requires PHP 8.2.');
}

if (MysqliQueryReflector::NAME !== getenv('DBA_REFLECTOR')) {
self::markTestSkipped('mysqli test only.');
}

$this->analyse([__DIR__ . '/data/mysqli_execute_query.php'], [
[
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'freigabe1u1 FROM ada LIMIT 0' at line 1 (1064).",
Expand Down
4 changes: 4 additions & 0 deletions tests/rules/SyntaxErrorInQueryMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ public function testMysqliExecuteQuery(): void
self::markTestSkipped('Test requires PHP 8.2.');
}

if (MysqliQueryReflector::NAME !== getenv('DBA_REFLECTOR')) {
self::markTestSkipped('mysqli test only.');
}

$this->analyse([__DIR__ . '/data/mysqli_execute_query.php'], [
[
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'freigabe1u1 FROM ada LIMIT 0' at line 1 (1064).",
Expand Down
5,743 changes: 2,346 additions & 3,397 deletions tests/rules/config/.phpunit-phpstan-dba-mysqli.cache

Large diffs are not rendered by default.

5,727 changes: 2,346 additions & 3,381 deletions tests/rules/config/.phpunit-phpstan-dba-pdo-mysql.cache

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions tests/rules/data/pdo-stmt-execute-error.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,13 @@ public function bug422UnnamedPlaceholdersInCommentInsideOfQuery(PDO $pdo)
$stmt = $pdo->prepare('SELECT email, adaid /* why? ? */ FROM ada /* just ?? :because ?*/ WHERE email = :email -- ?');
$stmt->execute(['email' => 'a']);
}

public function supportNestedQuotes(PDO $pdo)
{
$stmt = $pdo->prepare(<<<SQL
SELECT payload ->> '$."dash-separated"' = :value FROM ada WHERE 'foo'
SQL
);
$stmt->execute(['value' => 'bar']);
}
}

0 comments on commit 7079671

Please sign in to comment.