Skip to content

Add ArrayValue to search for autocompleting #16

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions app/Contexts/AbstractContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ abstract class AbstractContext
{
public array $children = [];

/**
* Whether this context can be found as last result
* in findAutocompleting method
*/
public bool $findable = false;

public bool $autocompleting = false;

protected array $freshObject;
Expand Down Expand Up @@ -48,20 +54,20 @@ public function flip()
public function findAutocompleting(?AbstractContext $context = null)
{
$context = $context ?? $this;
$result = $this->seachForAutocompleting($context, true);
$result = $this->searchForAutocompleting($context, true);
$lastResult = null;

while ($result !== null) {
$lastResult = $result;
$result = $this->seachForAutocompleting($result);
$result = $this->searchForAutocompleting($result);
}

return $lastResult;
}

protected function seachForAutocompleting(AbstractContext $context, $checkCurrent = false)
protected function searchForAutocompleting(AbstractContext $context, $checkCurrent = false)
{
if ($checkCurrent && $context->autocompleting && ($context instanceof MethodCall || $context instanceof ObjectValue)) {
if ($checkCurrent && $context->autocompleting && $context->findable) {
return $context;
}

Expand Down
2 changes: 2 additions & 0 deletions app/Contexts/MethodCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class MethodCall extends AbstractContext
{
public bool $findable = true;

public ?string $methodName = null;

public ?string $className = null;
Expand Down
2 changes: 2 additions & 0 deletions app/Contexts/ObjectValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class ObjectValue extends AbstractContext
{
public bool $findable = true;

public ?string $className = null;

public Arguments $arguments;
Expand Down
23 changes: 23 additions & 0 deletions app/Parsers/ArrayCreationExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use App\Contexts\AbstractContext;
use App\Contexts\ArrayValue;
use Microsoft\PhpParser\MissingToken;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\Expression\ArrayCreationExpression;
use Microsoft\PhpParser\Node\Expression\CallExpression;
use Microsoft\PhpParser\Node\Expression\ObjectCreationExpression;

class ArrayCreationExpressionParser extends AbstractParser
{
Expand All @@ -14,8 +17,28 @@ class ArrayCreationExpressionParser extends AbstractParser
*/
protected AbstractContext $context;

private function isParentNode(Node $node, array $nodeClasses): bool
{
if ($node->getParent() !== null) {
if (in_array(get_class($node->getParent()), $nodeClasses)) {
return true;
}

return $this->isParentNode($node->getParent(), $nodeClasses);
}

return false;
}

public function parse(ArrayCreationExpression $node)
{
// If array is inside a method, for example Validator::validate(['
// then we need to ignore findable for ArrayValue because
// priority is given to App\Contexts\MethodCall or App\Contexts\ObjectValue
if (!$this->isParentNode($node, [CallExpression::class, ObjectCreationExpression::class])) {
$this->context->findable = true;
}

$this->context->autocompleting = $node->closeParenOrBracket instanceof MissingToken;

return $this->context;
Expand Down