Req #64313 [Opn]: DateTime & DateTimeZone constructs should emit DateTimeException
Edit report at https://bugs.php.net/bug.php?id=64313&edit=1
ID: 64313
Updated by: heiglandreas@php.net
Reported by: mattsch at gmail dot com
Summary: DateTime & DateTimeZone constructs should emit
DateTimeException
Status: Open
Type: Feature/Change Request
Package: Date/time related
PHP Version: 5.3.22
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to consider this and also add a patch.
Sadly this has not received the attention it should have received and now the patch targets an old
and outdated version of PHP. Would you mind addapting the patch so that it works with the current
versions of PHP? And perhaps open a PR against https://github.com/php/php-src? That would make
integration of the patch even easier.
Thanks!
Previous Comments:
------------------------------------------------------------------------
[2015-08-18 13:28:45] cmb@php.net
JFTR: PHP throws a TypeError instead of Exception, if the second
argument isn't a DateTime, see <https://3v4l.org/X5YdR>.
------------------------------------------------------------------------
[2013-02-27 18:47:57] mattsch at gmail dot com
Forget the $format part of the test script. I changed the class slightly to show a simplified form
of the problem.
------------------------------------------------------------------------
[2013-02-27 18:46:31] mattsch at gmail dot com
Description:
------------
DateTime and DateTimeZone constructs currently emit Exception but they should really emit an
exception related to their respective classes. The reason for this is the code can run in multiple
environments (ui, phpunit, etc). If I want the exception to bubble up to the base exception in my
application environment, I want to catch it and rethrow it with an exception that ultimately extends
my app base exception. As a result, I must catch the base php Exception class and rethrow it with
the wrapper exception which extends my base app exception. The problem comes when I try to run
testcases on the class in the phpunit environment. Since phpunit emits exceptions, catching
Exception will also catch phpunit environment exceptions which is not what I want. As a result, I
must check the instance of the exception and allow phpunit exceptions to go through unchanged. If
the DateTime and DateTimeZone classes did not throw Exception, then this would not be a problem. In
fact, I d!
on't think any php command should actually throw the main php Exception and should always
throw an exception that is extended from Exception to avoid problems like this.
Test script:
---------------
<?php
class DateTimeWrapper
{
private $dateTime;
public function __construct($time = 'now', DateTimeZone $timezone = null, $format = null)
{
$this->setDateTime($time, $timezone, $format);
}
public function setDateTime($time = 'now', DateTimeZone $timezone = null, $format = null)
{
try {
$this->dateTime = new DateTime($time, $timezone);
}
catch (Exception $e) {
/*
PHP DateTime really shouldn't throw the catch all Exception so the workaround is
to keep this from catching phpunit exceptions as well so do instance checks on it.
*/
// @codeCoverageIgnoreStart
if ($e instanceof PHPUnit_Framework_Error
|| $e instanceof PHPUnit_Framework_Exception) {
throw $e;
}
// @codeCoverageIgnoreEnd
throw new DateTimeWrapperException($e->getMessage(), $e->getCode(), $e);
}
}
}
Expected result:
----------------
php should not throw Exception directly but rather an exception that extends Exception.
Actual result:
--------------
php throws Exception in places like DateTime and DateTimeZone when it should really throw an
exception like DateTimeException instead which would extend Exception.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64313&edit=1
Thread (5 messages)