From 35ca80b3b4d990abc4ed59c6384e75e3fd23d0dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Thu, 4 Jul 2024 00:58:56 +0200 Subject: [PATCH] Fix query flags for lower-case functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Krög --- phpstan-baseline.neon | 5 +++++ psalm-baseline.xml | 3 ++- src/Utils/Query.php | 6 ++++-- tests/Utils/QueryTest.php | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 878a51123..59f22a08d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -955,6 +955,11 @@ parameters: count: 1 path: src/Utils/Query.php + - + message: "#^Parameter \\#1 \\$str of function strtoupper expects string, mixed given\\.$#" + count: 1 + path: src/Utils/Query.php + - message: "#^Cannot access offset 'value' on mixed\\.$#" count: 3 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 80d5bdb4a..f3e223602 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1342,8 +1342,9 @@ int - + $expr + $expr->function $clauses[$token->keyword] diff --git a/src/Utils/Query.php b/src/Utils/Query.php index f5b5320b7..8ebd239e3 100644 --- a/src/Utils/Query.php +++ b/src/Utils/Query.php @@ -36,6 +36,7 @@ use function count; use function in_array; use function is_string; +use function strtoupper; use function trim; /** @@ -317,9 +318,10 @@ private static function getFlagsSelect($statement, $flags) foreach ($expressions as $expr) { if (! empty($expr->function)) { - if ($expr->function === 'COUNT') { + $function = strtoupper($expr->function); + if ($function === 'COUNT') { $flags['is_count'] = true; - } elseif (in_array($expr->function, static::$FUNCTIONS)) { + } elseif (in_array($function, static::$FUNCTIONS, true)) { $flags['is_func'] = true; } } diff --git a/tests/Utils/QueryTest.php b/tests/Utils/QueryTest.php index 62981ed0a..53f34c7aa 100644 --- a/tests/Utils/QueryTest.php +++ b/tests/Utils/QueryTest.php @@ -181,6 +181,24 @@ public function getFlagsProvider(): array 'querytype' => 'SELECT', ], ], + [ + 'SELECT count(*) FROM tbl', + [ + 'is_count' => true, + 'is_select' => true, + 'select_from' => true, + 'querytype' => 'SELECT', + ], + ], + [ + 'SELECT sum(*) FROM tbl', + [ + 'is_func' => true, + 'is_select' => true, + 'select_from' => true, + 'querytype' => 'SELECT', + ], + ], [ 'SELECT (SELECT "foo")', [