php / php/php-src

PDOException::getCode() return value type is string

オープン
#9,529 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Status: Requires RFC
主要言語
C
スター
40.4k
フォーク
8.1k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

Description

The following code:

<?php

declare(strict_types=1);

try
{
    $pdo = new PDO('mysql:host=127.0.0.1', 'root', 'root');
    $pdo->query('select x');
}
catch (\Throwable $e)
{
    var_dump($e->getCode());
    throw new \Exception($e->getMessage(), $e->getCode(), $e);
}

Resulted in this output:

string(5) "42S22"
PHP Fatal error:  Uncaught TypeError: Exception::__construct(): Argument #2 ($code) must be of type int, string given in /mnt/d/projects/imi/imi-2.1/test.php:13
Stack trace:
#0 /mnt/d/projects/imi/imi-2.1/test.php(13): Exception->__construct()
#1 {main}
  thrown in /mnt/d/projects/imi/imi-2.1/test.php on line 13

But I expected this output instead:

string(5) "42S22"
PHP Fatal error:  Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'x' in 'field list' in /mnt/d/projects/imi/imi-2.1/test.php:8
Stack trace:
#0 /mnt/d/projects/imi/imi-2.1/test.php(8): PDO->query()
#1 {main}

Next Exception: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'x' in 'field list' in /mnt/d/projects/imi/imi-2.1/test.php:13
Stack trace:
#0 {main}
  thrown in /mnt/d/projects/imi/imi-2.1/test.php on line 13

Is it possible to change the return value of Throwable::getCode() to int|string?

https://github.com/php/php-src/blob/eb22f1a31ad02699c718fe15ce648acd401900e4/Zend/zend_exceptions.stub.php#L12

PHP Version

8.0.21

Operating System

Ubuntu 20.04

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、Zend/zend_exceptions.stub.php にリンクされている Throwable の宣言を確認し、PHP 8.0.21 で PDOException の例を再現します。例外コードがどのように表現され、検査されているかを追跡し、次に提案されている戻り値の型 int|string に必要な互換性要件を判断します。意図された動作と、影響を受けるテストまたは API の制約が文書化され、検証されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
php
領域
backend-api-design
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。