Skip to content

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

Open
@zodiark23

Description

@zodiark23

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.

Activity

duplicate-issues

duplicate-issues commented on Aug 24, 2017

@duplicate-issues

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

raheelkhan commented on Nov 13, 2017

@raheelkhan

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

gnoe commented on Nov 9, 2018

@gnoe

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

gnoe commented on Nov 9, 2018

@gnoe

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

felixfbecker commented on Nov 9, 2018

@felixfbecker
Owner
gnoe

gnoe commented on Nov 9, 2018

@gnoe

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @raheelkhan@felixfbecker@gnoe@zodiark23

      Issue actions

        Intellisense autocomplete also suggests private or protected method and properties · Issue #469 · felixfbecker/php-language-server