Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lack of support for nullable types #49

Open
more7dev opened this issue Jun 20, 2018 · 5 comments
Open

Lack of support for nullable types #49

more7dev opened this issue Jun 20, 2018 · 5 comments

Comments

@more7dev
Copy link

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

@eric-therond
Copy link
Contributor

Hello @ircmaxell, @nikic
I have commited a fix for nullable types & group use declarations
eric-therond@913cfdb

Eric

@eric-therond
Copy link
Contributor

@ircmaxell, have you checked this fix I propose ?

Eric

@ircmaxell
Copy link
Owner

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

@eric-therond
Copy link
Contributor

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

@more7dev
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants