Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of Psy Shell.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) 2012-2017 Justin Hileman
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Psy\CodeCleaner;
|
Chris@0
|
13
|
Chris@0
|
14 use PhpParser\Node;
|
Chris@0
|
15 use PhpParser\Node\Name;
|
Chris@0
|
16 use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
|
Chris@0
|
17 use PhpParser\Node\Stmt\Namespace_;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Abstract namespace-aware code cleaner pass.
|
Chris@0
|
21 */
|
Chris@0
|
22 abstract class NamespaceAwarePass extends CodeCleanerPass
|
Chris@0
|
23 {
|
Chris@0
|
24 protected $namespace;
|
Chris@0
|
25 protected $currentScope;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * @todo should this be final? Extending classes should be sure to either
|
Chris@0
|
29 * use afterTraverse or call parent::beforeTraverse() when overloading.
|
Chris@0
|
30 *
|
Chris@0
|
31 * Reset the namespace and the current scope before beginning analysis
|
Chris@0
|
32 */
|
Chris@0
|
33 public function beforeTraverse(array $nodes)
|
Chris@0
|
34 {
|
Chris@0
|
35 $this->namespace = array();
|
Chris@0
|
36 $this->currentScope = array();
|
Chris@0
|
37 }
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * @todo should this be final? Extending classes should be sure to either use
|
Chris@0
|
41 * leaveNode or call parent::enterNode() when overloading
|
Chris@0
|
42 *
|
Chris@0
|
43 * @param Node $node
|
Chris@0
|
44 */
|
Chris@0
|
45 public function enterNode(Node $node)
|
Chris@0
|
46 {
|
Chris@0
|
47 if ($node instanceof Namespace_) {
|
Chris@0
|
48 $this->namespace = isset($node->name) ? $node->name->parts : array();
|
Chris@0
|
49 }
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Get a fully-qualified name (class, function, interface, etc).
|
Chris@0
|
54 *
|
Chris@0
|
55 * @param mixed $name
|
Chris@0
|
56 *
|
Chris@0
|
57 * @return string
|
Chris@0
|
58 */
|
Chris@0
|
59 protected function getFullyQualifiedName($name)
|
Chris@0
|
60 {
|
Chris@0
|
61 if ($name instanceof FullyQualifiedName) {
|
Chris@0
|
62 return implode('\\', $name->parts);
|
Chris@0
|
63 } elseif ($name instanceof Name) {
|
Chris@0
|
64 $name = $name->parts;
|
Chris@0
|
65 } elseif (!is_array($name)) {
|
Chris@0
|
66 $name = array($name);
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 return implode('\\', array_merge($this->namespace, $name));
|
Chris@0
|
70 }
|
Chris@0
|
71 }
|