From f95ebceb02f904879b5c01987a5c5f05ca478dbd Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Tue, 18 Oct 2016 11:14:42 +0200 Subject: [PATCH] Remove old definition/references after reparse --- src/PhpDocument.php | 12 ++++++++++++ src/Project.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/PhpDocument.php b/src/PhpDocument.php index fe9a1a2..e5436db 100644 --- a/src/PhpDocument.php +++ b/src/PhpDocument.php @@ -167,12 +167,24 @@ class PhpDocument $traverser->traverse($stmts); + // Unregister old definitions + if (isset($this->definitions)) { + foreach ($this->definitions as $fqn => $node) { + $this->project->removeDefinition($fqn); + } + } // Register this document on the project for all the symbols defined in it $this->definitions = $definitionCollector->definitions; foreach ($definitionCollector->definitions as $fqn => $node) { $this->project->setDefinitionUri($fqn, $this->uri); } + // Unregister old references + if (isset($this->references)) { + foreach ($this->references as $fqn => $node) { + $this->project->removeReferenceUri($fqn, $this->uri); + } + } // Register this document on the project for references $this->references = $referencesCollector->references; foreach ($referencesCollector->references as $fqn => $nodes) { diff --git a/src/Project.php b/src/Project.php index 06b3a08..00d1fc2 100644 --- a/src/Project.php +++ b/src/Project.php @@ -150,6 +150,18 @@ class Project $this->definitions[$fqn] = $uri; } + /** + * Unsets a document URI as the container for a specific symbol + * and removes all references pointing to that symbol + * + * @param string $fqn The fully qualified name of the symbol + * @return void + */ + public function removeDefinition(string $fqn) { + unset($this->definitions[$fqn]); + unset($this->references[$fqn]); + } + /** * Adds a document URI as a referencee of a specific symbol * @@ -167,6 +179,24 @@ class Project } } + /** + * Removes a document URI as the container for a specific symbol + * + * @param string $fqn The fully qualified name of the symbol + * @param string $uri The URI + * @return void + */ + public function removeReferenceUri(string $fqn, string $uri) { + if (!isset($this->references[$fqn])) { + return; + } + $index = array_search($fqn, $this->references[$fqn], true); + if ($index === false) { + return; + } + array_splice($this->references[$fqn], $index, 1); + } + /** * Returns all documents that reference a symbol *