Skip to content

Commit f559241

Browse files
authored
Support # comments (#696)
1 parent 140f523 commit f559241

18 files changed

+52230
-44735
lines changed

.phpstan-dba-mysqli.cache

+829-139
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.phpstan-dba-pdo-mysql.cache

+829-139
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/QueryReflection/QueryReflection.php

+9
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public static function setupReflector(QueryReflector $reflector, RuntimeConfigur
7777

7878
public function validateQueryString(string $queryString): ?Error
7979
{
80+
$queryString = QuerySimulation::stripComments($queryString);
8081
$queryType = self::getQueryType($queryString);
8182

8283
if (self::getRuntimeConfiguration()->isAnalyzingWriteQueries()) {
@@ -110,6 +111,8 @@ public function validateQueryString(string $queryString): ?Error
110111
*/
111112
public function getResultType(string $queryString, int $fetchType): ?Type
112113
{
114+
$queryString = QuerySimulation::stripComments($queryString);
115+
113116
if ('SELECT' !== self::getQueryType($queryString)) {
114117
return null;
115118
}
@@ -397,6 +400,7 @@ private function resolveQueryStringExpr(Expr $queryExpr, Scope $scope, bool $res
397400

398401
public static function getQueryType(string $query): ?string
399402
{
403+
$query = QuerySimulation::stripComments($query);
400404
$query = ltrim($query);
401405

402406
if (1 === preg_match('/^\s*\(?\s*(SELECT|SHOW|UPDATE|INSERT|DELETE|REPLACE|CREATE|CALL|OPTIMIZE)/i', $query, $matches)) {
@@ -546,6 +550,8 @@ public static function getRuntimeConfiguration(): RuntimeConfiguration
546550
*/
547551
public function countPlaceholders(string $queryString): int
548552
{
553+
$queryString = QuerySimulation::stripComments($queryString);
554+
549555
// match named placeholders first, as the regex involved is more specific/less error prone
550556
$namedPlaceholders = $this->extractNamedPlaceholders($queryString);
551557

@@ -573,6 +579,7 @@ public function countPlaceholders(string $queryString): int
573579
*/
574580
public function containsNamedPlaceholders(string $queryString, array $parameters): bool
575581
{
582+
$queryString = QuerySimulation::stripComments($queryString);
576583
$namedPlaceholders = $this->extractNamedPlaceholders($queryString);
577584

578585
if ([] !== $namedPlaceholders) {
@@ -593,6 +600,8 @@ public function containsNamedPlaceholders(string $queryString, array $parameters
593600
*/
594601
public function extractNamedPlaceholders(string $queryString): array
595602
{
603+
$queryString = QuerySimulation::stripComments($queryString);
604+
596605
if (preg_match_all(self::REGEX_NAMED_PLACEHOLDER, $queryString, $matches) > 0) {
597606
$candidates = $matches[0];
598607

src/QueryReflection/QuerySimulation.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,17 @@ public static function stripTrailers(string $queryString): ?string
144144
}
145145

146146
/**
147-
* @see https://larrysteinle.com/2011/02/09/use-regular-expressions-to-clean-sql-statements/
148147
* @see https://github.com/decemberster/sql-strip-comments/blob/3bef3558211a6f6191d2ad0ceb8577eda39dd303/index.js
149148
*/
150149
public static function stripComments(string $query): string
151150
{
151+
// one line comments: from "#" to end of line,
152+
// one line comments: from "--" to end of line,
153+
// or multiline: from "/*" to "*/".
154+
// string literals with sql comments omited
155+
// nested comments are not supported
152156
return trim(preg_replace_callback(
153-
'/("(""|[^"])*")|(\'(\'\'|[^\'])*\')|(--[^\n\r]*)|(\/\*[\w\W]*?(?=\*\/)\*\/)/m',
157+
'/("(""|[^"])*")|(\'(\'\'|[^\'])*\')|((?:--|#)[^\n\r]*)|(\/\*[\w\W]*?(?=\*\/)\*\/)/m',
154158
static function (array $matches): string {
155159
$match = $matches[0];
156160
$matchLength = \strlen($match);

tests/default/DbaInferenceTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public function dataFileAsserts(): iterable
2020

2121
yield from $this->gatherAssertTypes(__DIR__ . '/data/doctrine-dbal.php');
2222
yield from $this->gatherAssertTypes(__DIR__ . '/data/inference-placeholder.php');
23+
24+
// for some reason does not work in pgsql
25+
if ('pdo-pgsql' !== getenv('DBA_REFLECTOR')) {
26+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-680.php');
27+
}
2328
}
2429

2530
// make sure class constants can be resolved

0 commit comments

Comments
 (0)