phpmyadmin / phpmyadmin/sql-parser
Incorrect type in SelectStatement::$expr when query has CASE-clause in selected fields
還沒有人認領這個 Issue。
- 主要語言
- PHP
- 星號
- 485
- 分支
- 119
- PR 合併指標
- 30 天內沒有已合併 PR
描述
The public field SelectStatement::$expr is defined in a PHP docblock as an Expression[]. When parsing a query with a CASE statement, a CaseExpression object is present in this list, despite CaseExpression not extending Expression. This causes issues when explicitly requiring the Expression type as should be returned by SelectStatement::$expr.
Reproduction case
<?php
declare(strict_types=1);
use PhpMyAdmin\SqlParser\Components\Expression;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\SelectStatement;
include_once 'vendor/autoload.php';
$p = new Parser('SELECT a, CASE WHEN b IS NOT NULL THEN 1 ELSE 0 END as c FROM t');
$stmt = $p->statements[0];
if (! $stmt instanceof SelectStatement) {
throw new Exception('Could not parse select statement');
}
function acceptExpression(Expression $e): void {}
$expressions = $stmt->expr;
foreach ($expressions as $expression) {
acceptExpression($expression);
}
Expected behavior
I would expect this script to not error out as the $stmt->expr returns Expression[] according to its PHP docblock. Each element should therefore be compatible with Expression in my acceptExpression function.
Real behavior
$ php test.php
PHP Fatal error: Uncaught TypeError: acceptExpression(): Argument #1 ($e) must be of type PhpMyAdmin\SqlParser\Components\Expression, PhpMyAdmin\SqlParser\Components\CaseExpression given, called in /path/to/test.php on line 22 and defined in /path/to/test.php:18
Stack trace:
#0 /path/to/test.php(22): acceptExpression()
#1 {main}
thrown in /path/to/test.php on line 18
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 issue 中的 Parser 重現開始,檢查選取 CASE 子句時 SelectStatement::$expr 是如何填入的。比較 Expression 和 CaseExpression 型別,並透過重新執行所提供的 script 來驗證預期的契約;完成的標準是所回報的 TypeError 不再發生,同時運算式清單仍保持正確的型別。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- php
- 領域
- databases
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100