# Change to Too Few Args Exception RFC

- Thread: https://externals.io/message/94871
- Original archive: https://news-web.php.net/php.internals/94871
- Started by Davey Shafik on 2016-08-06T02:26:19+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #94871 — Davey Shafik — 2016-08-06T02:26:19+00:00

URL: https://externals.io/message/94871#94871

Hey all,

I know this is a little late in the process, but it's something I've
noticed while prepping some content around 7.1.

This RFC: https://wiki.php.net/rfc/too_few_args

Passed, and has been implemented, but I feel that throwing an `\Error`
exception is a mistake. I think we should another more concrete exception
class for this error:

`\TooFewArgumentsError extends \Error`

A use case where this may trivially occur is where you are using argument
unpacking and the unpacked array is too small. Writing this, just looks bad:

try {
   foo(… $args);
} catch (\Error $e) { }

compared:

try {
   foo(… $args);
} catch (\TooFewArgumentsError $e) { }

Thoughts? Dmitry?

Given the tiny change this is, and that is backwards compatible with the
original RFC, I would like to add this to 7.1 for beta3.

I think I can make this change myself.

- Davey

---

## #94877 — Nikita Popov — 2016-08-06T09:34:00+00:00

URL: https://externals.io/message/94871#94877 · In reply to: #94871

> Hey all,
>
> I know this is a little late in the process, but it's something I've
> noticed while prepping some content around 7.1.
>
> This RFC: https://wiki.php.net/rfc/too_few_args
>
> Passed, and has been implemented, but I feel that throwing an `\Error`
> exception is a mistake. I think we should another more concrete exception
> class for this error:
>
> `\TooFewArgumentsError extends \Error`
>
> A use case where this may trivially occur is where you are using argument
> unpacking and the unpacked array is too small. Writing this, just looks
> bad:
>
> try {
>    foo(… $args);
> } catch (\Error $e) { }
>
> compared:
>
> try {
>    foo(… $args);
> } catch (\TooFewArgumentsError $e) { }
>
> Thoughts? Dmitry?
>
> Given the tiny change this is, and that is backwards compatible with the
> original RFC, I would like to add this to 7.1 for beta3.
>
> I think I can make this change myself.
>
> - Davey
>

Problem: We already use TypeError for this for internal functions. If we
want to introduce an extra exception for this, lets use it for internal
functions as well. In that case we should probably go with something that
applies not just to too few arguments, but also to too many.

Nikita

---

## #94878 — Davey Shafik — 2016-08-06T09:53:27+00:00

URL: https://externals.io/message/94871#94878 · In reply to: #94877

> 
>
> > […]
>
> Problem: We already use TypeError for this for internal functions. If we
> want to introduce an extra exception for this, lets use it for internal
> functions as well. In that case we should probably go with something that
> applies not just to too few arguments, but also to too many.
>

Are you saying that in PHP 7.0, if you call an internal function with too
few, or too many arguments it will emit a TypeError exception?

Can you provide examples? It's obviously not every function, e.g. `fopen()`
emits a Warning if you forget the second arg.

With that in mind, to make this BC, TooFewArguments would have to extend
TypeError which isn't great.  Maybe, "ArgumentError" and the message will
make it clear if it's too few or too many?

- Davey

---

## #94879 — Niklas Keller — 2016-08-06T10:44:41+00:00

URL: https://externals.io/message/94871#94879 · In reply to: #94878

2016-08-06 11:53 GMT+02:00 Davey Shafik <davey@php.net>:

> 
>
> > […]
> exception
> > […]
> argument
> > […]
>
> Are you saying that in PHP 7.0, if you call an internal function with too
> few, or too many arguments it will emit a TypeError exception?
>
> Can you provide examples? It's obviously not every function, e.g. `fopen()`
> emits a Warning if you forget the second arg.
>
> With that in mind, to make this BC, TooFewArguments would have to extend
> TypeError which isn't great.  Maybe, "ArgumentError" and the message will
> make it clear if it's too few or too many?
>
> - Davey
>

I don't like "ArgumentError", how about "WrongArgumentCountError"? Maybe
also "WrongArgumentsError".

Regards, Niklas

---

## #94880 — Nikita Popov — 2016-08-06T11:02:13+00:00

URL: https://externals.io/message/94871#94880 · In reply to: #94878

> 
>
> > […]
>
> Are you saying that in PHP 7.0, if you call an internal function with too
> few, or too many arguments it will emit a TypeError exception?
>
> Can you provide examples? It's obviously not every function, e.g. `fopen()`
> emits a Warning if you forget the second arg.
>
> With that in mind, to make this BC, TooFewArguments would have to extend
> TypeError which isn't great.  Maybe, "ArgumentError" and the message will
> make it clear if it's too few or too many?
>

Forgot to mention, this is only for strict_types=1, or if zpp explicitly
opts to throw (e.g. constructors).

Nikita

---

## #94882 — Thomas Punt — 2016-08-06T11:10:23+00:00

URL: https://externals.io/message/94871#94882

Hi!

> From: me@kelunik.com>
> I don't like "ArgumentError", how about "WrongArgumentCountError"? Maybe
> also "WrongArgumentsError".

I'd favour simply ArgumentCountError. No need to prepend a "wrong" to theerror class name - we can already guess it's wrong because it has resulted inan error :)
-Tom

---

## #94888 — Davey Shafik — 2016-08-06T23:04:04+00:00

URL: https://externals.io/message/94871#94888 · In reply to: #94882

AFAICT, to make this change, I'd have to modify:

ZEND_API ZEND_COLD void zend_internal_type_error(zend_bool throw_exception,
const char *format, ...) /* {{{ */

To be:

ZEND_API ZEND_COLD void zend_internal_type_error(zend_bool throw_exception,
zend_class_entry *zend_error, const char *format, …) /* {{{ */

Which… would be a BC break for extensions perhaps?

There is also zend_type_error, I'm not sure how that relates, I don't think
it gets used in this case. Or, I could add
zend_(internal_?)argument_count_error and use that instead.

Thoughts?

- Davey




> Hi!
>
> > […]
>
> I'd favour simply ArgumentCountError. No need to prepend a "wrong" to the
> error class name - we can already guess it's wrong because it has resulted
> in
> an error :)
>
> -Tom

---

## #95230 — Davey Shafik — 2016-08-16T13:44:51+00:00

URL: https://externals.io/message/94871#95230 · In reply to: #94888

Hey all,

Can we please reach a resolution on this?

- Davey



> AFAICT, to make this change, I'd have to modify:
>
> ZEND_API ZEND_COLD void zend_internal_type_error(zend_bool
> throw_exception, const char *format, ...) /* {{{ */
>
> To be:
>
> ZEND_API ZEND_COLD void zend_internal_type_error(zend_bool
> throw_exception, zend_class_entry *zend_error, const char *format, …) /*
> {{{ */
>
> Which… would be a BC break for extensions perhaps?
>
> There is also zend_type_error, I'm not sure how that relates, I don't
> think it gets used in this case. Or, I could add zend_(internal_?)argument_count_error
> and use that instead.
>
> Thoughts?
>
> - Davey
>
>
> 
>
> > […]
