Req #60341 [Com]: SplFixedArray should throw specific exceptions.
Edit report at https://bugs.php.net/bug.php?id=60341&edit=1
ID: 60341
Comment by: morrison dot levi at gmail dot com
Reported by: morrison dot levi at gmail dot com
Summary: SplFixedArray should throw specific exceptions.
Status: Assigned
Type: Feature/Change Request
Package: SPL related
Operating System: irrelevant
PHP Version: 5.3
Assigned To: colder
Block user comment: N
Private report: N
New Comment:
The proposed patch addresses all the issues I have submitted. However, it is
important to note that it breaks backwards compatibility in these areas:
- Passing an Object as a key will throw InvalidArgumentException which does not
inherit from RuntimeException.
- Passing a key that does not represent an int will throw
InvalidArgumentException instead of RuntimeException.
All other changes are backwards compatible (I believe. Still new to patching
PHP).
Previous Comments:
------------------------------------------------------------------------
[2011-11-22 21:56:39] morrison dot levi at gmail dot com
Note that the proposed patch isn't perfect. Using a string index that is not
numeric will throw OutOfBoundsException instead of InvalidArgumentException. I
haven't figured out how to do that in C yet, hopefully I'll figure it out soon or
someone else knows how and can submit the patch.
------------------------------------------------------------------------
[2011-11-22 19:46:59] morrison dot levi at gmail dot com
I have a patch nearly ready for this. It's at home, not on this machine, but I
noticed that it got assigned, so I thought it was worth mentioning.
Also, I think that:
$fixedArray[] = '';
Should throw OverflowException instead of RuntimeException. See
http://stackoverflow.com/questions/8219158/correct-exception-type-for-adding-to-
an-array-when-it-isnt-allowed and
http://php.net/manual/en/class.overflowexception.php
------------------------------------------------------------------------
[2011-11-21 17:36:11] morrison dot levi at gmail dot com
This should really be titled 'SplFixedArray should throw specific exceptions'
------------------------------------------------------------------------
[2011-11-19 21:31:32] morrison dot levi at gmail dot com
Description:
------------
SplFixedArray thankfully throws exceptions when you try to do incorrect things
with indices. However, the types of exceptions are just too generic. If I give
the wrong type of index, that's a logic error (I'd expect InvalidArgument or at
least something that inherits from LogicError to be thrown). If I give an index
that's that's a valid type but doesn't exist, I'd expect an OutOfBoundsException
to be thrown. Instead I get a generic RuntimeException.
I should expect because they are very different problems that I would at least
get a distinguishing message between the two. However, I get the same
descriptions: 'Index invalid or out of range'. The very message suggests they
should be different exceptions.
The first fix would sort-of break backwards compatibility: throw an
InvalidArgumentException for things of the wrong type.
The second fix, throw OutOfBoundsException on incorrect index, could be
implemented and keep backwards compatibility.
Test script:
---------------
$fa = new SplFixedArray(1);
$fa[new StdClass()]; //expect InvalidArgumentException or perhaps OutOfRangeException
$fa[2] = 'james'; // expect OutOfBoundsException
Expected result:
----------------
I expect $fa[new StdClass] to throw an InvalidArgumentException, not a
RuntimeException
I expect $fa['2'] to throw OutOfBoundsException not a RuntimeException.
Actual result:
--------------
Both throw RuntimeExceptions.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60341&edit=1
Thread (8 messages)