From 4b573acde192dac226d7849a6a0ab8a080585e57 Mon Sep 17 00:00:00 2001 From: Rob Lourens Date: Fri, 9 Jun 2017 10:51:09 -0700 Subject: [PATCH] Inline strStartsWith and remove it --- src/CompletionProvider.php | 12 ++++++------ src/utils.php | 5 ----- tests/Utils/UtilsTest.php | 35 ----------------------------------- 3 files changed, 6 insertions(+), 46 deletions(-) delete mode 100644 tests/Utils/UtilsTest.php diff --git a/src/CompletionProvider.php b/src/CompletionProvider.php index 4752525..9299669 100644 --- a/src/CompletionProvider.php +++ b/src/CompletionProvider.php @@ -12,7 +12,6 @@ use LanguageServer\Protocol\{ CompletionItem, CompletionItemKind }; -use function LanguageServer\strStartsWith; use Microsoft\PhpParser; use Microsoft\PhpParser\Node; @@ -248,7 +247,7 @@ class CompletionProvider } foreach ($this->index->getDefinitions() as $fqn => $def) { - $fqnStartsWithPrefix = strStartsWith($fqn, $prefix); + $fqnStartsWithPrefix = substr($fqn, 0, strlen($prefix)) === $prefix; $fqnContainsPrefix = empty($prefix) || strpos($fqn, $prefix) !== false; if (($def->canBeInstantiated || ($def->isGlobal && !isset($creation))) && $fqnContainsPrefix) { if ($namespaceDefinition !== null && $namespaceDefinition->name !== null) { @@ -259,7 +258,7 @@ class CompletionProvider $isNotFullyQualified = !($class instanceof Node\QualifiedName) || !$class->isFullyQualifiedName(); if ($isNotFullyQualified) { foreach ($namespaceImportTable as $alias => $name) { - if (strStartsWith($fqn, $name)) { + if (substr($fqn, 0, strlen($name)) === $name) { $fqn = $alias; $isAliased = true; break; @@ -267,12 +266,13 @@ class CompletionProvider } } - - $isFullyQualifiedAndPrefixMatches = !$isNotFullyQualified && ($fqnStartsWithPrefix || strStartsWith($fqn, $namespacePrefix . "\\" . $prefix)); + $prefixWithNamespace = $namespacePrefix . "\\" . $prefix; + $fqnMatchesPrefixWithNamespace = substr($fqn, 0, strlen($prefixWithNamespace)) === $prefixWithNamespace; + $isFullyQualifiedAndPrefixMatches = !$isNotFullyQualified && ($fqnStartsWithPrefix || $fqnMatchesPrefixWithNamespace); if (!$isFullyQualifiedAndPrefixMatches && !$isAliased && !array_search($fqn, array_values($namespaceImportTable))) { if (empty($prefix)) { $fqn = '\\' . $fqn; - } elseif (strStartsWith($fqn, $namespacePrefix . "\\" . $prefix)) { + } elseif ($fqnMatchesPrefixWithNamespace) { $fqn = substr($fqn, strlen($namespacePrefix) + 1); } else { continue; diff --git a/src/utils.php b/src/utils.php index 04553ce..636b41e 100644 --- a/src/utils.php +++ b/src/utils.php @@ -171,8 +171,3 @@ function getVendorDir(\stdClass $composerJson = null): string { return $composerJson->config->{'vendor-dir'} ?? 'vendor'; } - -function strStartsWith(string $haystack, string $prefix): bool -{ - return substr($haystack, 0, strlen($prefix)) === $prefix; -} diff --git a/tests/Utils/UtilsTest.php b/tests/Utils/UtilsTest.php deleted file mode 100644 index 234bf0c..0000000 --- a/tests/Utils/UtilsTest.php +++ /dev/null @@ -1,35 +0,0 @@ -assertEquals(strStartsWith($haystack, $prefix), $expectedResult); - } -}