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