[PHP-DEV][PRE-RFC] PREG_THROW_ON_ERROR flag

Osama Aldemeery <[email protected]>
Newsgroups gmane.comp.php.devel
Message-ID <CABE7Eu8SUXaYMondN6z26dyvAOCYg0V_VdnTJU2nt7r1_1R9tQ@mail.gmail.com>
Hi all,

I'd like to propose adding a `PREG_THROW_ON_ERROR` flag to the `preg_*()`
functions, and gauge interest in that.

Json and `ext/filter` both grew an opt-in way to turn a silent error into
an exception...that is `JSON_THROW_ON_ERROR` and `FILTER_THROW_ON_FAILURE`.
PCRE is the one left out where a failing call is easy to miss. An execution
error for example (e.g. a bad UTF-8) surfaces only if
you call `preg_last_error()` afterwards, and a malformed pattern emits a
warning and returns `false`.

`PREG_THROW_ON_ERROR` would do for PCRE what those two do for their
functions.
Passing it to any `preg_*()` call makes a PCRE error throw a
`Pcre\PcreException` that carries the `PREG_*_ERROR` code and the
`preg_last_error_msg()` text
instead of warning or returning `false`/`null`.
And it covers both kinds: a compilation error (e.g. a malformed pattern)
and an execution error, so opting in means any PCRE failure becomes a
single catchable exception.
Also, `preg_replace()` and `preg_filter()` gain an optional `$flags`
parameter to accept it (the only two without one as far as I know).

Concretely, the check you'd write today:

```
if (preg_match($pattern, $subject, $m) === false) {
    throw new RuntimeException(preg_last_error_msg());
}
```

...collapses to:

```
preg_match($pattern, $subject, $m, PREG_THROW_ON_ERROR);
```

First, I'd like to know whether there's interest at all and whether anyone
sees a reason not to add it.

If there is interest, a couple of design questions I'd want the list's read
on:

   1. Array subjcets. Currently `preg_last_error()` reflects only the last
   element processed, but I'd have the flag throw on the first failing
   element rather than keep last-one-wins...does that seem right?
   2. Whether `*_ON_ERROR` reads better than `*_ON_FAILURE` given the
   existing `preg_last_error()`/`PREG_*_ERROR` vocabulary.

If it seems worth pursuing, I'll write it up as a proper RFC.

Thanks,
Osama
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.