X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fpsy%2Fpsysh%2Fsrc%2FCodeCleaner%2FValidFunctionNamePass.php;fp=vendor%2Fpsy%2Fpsysh%2Fsrc%2FCodeCleaner%2FValidFunctionNamePass.php;h=a6bf01e722aa857ce1d5c281f21ce9649b9773f6;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/vendor/psy/psysh/src/CodeCleaner/ValidFunctionNamePass.php b/vendor/psy/psysh/src/CodeCleaner/ValidFunctionNamePass.php new file mode 100644 index 000000000..a6bf01e72 --- /dev/null +++ b/vendor/psy/psysh/src/CodeCleaner/ValidFunctionNamePass.php @@ -0,0 +1,97 @@ +conditionalScopes++; + } elseif ($node instanceof Function_) { + $name = $this->getFullyQualifiedName($node->name); + + // @todo add an "else" here which adds a runtime check for instances where we can't tell + // whether a function is being redefined by static analysis alone. + if ($this->conditionalScopes === 0) { + if (function_exists($name) || + isset($this->currentScope[strtolower($name)])) { + $msg = sprintf('Cannot redeclare %s()', $name); + throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine()); + } + } + + $this->currentScope[strtolower($name)] = true; + } + } + + /** + * Validate that function calls will succeed. + * + * @throws FatalErrorException if a function is redefined + * @throws FatalErrorException if the function name is a string (not an expression) and is not defined + * + * @param Node $node + */ + public function leaveNode(Node $node) + { + if (self::isConditional($node)) { + $this->conditionalScopes--; + } elseif ($node instanceof FuncCall) { + // if function name is an expression or a variable, give it a pass for now. + $name = $node->name; + if (!$name instanceof Expr && !$name instanceof Variable) { + $shortName = implode('\\', $name->parts); + $fullName = $this->getFullyQualifiedName($name); + $inScope = isset($this->currentScope[strtolower($fullName)]); + if (!$inScope && !function_exists($shortName) && !function_exists($fullName)) { + $message = sprintf('Call to undefined function %s()', $name); + throw new FatalErrorException($message, 0, E_ERROR, null, $node->getLine()); + } + } + } + } + + private static function isConditional(Node $node) + { + return $node instanceof If_ || + $node instanceof While_ || + $node instanceof Do_ || + $node instanceof Switch_; + } +}