php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81303 Error message for match() is misleading and unhelpful
Submitted: 2021-07-27 16:45 UTC Modified: -
From: crell@php.net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 8.0.8 OS: Linux
Private report: No CVE-ID: None
 [2021-07-27 16:45 UTC] crell@php.net
Description:
------------
When a match() statement finds no matching arm, it throws an exception. The exception's error message text, however, is very generic. Specifically, it reads "Unhandled match value of type $type_of_the_match_var".  The type of the variable is rarely useful in that case.  The *value* of the variable is what didn't match, so that's what should be in the message.  However, it's not even in the thrown object at all.

Test script:
---------------
try {
  $v = match('float') {
    'string' => 's',
    'int' => 'i',
  };
  var_dump($v);
} catch (UnhandledMatchError $e) {
  var_dump($e);
}

Expected result:
----------------
An exception thrown with a less misleading error message string, including the value of the variable, not its type.

Actual result:
--------------
object(UnhandledMatchError)#1 (7) {
  ["message":protected]=>
  string(36) "Unhandled match value of type string"
  ["string":"Error":private]=>
  string(0) ""
  ["code":protected]=>
  int(0)
  ["file":protected]=>
  string(9) "/in/U4Ns2"
  ["line":protected]=>
  int(4)
  ["trace":"Error":private]=>
  array(0) {
  }
  ["previous":"Error":private]=>
  NULL
}

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-27 20:34 UTC] crell@php.net
The following pull request has been associated:

Patch Name: Improve match() error messages
On GitHub:  https://github.com/php/php-src/pull/7312
Patch:      https://github.com/php/php-src/pull/7312.patch
 [2021-08-02 15:32 UTC] git@php.net
Automatic comment on behalf of krakjoe
Revision: https://github.com/php/php-src/commit/05ef6334cdf9bec98e056c0a80bd02a0a0ab242f
Log: Fix bug #81303 improve match errors
 [2021-08-02 15:32 UTC] git@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Nov 28 09:03:14 2021 UTC