Skip to content

Commit

Permalink
Merge pull request #16 from laravel/fix/null-safe-operator-with-prope…
Browse files Browse the repository at this point in the history
…rties

Fixes null safe operator with properties
  • Loading branch information
nunomaduro authored Sep 29, 2021
2 parents 2f4d826 + f347507 commit ab1e38a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Support/ReflectionClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
defined('T_NAME_QUALIFIED') || define('T_NAME_QUALIFIED', -4);
defined('T_NAME_FULLY_QUALIFIED') || define('T_NAME_FULLY_QUALIFIED', -5);
defined('T_FN') || define('T_FN', -6);
defined('T_NULLSAFE_OBJECT_OPERATOR') || define('T_NULLSAFE_OBJECT_OPERATOR', -7);

use Closure;
use ReflectionFunction;
Expand Down Expand Up @@ -389,6 +390,7 @@ public function getCode()
$lastState = 'closure';
break;
case T_OBJECT_OPERATOR:
case T_NULLSAFE_OBJECT_OPERATOR:
case T_DOUBLE_COLON:
$code .= $token[1];
$lastState = 'closure';
Expand Down
17 changes: 16 additions & 1 deletion tests/ReflectionClosurePhp80Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
expect($f1)->toBeCode($e1);
});

test('null safe operator', function () {
test('null safe operator with methods', function () {
$f1 = function () {
$obj = new \stdClass();

Expand All @@ -52,6 +52,21 @@
expect($f1)->toBeCode($e1);
});

test('null safe operator with properties', function () {
$f1 = function () {
$obj = new \stdClass();

return $obj?->invalid;
};
$e1 = 'function () {
$obj = new \stdClass();
return $obj?->invalid;
}';

expect($f1)->toBeCode($e1);
});

test('trailling comma', function () {
$f1 = function (string $param, ) {
};
Expand Down

0 comments on commit ab1e38a

Please sign in to comment.