php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79602 wrong parameters to callback set via assert_options(ASSERT_CALLBACK, ...)
Submitted: 2020-05-15 11:28 UTC Modified: 2023-01-04 16:59 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: mikko dot rantalainen at peda dot net Assigned: cmb (profile)
Status: Wont fix Package: *General Issues
PHP Version: 7.4.6 OS: Ubuntu Linux 18.04 64 bit
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
31 + 46 = ?
Subscribe to this entry?

 
 [2020-05-15 11:28 UTC] mikko dot rantalainen at peda dot net
Description:
------------
---
From manual page: https://php.net/function.assert-options
---

The documentation says that the signature of callback should be

assert_callback(string $file, int $line, string $assertion, string $description)

However, the $assertion will be passed an empty string unless the deprecated syntax is used for the actual assert(). The actual assertion should be always passed as the 3rd parameter.

Note that if first parameter to assert() is a string, the string will be passed as 3rd parameter to the handler. If first parameter is not a string and second parameter is not set, the assert code will be passed as 4th parameter to the handler. If first parameter passed to assert() is not a string and second parameter is a string, the actual assert code will be lost and the string given as the second parameter will be passed as the 4th parameter to the handler.

The behavior seems to have been the same since version 5.4.8. Version 5.4.7 or any older didn't support the second parameter to assert().

The only way to get the expected output with current PHP version is to use deprecated syntax assert(string, string) where both 3rd and 4th parameter passed to assert handler will be correctly set.

I think the behavior of PHP should be changed instead of simply changing the documentation. It really makes zero sense to always pass empty string as the 3rd argument if non-deprecated features are used. (And yeah, I see that sniper@php.net considered this feature-not-a-bug in 2002 - perhaps it's time to reconsider now that the string as the first argument to assert() has been deprecated: https://bugs.php.net/bug.php?id=20075)

Example test also at https://3v4l.org/1G2Zo


Test script:
---------------
<?php
assert_options(ASSERT_CALLBACK, "assert_handler");

function assert_handler($file, $line, $assertion, $description="")
{
  echo "* assert_handler('$file', '$line', '$assertion', '$description') called";
  return true;
}

assert('false == true');
assert(false == true);
assert(false == true, "Expected assert() to fail for this test and emit the actual test as 3rd parameter to handler");

Expected result:
----------------
* assert_handler('/in/1G2Zo', '10', 'false == true', '') called
* assert_handler('/in/1G2Zo', '11', 'false == true', '') called
* assert_handler('/in/1G2Zo', '12', 'false == true', 'Expected assert() to fail for this test and emit the actual test as 3rd parameter to handler') called


Actual result:
--------------
* assert_handler('/in/1G2Zo', '10', 'false == true', '') called
* assert_handler('/in/1G2Zo', '11', '', 'assert(false == true)') called
* assert_handler('/in/1G2Zo', '12', '', 'Expected assert() to fail for this test and emit the actual test as 3rd parameter to handler') called


Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-08 19:41 UTC] alexinbeijing at gmail dot com
Hmm. Well, first thing, using string-type assertions was deprecated in PHP 7.2 and support was completely removed in January 2019.

If you look at the implementation of `assert` in recent versions on PHP, it makes no attempt at all to pass the assertion as the 3rd argument to the callback. Rather, it always passes `null`.

When the assertion was passed as a string, it was easy to pass it to a callback, but now that it's not, it is quite hard to see how to implement that. Probably it would be more practical to just update the documentation to indicate that the 3rd argument is always `null` now.
 [2020-06-09 08:03 UTC] alexinbeijing at gmail dot com
Just looking at this again. Maybe it's not so hard to implement. Will give it a try.
 [2020-06-10 08:28 UTC] carusogabriel@php.net
The following pull request has been associated:

Patch Name: assert passes 3rd arg to callback (as stated in documentation)
On GitHub:  https://github.com/php/php-src/pull/5691
Patch:      https://github.com/php/php-src/pull/5691.patch
 [2020-06-10 17:41 UTC] alexinbeijing at gmail dot com
Looks like this one will not be 'fixed'; assert callbacks are probably on the way out anyways. (See discussion in the attached PR.)

I have just submitted a PR to adjust the manual so that it is more clear in regard to the arguments passed to assert callbacks.
 [2023-01-04 16:59 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2023-01-04 16:59 UTC] cmb@php.net
> Looks like this one will not be 'fixed'; assert callbacks are
> probably on the way out anyways.

Right.  Thus closing as WONTFIX.

Doc-fix: <https://github.com/php/doc-en/commit/e2e41fdb46fd7e23acc16c7e7ff36a9894b2cec7>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 14:01:29 2024 UTC