31fe6bfb08d598397a1eccd931e008236faae4a5
[yaffs-website] / vendor / psy / psysh / src / Sudo / SudoVisitor.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Sudo;
13
14 use PhpParser\Node;
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\Identifier;
23 use PhpParser\Node\Name;
24 use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
25 use PhpParser\Node\Scalar\String_;
26 use PhpParser\NodeVisitorAbstract;
27
28 /**
29  * A PHP Parser node visitor which rewrites property and method access to use
30  * the Psy\Sudo visibility bypass methods.
31  *
32  * @todo handle assigning by reference
33  */
34 class SudoVisitor extends NodeVisitorAbstract
35 {
36     const SUDO_CLASS = 'Psy\Sudo';
37
38     const PROPERTY_FETCH         = 'fetchProperty';
39     const PROPERTY_ASSIGN        = 'assignProperty';
40     const METHOD_CALL            = 'callMethod';
41     const STATIC_PROPERTY_FETCH  = 'fetchStaticProperty';
42     const STATIC_PROPERTY_ASSIGN = 'assignStaticProperty';
43     const STATIC_CALL            = 'callStatic';
44     const CLASS_CONST_FETCH      = 'fetchClassConst';
45
46     /**
47      * {@inheritdoc}
48      */
49     public function enterNode(Node $node)
50     {
51         if ($node instanceof PropertyFetch) {
52             $name = $node->name instanceof Identifier ? $node->name->toString() : $node->name;
53             $args = [
54                 $node->var,
55                 is_string($name) ? new String_($name) : $name,
56             ];
57
58             return $this->prepareCall(self::PROPERTY_FETCH, $args);
59         } elseif ($node instanceof Assign && $node->var instanceof PropertyFetch) {
60             $target = $node->var;
61             $name   = $target->name instanceof Identifier ? $target->name->toString() : $target->name;
62             $args   = [
63                 $target->var,
64                 is_string($name) ? new String_($name) : $name,
65                 $node->expr,
66             ];
67
68             return $this->prepareCall(self::PROPERTY_ASSIGN, $args);
69         } elseif ($node instanceof MethodCall) {
70             $name = $node->name instanceof Identifier ? $node->name->toString() : $node->name;
71             $args = $node->args;
72             array_unshift($args, new Arg(is_string($name) ? new String_($name) : $name));
73             array_unshift($args, new Arg($node->var));
74
75             // not using prepareCall because the $node->args we started with are already Arg instances
76             return new StaticCall(new FullyQualifiedName(self::SUDO_CLASS), self::METHOD_CALL, $args);
77         } elseif ($node instanceof StaticPropertyFetch) {
78             $class = $node->class instanceof Name ? $node->class->toString() : $node->class;
79             $name = $node->name instanceof Identifier ? $node->name->toString() : $node->name;
80             $args  = [
81                 is_string($class) ? new String_($class) : $class,
82                 is_string($name) ? new String_($name) : $name,
83             ];
84
85             return $this->prepareCall(self::STATIC_PROPERTY_FETCH, $args);
86         } elseif ($node instanceof Assign && $node->var instanceof StaticPropertyFetch) {
87             $target = $node->var;
88             $class  = $target->class instanceof Name ? $target->class->toString() : $target->class;
89             $name   = $target->name instanceof Identifier ? $target->name->toString() : $target->name;
90             $args   = [
91                 is_string($class) ? new String_($class) : $class,
92                 is_string($name) ? new String_($name) : $name,
93                 $node->expr,
94             ];
95
96             return $this->prepareCall(self::STATIC_PROPERTY_ASSIGN, $args);
97         } elseif ($node instanceof StaticCall) {
98             $args  = $node->args;
99             $class = $node->class instanceof Name ? $node->class->toString() : $node->class;
100             $name  = $node->name instanceof Identifier ? $node->name->toString() : $node->name;
101             array_unshift($args, new Arg(is_string($name) ? new String_($name) : $name));
102             array_unshift($args, new Arg(is_string($class) ? new String_($class) : $class));
103
104             // not using prepareCall because the $node->args we started with are already Arg instances
105             return new StaticCall(new FullyQualifiedName(self::SUDO_CLASS), self::STATIC_CALL, $args);
106         } elseif ($node instanceof ClassConstFetch) {
107             $class = $node->class instanceof Name ? $node->class->toString() : $node->class;
108             $name  = $node->name instanceof Identifier ? $node->name->toString() : $node->name;
109             $args  = [
110                 is_string($class) ? new String_($class) : $class,
111                 is_string($name) ? new String_($name) : $name,
112             ];
113
114             return $this->prepareCall(self::CLASS_CONST_FETCH, $args);
115         }
116     }
117
118     private function prepareCall($method, $args)
119     {
120         return new StaticCall(new FullyQualifiedName(self::SUDO_CLASS), $method, array_map(function ($arg) {
121             return new Arg($arg);
122         }, $args));
123     }
124 }