php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62715 ReflectionParameter::isDefaultValueAvailable() wrong result
Submitted: 2012-08-01 11:24 UTC Modified: 2012-08-24 09:59 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: benjamin dot morel at strictcoding dot co dot uk Assigned: johannes
Status: Assigned Package: Reflection related
PHP Version: 5.4.5 OS: CentOS 6.3, Windows 7
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-08-01 11:24 UTC] benjamin dot morel at strictcoding dot co dot uk
Description:
------------
When ReflectionParameter::isOptional() returns false, because other parameters 
after it are required, isDefaultValueAvailable() always return false, even is 
there actually is a default value.

I've read this older bug report: https://bugs.php.net/bug.php?id=41382
It looks like at that time, this behaviour had been considerer consistent.
I do think this should be revisited however, as in the example function below, 
there is absolutely no way with Reflection to get the default value for the 
given parameter.

Reflection is a powerful tool for autowiring in Dependency Injection containers, 
where it is interesting to check whether default parameters are available, 
regardless of their order.


Test script:
---------------
function test(PDO $a = null, $b = 0, array $c) {}
$r = new ReflectionFunction('test');

foreach ($r->getParameters() as $p) {
    echo $p->getName();
    echo "   isDefaultValueAvailable: " . var_export($p->isDefaultValueAvailable(), true) . "\n";
    echo "    isOptional: " . var_export($p->isOptional(), true) . "\n";
    echo "    allowsNull: " . var_export($p->allowsNull(), true) . "\n";
    echo "\n";
}

Expected result:
----------------
a   isDefaultValueAvailable: true
    isOptional: false
    allowsNull: true

b   isDefaultValueAvailable: true
    isOptional: false
    allowsNull: true

c   isDefaultValueAvailable: false
    isOptional: false
    allowsNull: false

Actual result:
--------------
a   isDefaultValueAvailable: false
    isOptional: false
    allowsNull: true

b   isDefaultValueAvailable: false
    isOptional: false
    allowsNull: true

c   isDefaultValueAvailable: false
    isOptional: false
    allowsNull: false

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-01 12:21 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=10642aa9e4f1eb694a8f7b514cc234cb24545744
Log: Fixed bug #62715 (ReflectionParameter::isDefaultValueAvailable() wrong result)
 [2012-08-01 12:22 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=10642aa9e4f1eb694a8f7b514cc234cb24545744
Log: Fixed bug #62715 (ReflectionParameter::isDefaultValueAvailable() wrong result)
 [2012-08-01 12:23 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=10642aa9e4f1eb694a8f7b514cc234cb24545744
Log: Fixed bug #62715 (ReflectionParameter::isDefaultValueAvailable() wrong result)
 [2012-08-01 12:24 UTC] laruence@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: laruence
 [2012-08-01 12:24 UTC] laruence@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2012-08-02 10:19 UTC] benjamin dot morel at strictcoding dot co dot uk
Thanks for the quick fix, isDefaultValueAvailable() works indeed, but 
getDefaultValue() still throws an exception:

<?php
function test(PDO $a = null, $b = 0, array $c) {}
$r = new ReflectionFunction('test');

foreach ($r->getParameters() as $p) {
    if ($p->isDefaultValueAvailable()) {
        var_export($p->getDefaultValue());
    }
}

Fatal error: Uncaught exception 'ReflectionException' with message 'Parameter is 
not optional'
 [2012-08-02 14:26 UTC] laruence@php.net
Hi, fixed :)
 [2012-08-02 22:53 UTC] benjamin dot morel at strictcoding dot co dot uk
I can now confirm this works as expected. Thanks for your time!
 [2012-08-23 09:11 UTC] phpnet at bastian-frank dot de
It seems that 5.3.16 fixes the bug in isDefaultValueAvailable() but does NOT fix 
getDefaultValue, so the situation in 5.3.16 is even worse than before.

This breaks Twig and Doctrine in current versions, so 5.3.16 is not useable for 
Symfony 2 at the moment.

Is there a chance to get a new 5.3 release soon?
 [2012-08-23 14:25 UTC] laruence@php.net
the getdefaultvalue fix is : 170ee90bf962d288bdcf6cf0c8c4a2a30c5c1ba2
 [2012-08-24 09:59 UTC] pajoye@php.net
-Status: Closed +Status: Assigned -Assigned To: laruence +Assigned To: johannes
 [2012-08-24 09:59 UTC] pajoye@php.net
Johannes, please see: http://news.php.net/php.internals/62421
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 18:01:55 2014 UTC