Req #80389 [NEW]: With strict_types=1 catch type not validated
| From: | tom at tomegan dot tech | Date: | Fri, 20 Nov 2020 16:43:20 +0000 |
| Subject: | Req #80389 [NEW]: With strict_types=1 catch type not validated | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-230482@lists.php.net to get a copy of this message | ||
From: tom at tomegan dot tech
Operating system: any
PHP version: 7.4.12
Package: *Programming Data Structures
Bug Type: Feature/Change Request
Bug description:With strict_types=1 catch type not validated
Description:
------------
I encountered an issue with code in a namespaced file and strict_types=1
where i forgot to include either a use statement for \Exception or
indicate I wanted to catch an Exception in the default namespace. I
expected this code to trigger a warning or raise a TypeError as the
implied type did not exist. However, it appears that the type in a catch
statement is not validated. In my example, the catch statement tries to
catch a Bug\Exception, however this class does not exist and does not
match the \Exception which is thrown. The thrown \Exception is therefore
reported back. I believe that PHP developers would benefit from PHP
adding validation of the type to the catch statement at least in some
configuration(s) eg strict_types=1
Test script:
---------------
<?php
/**
* The type in a catch statement is not validated even with
strict_type=1
*
* @author Tom Egan
*/
declare(strict_types=1);
namespace Bug;
try {
throw new \Exception('A test exception');
} catch(Exception $e) {
echo $e->getMessage();
}
exit;
Expected result:
----------------
$ php bug-test.php
PHP Fatal error: Uncaught TypeError: the type Bug\Exception does not
exist bug-test.php:15
Stack trace:
#0 {main}
thrown in bug-test.php on line 15
Actual result:
--------------
$ php bug-test.php
PHP Fatal error: Uncaught Exception: A test exception in
bug-test.php:14
Stack trace:
#0 {main}
thrown in bug-test.php on line 14
--
Edit bug report at https://bugs.php.net/bug.php?id=80389&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80389&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80389&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80389&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80389&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80389&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80389&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80389&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80389&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80389&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80389&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80389&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80389&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80389&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80389&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80389&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80389&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80389&r=mysqlcfg