Assertion fail when returning an anonymous function.
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
The assertion error is fixed by https://github.com/ircmaxell/php-cfg/commit/d43ed9f52f320cabf025a82b622bae9ecc48e483.
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.
Follow-up fix: https://github.com/ircmaxell/php-cfg/commit/8e85ad3fb7b79b345cb49cceeb6bd982d83ea421
I didn't have notices enabled locally, so missed that part.
In https://github.com/ircmaxell/php-cfg/commit/0d54187759ac945e4cf02d8c8dff82fb121192e0 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?
GraphViz printer fixed in https://github.com/ircmaxell/php-cfg/commit/1b0e384103c5484ee1778514fe89888c908309c5. Also added a printScript method to print all functions into one graph.