Chris@13: cleaner = $cleaner; Chris@13: } Chris@13: Chris@13: /** Chris@13: * If this is a standalone namespace line, remember it for later. Chris@13: * Chris@13: * Otherwise, apply remembered namespaces to the code until a new namespace Chris@13: * is encountered. Chris@13: * Chris@13: * @param array $nodes Chris@13: */ Chris@13: public function beforeTraverse(array $nodes) Chris@13: { Chris@13: if (empty($nodes)) { Chris@13: return $nodes; Chris@13: } Chris@13: Chris@17: $last = \end($nodes); Chris@13: Chris@13: if ($last instanceof Namespace_) { Chris@13: $kind = $last->getAttribute('kind'); Chris@13: Chris@13: // Treat all namespace statements pre-PHP-Parser v3.1.2 as "open", Chris@13: // even though we really have no way of knowing. Chris@13: if ($kind === null || $kind === Namespace_::KIND_SEMICOLON) { Chris@13: // Save the current namespace for open namespaces Chris@13: $this->setNamespace($last->name); Chris@13: } else { Chris@13: // Clear the current namespace after a braced namespace Chris@13: $this->setNamespace(null); Chris@13: } Chris@13: Chris@13: return $nodes; Chris@13: } Chris@13: Chris@13: return $this->namespace ? [new Namespace_($this->namespace, $nodes)] : $nodes; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Remember the namespace and (re)set the namespace on the CodeCleaner as Chris@13: * well. Chris@13: * Chris@13: * @param null|Name $namespace Chris@13: */ Chris@13: private function setNamespace($namespace) Chris@13: { Chris@13: $this->namespace = $namespace; Chris@13: $this->cleaner->setNamespace($namespace === null ? null : $namespace->parts); Chris@13: } Chris@13: }