Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / Sudo / SudoVisitor.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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\Name\FullyQualified as FullyQualifiedName;
23 use PhpParser\Node\Scalar\String_;
24 use PhpParser\NodeVisitorAbstract;
25
26 /**
27  * A PHP Parser node visitor which rewrites property and method access to use
28  * the Psy\Sudo visibility bypass methods.
29  *
30  * @todo handle assigning by reference
31  */
32 class SudoVisitor extends NodeVisitorAbstract
33 {
34     const SUDO_CLASS = 'Psy\Sudo';
35
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';
43
44     /**
45      * {@inheritdoc}
46      */
47     public function enterNode(Node $node)
48     {
49         if ($node instanceof PropertyFetch) {
50             $args = array(
51                 $node->var,
52                 is_string($node->name) ? new String_($node->name) : $node->name,
53             );
54
55             return $this->prepareCall(self::PROPERTY_FETCH, $args);
56         } elseif ($node instanceof Assign && $node->var instanceof PropertyFetch) {
57             $target = $node->var;
58             $args = array(
59                 $target->var,
60                 is_string($target->name) ? new String_($target->name) : $target->name,
61                 $node->expr,
62             );
63
64             return $this->prepareCall(self::PROPERTY_ASSIGN, $args);
65         } elseif ($node instanceof MethodCall) {
66             $args = $node->args;
67             array_unshift($args, new Arg(is_string($node->name) ? new String_($node->name) : $node->name));
68             array_unshift($args, new Arg($node->var));
69
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;
74             $args = array(
75                 is_string($class) ? new String_($class) : $class,
76                 is_string($node->name) ? new String_($node->name) : $node->name,
77             );
78
79             return $this->prepareCall(self::STATIC_PROPERTY_FETCH, $args);
80         } elseif ($node instanceof Assign && $node->var instanceof StaticPropertyFetch) {
81             $target = $node->var;
82             $class = $target->class instanceof Name ? (string) $target->class : $target->class;
83             $args = array(
84                 is_string($class) ? new String_($class) : $class,
85                 is_string($target->name) ? new String_($target->name) : $target->name,
86                 $node->expr,
87             );
88
89             return $this->prepareCall(self::STATIC_PROPERTY_ASSIGN, $args);
90         } elseif ($node instanceof StaticCall) {
91             $args = $node->args;
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));
95
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;
100             $args = array(
101                 is_string($class) ? new String_($class) : $class,
102                 is_string($node->name) ? new String_($node->name) : $node->name,
103             );
104
105             return $this->prepareCall(self::CLASS_CONST_FETCH, $args);
106         }
107     }
108
109     private function prepareCall($method, $args)
110     {
111         return new StaticCall(new FullyQualifiedName(self::SUDO_CLASS), $method, array_map(function ($arg) {
112             return new Arg($arg);
113         }, $args));
114     }
115 }