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.
12 namespace Psy\CodeCleaner;
15 use PhpParser\Node\Name;
16 use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
17 use PhpParser\Node\Stmt\Namespace_;
20 * Abstract namespace-aware code cleaner pass.
22 abstract class NamespaceAwarePass extends CodeCleanerPass
25 protected $currentScope;
28 * @todo should this be final? Extending classes should be sure to either
29 * use afterTraverse or call parent::beforeTraverse() when overloading.
31 * Reset the namespace and the current scope before beginning analysis
33 public function beforeTraverse(array $nodes)
35 $this->namespace = array();
36 $this->currentScope = array();
40 * @todo should this be final? Extending classes should be sure to either use
41 * leaveNode or call parent::enterNode() when overloading
45 public function enterNode(Node $node)
47 if ($node instanceof Namespace_) {
48 $this->namespace = isset($node->name) ? $node->name->parts : array();
53 * Get a fully-qualified name (class, function, interface, etc).
59 protected function getFullyQualifiedName($name)
61 if ($name instanceof FullyQualifiedName) {
62 return implode('\\', $name->parts);
63 } elseif ($name instanceof Name) {
65 } elseif (!is_array($name)) {
69 return implode('\\', array_merge($this->namespace, $name));