From bc07c199578e6b13ed3e11aea4eb909d2331709d Mon Sep 17 00:00:00 2001 From: Robert Lu Date: Sat, 25 May 2019 13:46:47 +0800 Subject: [PATCH] use Ds\Set --- composer.json | 1 + src/Index/Index.php | 54 +++++++++++++++++++++------------------------ 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/composer.json b/composer.json index f63bf3a..a57f98b 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "jetbrains/phpstorm-stubs": "dev-master", "microsoft/tolerant-php-parser": "0.0.*", "netresearch/jsonmapper": "^1.0", + "php-ds/php-ds": "^1.2", "phpdocumentor/reflection-docblock": "^4.0.0", "psr/log": "^1.0", "sabre/event": "^5.0", diff --git a/src/Index/Index.php b/src/Index/Index.php index 0d61f9c..351ac02 100644 --- a/src/Index/Index.php +++ b/src/Index/Index.php @@ -1,8 +1,9 @@ $item; + yield $fqn . $name => $item; } elseif (is_array($item) && isset($item[''])) { - yield $fqn.$name => $item['']; + yield $fqn . $name => $item['']; } } } @@ -200,8 +201,10 @@ class Index implements ReadableIndex, \Serializable */ public function getReferenceUris(string $fqn): \Generator { - foreach ($this->references[$fqn] ?? [] as $uri) { - yield $uri; + if ($this->references[$fqn]) { + foreach ($this->references[$fqn] as $uri) { + yield $uri; + } } } @@ -209,7 +212,7 @@ class Index implements ReadableIndex, \Serializable * For test use. * Returns all references, keyed by fqn. * - * @return string[][] + * @return Set[] */ public function getReferences(): array { @@ -225,12 +228,9 @@ class Index implements ReadableIndex, \Serializable public function addReferenceUri(string $fqn, string $uri) { if (!isset($this->references[$fqn])) { - $this->references[$fqn] = []; - } - // TODO: use DS\Set instead of searching array - if (array_search($uri, $this->references[$fqn], true) === false) { - $this->references[$fqn][] = $uri; + $this->references[$fqn] = new Set(); } + $this->references[$fqn]->add($uri); } /** @@ -245,11 +245,7 @@ class Index implements ReadableIndex, \Serializable 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); + $this->references[$fqn]->remove($uri); } /** @@ -299,9 +295,9 @@ class Index implements ReadableIndex, \Serializable { foreach ($storage as $key => $value) { if (!is_array($value)) { - yield $prefix.$key => $value; + yield $prefix . $key => $value; } else { - yield from $this->yieldDefinitionsRecursively($value, $prefix.$key); + yield from $this->yieldDefinitionsRecursively($value, $prefix . $key); } } } @@ -365,8 +361,8 @@ class Index implements ReadableIndex, \Serializable * It can be an index node or a Definition if the $parts are precise * enough. Returns null when nothing is found. * - * @param string[] $path The splitted FQN - * @param array|Definition &$storage The current level to look for $path. + * @param string[] $path The splitted FQN + * @param array|Definition &$storage The current level to look for $path. * @return array|Definition|null */ private function getIndexValue(array $path, &$storage) @@ -389,10 +385,10 @@ class Index implements ReadableIndex, \Serializable * Recursive function that stores the given Definition in the given $storage array represented * as a tree matching the given $parts. * - * @param int $level The current level of FQN part - * @param string[] $parts The splitted FQN - * @param array &$storage The array in which to store the $definition - * @param Definition $definition The Definition to store + * @param int $level The current level of FQN part + * @param string[] $parts The splitted FQN + * @param array &$storage The array in which to store the $definition + * @param Definition $definition The Definition to store */ private function indexDefinition(int $level, array $parts, array &$storage, Definition $definition) { @@ -416,10 +412,10 @@ class Index implements ReadableIndex, \Serializable * $storage array. The function also looks up recursively to remove the parents of the * definition which no longer has children to avoid to let empty arrays in the index. * - * @param int $level The current level of FQN part - * @param string[] $parts The splitted FQN - * @param array &$storage The current array in which to remove data - * @param array &$rootStorage The root storage array + * @param int $level The current level of FQN part + * @param string[] $parts The splitted FQN + * @param array &$storage The current array in which to remove data + * @param array &$rootStorage The root storage array */ private function removeIndexedDefinition(int $level, array $parts, array &$storage, array &$rootStorage) {