From 44445e3af47705e0a9a9e2bd30da513fc687b999 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Wed, 19 Oct 2016 13:33:43 +0200 Subject: [PATCH] Remove old definition/references after reparse (#88) --- src/PhpDocument.php | 12 ++++++++++++ src/Project.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/PhpDocument.php b/src/PhpDocument.php index e024461..c208ae4 100644 --- a/src/PhpDocument.php +++ b/src/PhpDocument.php @@ -177,12 +177,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 5e3b5d1..2cb0162 100644 --- a/src/Project.php +++ b/src/Project.php @@ -159,6 +159,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 * @@ -176,6 +188,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 *