Skip to content
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
25 changes: 25 additions & 0 deletions app/Contexts/Variable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Contexts;

class Variable extends AbstractContext
{
public ?string $name = null;

public ?string $className = null;

protected bool $hasChildren = false;

public function type(): string
{
return 'variable';
}

public function castToArray(): array
{
return [
'name' => $this->name,
'className' => $this->className,
];
}
}
5 changes: 5 additions & 0 deletions app/Parsers/MethodDeclarationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public function parse(MethodDeclaration $node)
{
$this->context->methodName = $node->getName();

// Every method is a new context, so we need to clear
// the previous variable contexts
// @see https://github.com/laravel/vs-code-php-parser-cli/pull/14
VariableParser::$previousContexts = [];

return $this->context;
}

Expand Down
174 changes: 174 additions & 0 deletions app/Parsers/VariableParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

namespace App\Parsers;

use App\Contexts\AbstractContext;
use App\Contexts\Variable as VariableContext;
use App\Parser\Settings;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\Expression\Variable;
use Microsoft\PhpParser\Node\NamespaceUseClause;
use Microsoft\PhpParser\Node\Statement\NamespaceUseDeclaration;
use Microsoft\PhpParser\PositionUtilities;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\ParserConfig;

class VariableParser extends AbstractParser
{
/**
* @var VariableContext
*/
protected AbstractContext $context;

public static array $previousContexts = [];

private function createPhpDocParser(ParserConfig $config): PhpDocParser
{
$constExprParser = new ConstExprParser($config);
$typeParser = new TypeParser($config, $constExprParser);

return new PhpDocParser($config, $typeParser, $constExprParser);

}

private function getLatestDocComment(Node $node): ?string
{
$docComment = $node->getDocCommentText();

if ($docComment === null && $node->getParent() !== null) {
return $this->getLatestDocComment($node->getParent());
}

return $docComment;
}

private function searchClassNameInParameter(): ?string
{
$className = $this->context->searchForVar($this->context->name);

return is_string($className) ? $className : null;
}

private function searchClassNameInDocComment(Node $node): ?string
{
$docComment = $this->getLatestDocComment($node);

if ($docComment === null) {
return null;
}

$config = new ParserConfig([]);
$lexer = new Lexer($config);
$phpDocParser = $this->createPhpDocParser($config);

$tokens = new TokenIterator($lexer->tokenize($docComment));
$phpDocNode = $phpDocParser->parse($tokens);

$varTagValues = $phpDocNode->getVarTagValues();

/** @var VarTagValueNode|null $varTagValue */
$varTagValue = collect($varTagValues)
// We need to remove first character because it's always $
->first(fn (VarTagValueNode $valueNode) => substr($valueNode->variableName, 1) === $this->context->name);

if (! $varTagValue?->type instanceof IdentifierTypeNode) {
return null;
}

// If the class name starts with a backslash, it's a fully qualified name
if (str_starts_with($varTagValue->type->name, '\\')) {
return substr($varTagValue->type->name, 1);
}

// Otherwise, it's a short name and we need to find the fully qualified name from
// the imported namespaces
$uses = [];

foreach ($node->getRoot()->getDescendantNodes() as $node) {
if (! $node instanceof NamespaceUseDeclaration) {
continue;
}

foreach ($node->useClauses->children ?? [] as $clause) {
if (! $clause instanceof NamespaceUseClause) {
continue;
}

$fqcn = $clause->namespaceName->getText();

// If the namespace has an alias, we need to use the alias as the short name
$alias = $clause->namespaceAliasingClause
? str($clause->namespaceAliasingClause->getText())
->after('as')
->trim()
->toString()
: str($fqcn)->explode('\\')->last();

// Finally, we add the short and fully qualified name to the uses array
$uses[$alias] = $fqcn;
}
}

return $uses[$varTagValue->type->name] ?? null;
}

private function searchClassNameInPreviousContexts(): ?string
{
/** @var VariableContext|null $previousVariableContext */
$previousVariableContext = collect(self::$previousContexts)
->last(fn (VariableContext $context) => $context->name === $this->context->name);

return $previousVariableContext?->className;
}

public function parse(Variable $node)
{
$this->context->name = $node->getName();

// Firstly, we try to find the className from the method parameter
$this->context->className = $this->searchClassNameInParameter()
// If the className is still not found, we try to find the className
// from the doc comment, for example:
//
// /** @var \App\Models\User $user */
// Gate::allows('edit', $user);
?? $this->searchClassNameInDocComment($node)
// If the className is still not found, we try to find the className
// from the previous variable contexts, for example:
//
// /** @var \App\Models\User $user */
// $user = $request->user;
//
// Gate::allows('edit', $user);
?? $this->searchClassNameInPreviousContexts();

if (Settings::$capturePosition) {
$range = PositionUtilities::getRangeFromPosition(
$node->getStartPosition(),
mb_strlen($node->getText()),
$node->getRoot()->getFullText(),
);

if (Settings::$calculatePosition !== null) {
$range = Settings::adjustPosition($range);
}

$this->context->setPosition($range);
}

array_push(self::$previousContexts, $this->context);

return $this->context;
}

public function initNewContext(): ?AbstractContext
{
return new VariableContext;
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"illuminate/log": "^11.5",
"laravel-zero/framework": "^11.0.0",
"microsoft/tolerant-php-parser": "^0.1.2",
"phpstan/phpdoc-parser": "^2.3",
"stillat/blade-parser": "^1.10"
},
"require-dev": {
Expand Down
96 changes: 48 additions & 48 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.