Mercurial > hg > isophonics-drupal-site
comparison vendor/psy/psysh/src/Psy/CodeCleaner/NamespacePass.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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\CodeCleaner; | |
13 | |
14 use PhpParser\Node\Name; | |
15 use PhpParser\Node\Stmt\Namespace_; | |
16 use Psy\CodeCleaner; | |
17 | |
18 /** | |
19 * Provide implicit namespaces for subsequent execution. | |
20 * | |
21 * The namespace pass remembers the last standalone namespace line encountered: | |
22 * | |
23 * namespace Foo\Bar; | |
24 * | |
25 * ... which it then applies implicitly to all future evaluated code, until the | |
26 * namespace is replaced by another namespace. To reset to the top level | |
27 * namespace, enter `namespace {}`. This is a bit ugly, but it does the trick :) | |
28 */ | |
29 class NamespacePass extends CodeCleanerPass | |
30 { | |
31 private $namespace = null; | |
32 private $cleaner; | |
33 | |
34 /** | |
35 * @param CodeCleaner $cleaner | |
36 */ | |
37 public function __construct(CodeCleaner $cleaner) | |
38 { | |
39 $this->cleaner = $cleaner; | |
40 } | |
41 | |
42 /** | |
43 * If this is a standalone namespace line, remember it for later. | |
44 * | |
45 * Otherwise, apply remembered namespaces to the code until a new namespace | |
46 * is encountered. | |
47 * | |
48 * @param array $nodes | |
49 */ | |
50 public function beforeTraverse(array $nodes) | |
51 { | |
52 if (empty($nodes)) { | |
53 return $nodes; | |
54 } | |
55 | |
56 $last = end($nodes); | |
57 if (!$last instanceof Namespace_) { | |
58 return $this->namespace ? array(new Namespace_($this->namespace, $nodes)) : $nodes; | |
59 } | |
60 | |
61 $this->setNamespace($last->name); | |
62 | |
63 return $nodes; | |
64 } | |
65 | |
66 /** | |
67 * Remember the namespace and (re)set the namespace on the CodeCleaner as | |
68 * well. | |
69 * | |
70 * @param null|Name $namespace | |
71 */ | |
72 private function setNamespace($namespace) | |
73 { | |
74 $this->namespace = $namespace; | |
75 $this->cleaner->setNamespace($namespace === null ? null : $namespace->parts); | |
76 } | |
77 } |