phpmyadmin / phpmyadmin/sql-parser
Incorrect type in SelectStatement::$expr when query has CASE-clause in selected fields
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- PHP
- Star
- 485
- Fork
- 119
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với phần tái hiện Parser trong issue và kiểm tra cách SelectStatement::$expr được điền khi một mệnh đề CASE được chọn. So sánh các kiểu Expression và CaseExpression, đồng thời xác minh hợp đồng dự kiến bằng cách chạy lại script được cung cấp; được xem là hoàn tất khi TypeError được báo cáo không còn xảy ra và danh sách biểu thức vẫn được định kiểu chính xác.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- php
- Lĩnh vực
- databases
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100