Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intellisense autocomplete also suggests private or protected method and properties #469

Open
zodiark23 opened this issue Aug 24, 2017 · 6 comments · May be fixed by #682
Open

Intellisense autocomplete also suggests private or protected method and properties #469

zodiark23 opened this issue Aug 24, 2017 · 6 comments · May be fixed by #682

Comments

@zodiark23
Copy link

Just like said in the title it also suggest the methods that is declared private/public

image

Here is the class implementing it
image

Although php will also throw an error for this might be better if the intellisense won't suggest anymore.

@duplicate-issues
Copy link

Hey @zodiark23,

We did a quick check and this issue looks very darn similar to

This could be a coincidence, but if any of these issues solves your problem then I did a good job 😄

If not, the maintainers will get to this issue shortly.

Cheers,
Your Friendly Neighborhood ProBot

@raheelkhan
Copy link

I also face the same issue, and in the first glance i feel that there is no check specifically to check the access modifiers. May be it is there which i couldn't understand. I am trying to implement this. If I complete will ask to merge.

@gnoe
Copy link

gnoe commented Nov 9, 2018

Hi, I was checking this issue and I came up with a solution. I added this to the \LanguageServer\Definition class
/**
* Checks the definition's visibility.
*
* @return bool
*/
public function isVisible(string $match, string $caller, \Microsoft\PhpParser\Node $node): bool
{
$ancestor = $node->getFirstAncestor(\Microsoft\PhpParser\Node\MethodDeclaration::class);
if ($ancestor) {
if ($match !== $caller && $this->isPrivate()) {
return false;
}
} else if ($this->isProtected() || $this->isPrivate()) {
return false;
}
return true;
}

private function isPrivate(): bool
{
    return 'private' === substr($this->declarationLine, 0, 7);
}

private function isProtected(): bool
{
    return 'protected' === substr($this->declarationLine, 0, 9);
}

@gnoe
Copy link

gnoe commented Nov 9, 2018

Also, the \LanguageServer\CompletionProvider must be modified too.

Line: 245
// Collect all definitions that match any of the prefixes
foreach ($this->index->getDefinitions() as $fqn => $def) {
foreach ($prefixes as $prefix) {
if (substr($fqn, 0, strlen($prefix)) === $prefix &&
$def->isMember &&
$def->isVisible($prefix, $prefixes[0], $node)) {
$list->items[] = CompletionItem::fromDefinition($def);
}
}
}

@felixfbecker
Copy link
Owner

felixfbecker commented Nov 9, 2018

@gnoe want to submit a pull request? https://help.github.com/articles/creating-a-pull-request/

@gnoe
Copy link

gnoe commented Nov 9, 2018

@felixfbecker, yes, I am going to do it!

@gnoe gnoe linked a pull request Nov 11, 2018 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants