/** * @param string $fqname * * @return Fix */ private function getFix($fqname) { if ($this->insertRange === null) { $namespace = null; foreach (array_reverse($this->nodePathFromTop) as $ancestor) { if ($ancestor instanceof Stmt\Namespace_) { $namespace = $ancestor; break; } } $lastUse = null; $stmts = $namespace === null ? $this->parser->getNodes() : $namespace->stmts; foreach ($stmts as $stmt) { if ($stmt instanceof Stmt\Use_ || $stmt instanceof Stmt\GroupUse) { $lastUse = $stmt; } } if ($lastUse !== null) { /** @var Range */ $range = Range::fromNode($lastUse, $this->file->getPath()); $startLine = $range->getStart()->getLineAndColumn($this->file)[0]; $insertLocation = OffsetLocation::move($this->file, $range->getEnd()); $indent = $this->fixHelper->getIndentOfLines([$this->file->getLine($startLine)]); $this->insertText = "\n" . $this->fixHelper->makeIndent($indent) . "use %s;"; } else { $insertLocation = LineAndColumnLocation::moveToStartOfLine($this->file, Range::fromNode($stmts[0], $this->file->getPath())->getStart()); $this->insertText = "use %s;\n\n"; } $this->insertRange = new Range($insertLocation, OffsetLocation::move($this->file, $insertLocation, -1)); } $fqname = ltrim($fqname, '\\'); return new Fix([new FixChunk($this->insertRange, sprintf($this->insertText, $fqname))], 'use ' . $fqname); }