4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
15 use PhpParser\Node\Arg;
16 use PhpParser\Node\Expr\Assign;
17 use PhpParser\Node\Expr\ClassConstFetch;
18 use PhpParser\Node\Expr\MethodCall;
19 use PhpParser\Node\Expr\PropertyFetch;
20 use PhpParser\Node\Expr\StaticCall;
21 use PhpParser\Node\Expr\StaticPropertyFetch;
22 use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
23 use PhpParser\Node\Scalar\String_;
24 use PhpParser\NodeVisitorAbstract;
27 * A PHP Parser node visitor which rewrites property and method access to use
28 * the Psy\Sudo visibility bypass methods.
30 * @todo handle assigning by reference
32 class SudoVisitor extends NodeVisitorAbstract
34 const SUDO_CLASS = 'Psy\Sudo';
36 const PROPERTY_FETCH = 'fetchProperty';
37 const PROPERTY_ASSIGN = 'assignProperty';
38 const METHOD_CALL = 'callMethod';
39 const STATIC_PROPERTY_FETCH = 'fetchStaticProperty';
40 const STATIC_PROPERTY_ASSIGN = 'assignStaticProperty';
41 const STATIC_CALL = 'callStatic';
42 const CLASS_CONST_FETCH = 'fetchClassConst';
47 public function enterNode(Node $node)
49 if ($node instanceof PropertyFetch) {
52 is_string($node->name) ? new String_($node->name) : $node->name,
55 return $this->prepareCall(self::PROPERTY_FETCH, $args);
56 } elseif ($node instanceof Assign && $node->var instanceof PropertyFetch) {
60 is_string($target->name) ? new String_($target->name) : $target->name,
64 return $this->prepareCall(self::PROPERTY_ASSIGN, $args);
65 } elseif ($node instanceof MethodCall) {
67 array_unshift($args, new Arg(is_string($node->name) ? new String_($node->name) : $node->name));
68 array_unshift($args, new Arg($node->var));
70 // not using prepareCall because the $node->args we started with are already Arg instances
71 return new StaticCall(new FullyQualifiedName(self::SUDO_CLASS), self::METHOD_CALL, $args);
72 } elseif ($node instanceof StaticPropertyFetch) {
73 $class = $node->class instanceof Name ? (string) $node->class : $node->class;
75 is_string($class) ? new String_($class) : $class,
76 is_string($node->name) ? new String_($node->name) : $node->name,
79 return $this->prepareCall(self::STATIC_PROPERTY_FETCH, $args);
80 } elseif ($node instanceof Assign && $node->var instanceof StaticPropertyFetch) {
82 $class = $target->class instanceof Name ? (string) $target->class : $target->class;
84 is_string($class) ? new String_($class) : $class,
85 is_string($target->name) ? new String_($target->name) : $target->name,
89 return $this->prepareCall(self::STATIC_PROPERTY_ASSIGN, $args);
90 } elseif ($node instanceof StaticCall) {
92 $class = $node->class instanceof Name ? (string) $node->class : $node->class;
93 array_unshift($args, new Arg(is_string($node->name) ? new String_($node->name) : $node->name));
94 array_unshift($args, new Arg(is_string($class) ? new String_($class) : $class));
96 // not using prepareCall because the $node->args we started with are already Arg instances
97 return new StaticCall(new FullyQualifiedName(self::SUDO_CLASS), self::STATIC_CALL, $args);
98 } elseif ($node instanceof ClassConstFetch) {
99 $class = $node->class instanceof Name ? (string) $node->class : $node->class;
101 is_string($class) ? new String_($class) : $class,
102 is_string($node->name) ? new String_($node->name) : $node->name,
105 return $this->prepareCall(self::CLASS_CONST_FETCH, $args);
109 private function prepareCall($method, $args)
111 return new StaticCall(new FullyQualifiedName(self::SUDO_CLASS), $method, array_map(function ($arg) {
112 return new Arg($arg);