php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79155 Property nullability lost when using multiple property definition
Submitted: 2020-01-22 13:33 UTC Modified: 2020-01-23 11:35 UTC
From: ondrej at mirtes dot cz Assigned: nikic (profile)
Status: Closed Package: Reflection related
PHP Version: 7.4.1 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ondrej at mirtes dot cz
New email:
PHP Version: OS:

 

 [2020-01-22 13:33 UTC] ondrej at mirtes dot cz
Description:
------------
When I define multiple properties within a single statement and a native typehint, ReflectionType reports wrong allowsNull():

private ?DateTimeImmutable $startDate, $endDate;

Reproduction: https://3v4l.org/hITTk

Related PHPStan issue: https://github.com/phpstan/phpstan/issues/2886

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

final class CreateStatementRequest
{
    private ?DateTimeImmutable $startDate, $endDate;
}

$ref = new \ReflectionClass(CreateStatementRequest::class);
foreach ($ref->getProperties() as $refProperty) {
    var_dump($refProperty->getName());
    $refType = $refProperty->getType();
    var_dump($refType->getName());
    var_dump($refType->allowsNull());
    
    var_dump('---');
}

Expected result:
----------------
string(9) "startDate"
string(17) "DateTimeImmutable"
bool(true)
string(3) "---"
string(7) "endDate"
string(17) "DateTimeImmutable"
bool(true)
string(3) "---"

Actual result:
--------------
string(9) "startDate"
string(17) "DateTimeImmutable"
bool(true)
string(3) "---"
string(7) "endDate"
string(17) "DateTimeImmutable"
bool(false)
string(3) "---"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-23 10:26 UTC] a at b dot c dot de
Not specifically a reflection issue: the ReflectionClass is accurately reflecting the state of affairs:

<?php
class foo
{

	public ?string $a, $b;

}

$t = new foo;
$t->a = "start";
$t->b = "end";

echo $t->a, ' ', $t->b, "\n";

echo "Nulling a:\n";
$t->a = null;

echo "Nulling b:\n";
$t->b = null;
?>

In the above code, property 'a' is nullable, but property 'b' is not.

Which seems wrong.
 [2020-01-23 11:35 UTC] nikic@php.net
-Summary: Wrong ReflectionType::allowsNull value with multiple property definition +Summary: Property nullability lost when using multiple property definition -Assigned To: +Assigned To: nikic
 [2020-01-23 11:56 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2eb33818b60f0e089833937605f07fe2dc0a3237
Log: Fixed bug #79155
 [2020-01-23 11:56 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 15 09:01:26 2024 UTC