annotate vendor/psy/psysh/src/Psy/CodeCleaner/ValidFunctionNamePass.php @ 7:848c88cfe644

More layout
author Chris Cannam
date Fri, 05 Jan 2018 13:59:44 +0000
parents 4c8ae668cc8c
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2017 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\CodeCleaner;
Chris@0 13
Chris@0 14 use PhpParser\Node;
Chris@0 15 use PhpParser\Node\Expr;
Chris@0 16 use PhpParser\Node\Expr\FuncCall;
Chris@0 17 use PhpParser\Node\Expr\Variable;
Chris@0 18 use PhpParser\Node\Stmt\Do_;
Chris@0 19 use PhpParser\Node\Stmt\Function_;
Chris@0 20 use PhpParser\Node\Stmt\If_;
Chris@0 21 use PhpParser\Node\Stmt\Switch_;
Chris@0 22 use PhpParser\Node\Stmt\While_;
Chris@0 23 use Psy\Exception\FatalErrorException;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Validate that function calls will succeed.
Chris@0 27 *
Chris@0 28 * This pass throws a FatalErrorException rather than letting PHP run
Chris@0 29 * headfirst into a real fatal error and die.
Chris@0 30 */
Chris@0 31 class ValidFunctionNamePass extends NamespaceAwarePass
Chris@0 32 {
Chris@0 33 private $conditionalScopes = 0;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Store newly defined function names on the way in, to allow recursion.
Chris@0 37 *
Chris@0 38 * @param Node $node
Chris@0 39 */
Chris@0 40 public function enterNode(Node $node)
Chris@0 41 {
Chris@0 42 parent::enterNode($node);
Chris@0 43
Chris@0 44 if (self::isConditional($node)) {
Chris@0 45 $this->conditionalScopes++;
Chris@0 46 } elseif ($node instanceof Function_) {
Chris@0 47 $name = $this->getFullyQualifiedName($node->name);
Chris@0 48
Chris@0 49 // @todo add an "else" here which adds a runtime check for instances where we can't tell
Chris@0 50 // whether a function is being redefined by static analysis alone.
Chris@0 51 if ($this->conditionalScopes === 0) {
Chris@0 52 if (function_exists($name) ||
Chris@0 53 isset($this->currentScope[strtolower($name)])) {
Chris@0 54 $msg = sprintf('Cannot redeclare %s()', $name);
Chris@0 55 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
Chris@0 56 }
Chris@0 57 }
Chris@0 58
Chris@0 59 $this->currentScope[strtolower($name)] = true;
Chris@0 60 }
Chris@0 61 }
Chris@0 62
Chris@0 63 /**
Chris@0 64 * Validate that function calls will succeed.
Chris@0 65 *
Chris@0 66 * @throws FatalErrorException if a function is redefined
Chris@0 67 * @throws FatalErrorException if the function name is a string (not an expression) and is not defined
Chris@0 68 *
Chris@0 69 * @param Node $node
Chris@0 70 */
Chris@0 71 public function leaveNode(Node $node)
Chris@0 72 {
Chris@0 73 if (self::isConditional($node)) {
Chris@0 74 $this->conditionalScopes--;
Chris@0 75 } elseif ($node instanceof FuncCall) {
Chris@0 76 // if function name is an expression or a variable, give it a pass for now.
Chris@0 77 $name = $node->name;
Chris@0 78 if (!$name instanceof Expr && !$name instanceof Variable) {
Chris@0 79 $shortName = implode('\\', $name->parts);
Chris@0 80 $fullName = $this->getFullyQualifiedName($name);
Chris@0 81 $inScope = isset($this->currentScope[strtolower($fullName)]);
Chris@0 82 if (!$inScope && !function_exists($shortName) && !function_exists($fullName)) {
Chris@0 83 $message = sprintf('Call to undefined function %s()', $name);
Chris@0 84 throw new FatalErrorException($message, 0, E_ERROR, null, $node->getLine());
Chris@0 85 }
Chris@0 86 }
Chris@0 87 }
Chris@0 88 }
Chris@0 89
Chris@0 90 private static function isConditional(Node $node)
Chris@0 91 {
Chris@0 92 return $node instanceof If_ ||
Chris@0 93 $node instanceof While_ ||
Chris@0 94 $node instanceof Do_ ||
Chris@0 95 $node instanceof Switch_;
Chris@0 96 }
Chris@0 97 }