|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Go! AOP framework |
| 4 | + * |
| 5 | + * @copyright Copyright 2018, Lisachenko Alexander <[email protected]> |
| 6 | + * |
| 7 | + * This source file is subject to the license that is bundled |
| 8 | + * with this source code in the file LICENSE. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Go\Instrument\Transformer; |
| 12 | + |
| 13 | +use PhpParser\Node; |
| 14 | +use PhpParser\Node\Name; |
| 15 | +use PhpParser\Node\Name\FullyQualified; |
| 16 | +use PhpParser\Node\Stmt; |
| 17 | +use PhpParser\Node\Expr; |
| 18 | +use PhpParser\NodeVisitorAbstract; |
| 19 | + |
| 20 | +/** |
| 21 | + * Node visitor that resolves class name for `self` nodes with FQN |
| 22 | + */ |
| 23 | +final class SelfValueVisitor extends NodeVisitorAbstract |
| 24 | +{ |
| 25 | + /** |
| 26 | + * List of replaced nodes |
| 27 | + * |
| 28 | + * @var Node[] |
| 29 | + */ |
| 30 | + protected $replacedNodes = []; |
| 31 | + |
| 32 | + /** |
| 33 | + * Current namespace |
| 34 | + * |
| 35 | + * @var null|Name|string |
| 36 | + */ |
| 37 | + protected $namespace; |
| 38 | + |
| 39 | + /** |
| 40 | + * Current class name |
| 41 | + * |
| 42 | + * @var null|Name |
| 43 | + */ |
| 44 | + protected $className; |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns list of changed `self` nodes |
| 48 | + * |
| 49 | + * @return Node[] |
| 50 | + */ |
| 51 | + public function getReplacedNodes() |
| 52 | + { |
| 53 | + return $this->replacedNodes; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @inheritDoc |
| 58 | + */ |
| 59 | + public function beforeTraverse(array $nodes) |
| 60 | + { |
| 61 | + $this->namespace = null; |
| 62 | + $this->className = null; |
| 63 | + $this->replacedNodes = []; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @inheritDoc |
| 68 | + */ |
| 69 | + public function enterNode(Node $node) |
| 70 | + { |
| 71 | + if ($node instanceof Stmt\Namespace_) { |
| 72 | + $this->namespace = $node->name; |
| 73 | + } elseif ($node instanceof Stmt\Class_) { |
| 74 | + if ($node->name !== null) { |
| 75 | + $this->className = new Name($node->name); |
| 76 | + } |
| 77 | + } elseif ($node instanceof Stmt\ClassMethod || $node instanceof Expr\Closure) { |
| 78 | + $node->returnType = $this->resolveType($node->returnType); |
| 79 | + } elseif ($node instanceof Node\Param) { |
| 80 | + $node->type = $this->resolveType($node->type); |
| 81 | + } elseif ( |
| 82 | + $node instanceof Expr\StaticCall |
| 83 | + || $node instanceof Expr\ClassConstFetch |
| 84 | + || $node instanceof Expr\New_ |
| 85 | + || $node instanceof Expr\Instanceof_ |
| 86 | + ) { |
| 87 | + if ($node->class instanceof Name) { |
| 88 | + $node->class = $this->resolveClassName($node->class); |
| 89 | + } |
| 90 | + } elseif ($node instanceof Stmt\Catch_) { |
| 91 | + foreach ($node->types as &$type) { |
| 92 | + $type = $this->resolveClassName($type); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Resolves `self` class name with value |
| 99 | + * |
| 100 | + * @param Name $name Instance of original node |
| 101 | + * |
| 102 | + * @return Name|FullyQualified |
| 103 | + */ |
| 104 | + protected function resolveClassName(Name $name) |
| 105 | + { |
| 106 | + // Skip all names except special `self` |
| 107 | + if (strtolower($name->toString()) !== 'self') { |
| 108 | + return $name; |
| 109 | + } |
| 110 | + |
| 111 | + // Save the original name |
| 112 | + $originalName = $name; |
| 113 | + $name = clone $originalName; |
| 114 | + $name->setAttribute('originalName', $originalName); |
| 115 | + |
| 116 | + $fullClassName = Name::concat($this->namespace, $this->className); |
| 117 | + $resolvedSelfName = new FullyQualified('\\' . $fullClassName->toString(), $name->getAttributes()); |
| 118 | + |
| 119 | + $this->replacedNodes[] = $resolvedSelfName; |
| 120 | + |
| 121 | + return $resolvedSelfName; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Helper method for resolving type nodes |
| 126 | + * |
| 127 | + * @param Node|string|null $node Instance of node |
| 128 | + * |
| 129 | + * @return Node|Name|FullyQualified |
| 130 | + */ |
| 131 | + private function resolveType($node) |
| 132 | + { |
| 133 | + if ($node instanceof Node\NullableType) { |
| 134 | + $node->type = $this->resolveType($node->type); |
| 135 | + return $node; |
| 136 | + } |
| 137 | + if ($node instanceof Name) { |
| 138 | + return $this->resolveClassName($node); |
| 139 | + } |
| 140 | + |
| 141 | + return $node; |
| 142 | + } |
| 143 | +} |
0 commit comments