Edit report at https://bugs.php.net/bug.php?id=79602&edit=1
ID: 79602
Comment by: ebonythomas121 at gmail dot com
Reported by: mikko dot rantalainen at peda dot net
Summary: wrong parameters to callback set via
assert_options(ASSERT_CALLBACK, ...)
Status: Open
Type: Bug
Package: *General Issues
Operating System: Ubuntu Linux 18.04 64 bit
PHP Version: 7.4.6
Block user comment: N
Private report: N
New Comment:
The behavior of assert() may be configured by assert_options() or by .ini-settings assert()
callbacks are https://www.nomnomnow.one/ particularly
useful for building automated test suites Parameters. assertion. The assertion. In PHP 5, this must
be either a string
Previous Comments:
------------------------------------------------------------------------
[2020-06-10 17:41:00] 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.
------------------------------------------------------------------------
[2020-06-10 08:28:05] 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-09 08:03:53] 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-08 19:41:27] 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-05-15 11:28:56] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79602&edit=1