assert icon indicating copy to clipboard operation
assert copied to clipboard

Add support for lazy messages

Open theofidry opened this issue 3 years ago • 3 comments

On some occasions, in order to provide a helpful error message, more processing is required to provide a helpful message:

$message = $createMessage(...); // heavy stuff happening there

Assert::smth(..., $message);

Maybe it would be cool to have support for $message being string|():string instead of just string:

Assert::smth(..., $createMessage);

In some cases this may end up more verbose, but on the other hand it prevents the assertion from being expensive in case of non-failure (the happy path).

WDYT?

theofidry avatar Feb 05 '22 12:02 theofidry

Doing some profiling on our app, it often happens that sprintf() calls with complex processing (implode and such) are the culprit of bad performance. Worst than that, 99% of the time they are executed without any need to.

It's sad to have to use if (...) { throw new InvalidArgumentException(...); } instead of Assert just for that reason... @BackEndTea What would be needed to make this issue fixed? Is there a particular blocker?

gnutix avatar Feb 27 '23 06:02 gnutix

I think a callback function would be a great addition yes.

kkevindev avatar Nov 21 '25 19:11 kkevindev