php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71053 Type Hint about DateTime::diff does not match with informed
Submitted: 2015-12-07 20:08 UTC Modified: 2015-12-13 08:24 UTC
From: gabriel dot heming at hotmail dot com Assigned: laruence (profile)
Status: Closed Package: Date/time related
PHP Version: 7.0.0 OS: Debian 8.2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: gabriel dot heming at hotmail dot com
New email:
PHP Version: OS:

 

 [2015-12-07 20:08 UTC] gabriel dot heming at hotmail dot com
Description:
------------
The second parameter (or third one) of method DateTime::diff must be a bool (as described in php documentation). AS can see below

http://php.net/manual/en/datetime.diff.php

Object oriented style:

public DateInterval DateTime::diff ( DateTimeInterface $datetime2 [, bool $absolute = false ] );
public DateInterval DateTimeImmutable::diff ( DateTimeInterface $datetime2 [, bool $absolute = false ] );
public DateInterval DateTimeInterface::diff ( DateTimeInterface $datetime2 [, bool $absolute = false ] );

Test script:
---------------
<?php
declare(strict_types=1);

$date1 = clone $date2 = new \DateTime();

var_dump($date1->diff($date2 , true));

Expected result:
----------------
object(DateInterval)#3 (15) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> int(0) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }

Actual result:
--------------
Fatal error: Uncaught TypeError: DateTime::diff() expects parameter 2 to be integer, boolean given in /code/3uk3Qo:6 Stack trace: #0 /code/3uk3Qo(6): DateTime->diff(Object(DateTime), true) #1 {main} thrown in /code/3uk3Qo on line 6

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-07 20:19 UTC] gabriel dot heming at hotmail dot com
-Package: SPL related +Package: *General Issues
 [2015-12-07 20:19 UTC] gabriel dot heming at hotmail dot com
The same strange behavior occurs when try to inherits DateTime:

Declaration of MyDateTime::diff(DateTimeInterface $object, bool $absolute = false) must be compatible with DateTimeInterface::diff($object, $absolute = NULL)
 [2015-12-07 21:12 UTC] requinix@php.net
-Type: Documentation Problem +Type: Bug -Package: *General Issues +Package: Date/time related
 [2015-12-07 21:12 UTC] requinix@php.net
Looking at the code, there's no reason why $absolute should be anything but a boolean - all it does when enabled is turn off the invert flag on the interval object. So I think this should be addressed in the code, not the documentation.

As for the compatibility error, the bool in "bool $absolute" is not actually part of the method signature. The documentation is telling you that the parameter must be (strict) or will be coerced to (loose) a boolean, but the parameter itself is only defined as "$absolute". Keep an eye on bug #71051.
 [2015-12-08 11:06 UTC] gabriel dot heming at hotmail dot com
Thanks about the reply. I'll keep an eye in boths reports.

I don't know if it matters, but the compatibility error occurs also for the type hint \DateTimeInterface $object.
 [2015-12-09 03:41 UTC] reeze@php.net
Automatic comment on behalf of reeze
Revision: http://git.php.net/?p=php-src.git;a=commit;h=943b23dcf6666a708c2ad4470cad5db36d47683a
Log: Fixed bug #71053 (Type Hint about DateTime::diff does not match with informed)
 [2015-12-09 03:41 UTC] reeze@php.net
-Status: Open +Status: Closed
 [2015-12-09 03:44 UTC] reeze@php.net
Automatic comment on behalf of reeze
Revision: http://git.php.net/?p=php-src.git;a=commit;h=943b23dcf6666a708c2ad4470cad5db36d47683a
Log: Fixed bug #71053 (Type Hint about DateTime::diff does not match with informed)
 [2015-12-09 03:50 UTC] reeze@php.net
@requinix I agree with you. It is a bug, at least in strict types context, there might be more cases in repo like this. I will try to find them.
 [2015-12-09 04:01 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f248309b7485fea08e3c112adab97bfa1fc74df4
Log: Revert &quot;Fixed bug #71053 (Type Hint about DateTime::diff does not match with informed)&quot;
 [2015-12-09 04:04 UTC] laruence@php.net
-Status: Closed +Status: Re-Opened
 [2015-12-09 04:04 UTC] laruence@php.net
I reverted the fix, because it break common used

$data->diff($data2, 1);

I'd prefer doc bug instead.

thanks
 [2015-12-09 13:35 UTC] gabriel dot heming at hotmail dot com
If the PHP 7 was builded to be faster and more strict, why this could be accepted as a doc bug?

I mean how could be more than a value, since the manual say about a boolean, how someone could use an integer instead?
 [2015-12-09 14:40 UTC] laruence@php.net
-Type: Bug +Type: Documentation Problem
 [2015-12-09 14:40 UTC] laruence@php.net
then what the problem if we change bool to int in doc?

thanks
 [2015-12-09 15:01 UTC] salathe@php.net
-Type: Documentation Problem +Type: Bug
 [2015-12-09 15:01 UTC] salathe@php.net
This is not a documentation bug. The source code should stipulate boolean for the $absolute parameter. That is the correct type given the a) documentation, b) prototype in the source, c) the use of the argument in the source.

If fixing the type breaks existing old (incorrect with strict types) code using a value of 1 for the argument, so be it.
 [2015-12-09 15:17 UTC] laruence@php.net
@salathe , it's not a simple fix that change l to b. internal functions are lots of mis-set type in parse parameters.

and it even dones't have refelction supports, if you try to reflects the paramters , and get the type of it , it returns NULL. is that also need to be fixed?

so, only fix this part means nothing. and I agree that this is a 'bug', but I'd prefer to do the whole fix only in master. and considering of this will leads people wrongly. change doc instead of change codes seems more safe for me.

thanks
 [2015-12-09 16:06 UTC] gabriel dot heming at hotmail dot com
@laruance by my interpretation, "safe" not means "correct". 

If we can report bugs (to me, this mismatch type is a bug), fix could create some BC and BC is not the safe way, why the reason we must report bugs?
 [2015-12-09 16:13 UTC] nikic@php.net
@laruence: Reflection does not provide type info for nearly all internal functions. That's not a reason for not fixing zpp type specs.

Imho this is a bug fix and it's better to do it earlier than later, otherwise people using strict_types will start relying on it.
 [2015-12-10 00:53 UTC] laruence@php.net
-Assigned To: +Assigned To: laruence
 [2015-12-10 00:53 UTC] laruence@php.net
okey, let me think about a whole fix way. maybe depends on parsing the doc repo
 [2015-12-13 08:24 UTC] laruence@php.net
-Status: Re-Opened +Status: Closed
 [2015-12-13 08:24 UTC] laruence@php.net
fix has been committed in https://github.com/php/php-src/commit/4171a8f6c631e3c34d1d070d553e7611b5474263

and some doc bugs also be reported in #71107
 [2016-07-20 11:34 UTC] davey@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f248309b7485fea08e3c112adab97bfa1fc74df4
Log: Revert &quot;Fixed bug #71053 (Type Hint about DateTime::diff does not match with informed)&quot;
 [2016-07-20 11:34 UTC] davey@php.net
Automatic comment on behalf of reeze
Revision: http://git.php.net/?p=php-src.git;a=commit;h=943b23dcf6666a708c2ad4470cad5db36d47683a
Log: Fixed bug #71053 (Type Hint about DateTime::diff does not match with informed)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC