Incorrect type in SelectStatement::$expr when query has CASE-clause in selected fields
还没有人认领这个 Issue。
评估
调研方向
从 issue 中的 Parser 复现开始,检查选择 CASE 子句时 SelectStatement::$expr 是如何填充的。比较 Expression 和 CaseExpression 类型,并通过重新运行所提供的脚本来验证预期的契约;完成的标准是报告的 TypeError 不再发生,同时表达式列表仍保持正确的类型。
由索引模型根据 Issue 内容生成。
描述
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
- 主要语言
- PHP
- 星标
- 485
- 派生
- 119
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
phpmyadmin/sql-parser 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
phpmyadmin/sql-parser#655 · 1 个 reaction ·
-
难度 3/5 1-2 天 新手友好度 55/100
phpmyadmin/sql-parser#666 ·
-
难度 3/5 1-2 天 新手友好度 65/100
phpmyadmin/sql-parser#662 · 2 个 reaction ·
-
kind/support
难度 3/5 1-2 天 新手友好度 45/100
phpmyadmin/sql-parser#653 · 1 个 reaction ·
-
难度 3/5 1-2 天 新手友好度 70/100
phpmyadmin/sql-parser#649 ·
查看 phpmyadmin/sql-parser 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 85/100
-
a11y admissions.uiowa.edu needs grooming SiteImprove best practice
难度 2/5 1-3 小时 新手友好度 70/100
-
Save States Menu 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 2/5 1-3 小时 新手友好度 84/100
pluginsGLPI/datainjection#673 ·