ircmaxell/php-cfg

Assertion fail when returning an anonymous function.

Opened this issue · 6 comments

In the following code example, an anonymous function is returned, acting like a closure. However, the Printer class expects a function name.

function generateComparisonFunctionForKey($key) {
        return function ($left, $right) use ($key) {
            if ($left[$key] == $right[$key])
                return 0;
            else
                return ($left[$key] < $right[$key]) ? -1 : 1;
        };
    }

Error:

Warning: assert(): Assertion failed in /Users/<redacted>/php-cfg/lib/PHPCfg/Printer.php on line 46
nikic commented

The assertion error is fixed by d43ed9f.

However the way we handle bound closure variables currently is not good (and for the reference case outright violates SSA). Closure body also shouldn't be part of the same CFG really...

Yea, seems like an issue I'll face later.

nikic commented

Follow-up fix: 8e85ad3

I didn't have notices enabled locally, so missed that part.

nikic commented

In 0d54187 I've introduced a Func structure, so each function has a clearly separated CFG. The Funcs are collected in a Script, which is what is returned by the parser now.

Closures still need extra work for the use variable handling.

Does GraphViz still work?

nikic commented

GraphViz printer fixed in 1b0e384. Also added a printScript method to print all functions into one graph.