phinx icon indicating copy to clipboard operation
phinx copied to clipboard

PHP 8.1 : Type issue when PDOException is thrown

Open gertvdb opened this issue 3 years ago • 3 comments

PDOException can return an int|mixed. This code is directly passed to InvalidArgumentException which requires 'code' to be an int. So this will crash in php 8.1 en therefor not show the real error.

https://github.com/cakephp/phinx/blob/15be19734a596051120ff34a27e6ba39d65743d0/src/Phinx/Db/Adapter/PdoAdapter.php#L96

gertvdb avatar Jul 05 '22 12:07 gertvdb

How are you connecting to the database that is causing the throw to have a non-number code?

~Per the PHP docs for PDOException, code should always be an int.~. Nevermind, I see that $code is listed twice, once as string|int and once as int, confusing. Having a reproducible error though would help so can write a test with a fix.

MasterOdin avatar Jul 08 '22 03:07 MasterOdin

The error should happen when you have a wrong host in your configuration file. It is indeed a language issue and $code should not be a string but it is sometimes.

/**
     * Gets the Exception code
     * @link https://php.net/manual/en/exception.getcode.php
     * @return mixed|int the exception code as integer in
     * <b>Exception</b> but possibly as other type in
     * <b>Exception</b> descendants (for example as
     * string in <b>PDOException</b>).
     */
    #[Pure]
    final public function getCode() {}

gertvdb avatar Jul 08 '22 06:07 gertvdb

Thanks for the information. I've opened a PR to just not bother setting the code for the recasted exception, and hopefully downstream users of phinx are not relying on that field, though I think this is probably the minimal breaking change as opposed to moving to a different exception that allows string codes, or just not bother recasting (which has its own downsides like potentially leaking DB info?).

MasterOdin avatar Jul 08 '22 22:07 MasterOdin