Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

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