php-cfg icon indicating copy to clipboard operation
php-cfg copied to clipboard

Lack of support for nullable types

Open more7dev opened this issue 7 years ago • 5 comments

PHP 7.1 and above has nullable types.

Parser doesn`t recognize them.

Example: Change the example function declaration at the bottom of demo.php and add nullable type:

function foo(?array $a) {

Effect:

PHP Fatal error: Uncaught RuntimeException: Unknown Expr Type NullableType in .../vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:799 Stack trace: #0 .../vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php(1253): PHPCfg\Parser->parseExprNode(Object(PhpParser\Node\NullableType)) #1 .../vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php(95): PHPCfg\Parser->parseParameterList(Object(PHPCfg\Func), Array) #2 .../vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php(332): PHPCfg\Parser->parseFunc(Object(PHPCfg\Func), Array, Array, NULL) #3 .../vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php(146): PHPCfg\Parser->parseStmt_Function(Object(PhpParser\Node\Stmt\Function_)) #4 .../vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php(132): PHPCfg\Parser->parseNode(Object(PhpParser\Node\Stmt\Function_)) #5 .../vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php(100): PHPCfg\Parser-> in .../vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php on line 799

more7dev avatar Jun 20 '18 11:06 more7dev

Hello @ircmaxell, @nikic I have commited a fix for nullable types & group use declarations https://github.com/eric-therond/php-cfg/commit/913cfdbfcaf78e6d220d6f65a50d3998fa7c22c4

Eric

eric-therond avatar Dec 21 '18 10:12 eric-therond

@ircmaxell, have you checked this fix I propose ?

Eric

eric-therond avatar Jul 18 '19 08:07 eric-therond

The only issue I see there is that it throws away the nullability. It makes it so it parses, but it also "forgets" that type is nullable.

Looking a bit deeper, I don't see how this error is possible. You're geting a call to parseExprNode via parseParameterList. In parseParameterList the only 2 calls are for the default block, and the variable name. Neither of which should have ?array in them. Can you try with the latest master? https://github.com/ircmaxell/php-cfg/blob/master/lib/PHPCfg/Parser.php#L1439

ircmaxell avatar Jul 18 '19 18:07 ircmaxell

I have tried with the latest 1.0 (there is a lot of error in the latest master ?) and it's Ok thank you

eric-therond avatar Jul 22 '19 08:07 eric-therond

My example on branch v1.0.

Created foo.php: <?php function foo(?array $a) { $a[] = 1; }

Then I run command: php demo.php foo.php

There is no errors and result contains:

Function foo(): mixed Block#1 Expr_Param declaredType: ?array name: LITERAL('a') result: Var#1<$a> Expr_ArrayDimFetch var: Var#1<$a> dim: NULL result: Var#2 Expr_Assign var: Var#2 expr: LITERAL(1) result: Var#3 Terminal_Return

So it looks ok.

The latest master is using syntax from php 7.4, so it`s unusable on any stable php release.

more7dev avatar Jul 22 '19 09:07 more7dev