Skip to content
Merged
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
26 changes: 26 additions & 0 deletions src/languagePlugins/php/invocationResolver/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, test } from "@std/testing/bdd";
import { expect } from "@std/expect";
import { PHPInvocationResolver } from "./index.ts";
import { PHPIncluseResolver } from "../incluseResolver/index.ts";
import { PHPRegistree } from "../registree/index.ts";
import { getPHPFilesMap } from "../testFiles/index.ts";
import { INVOCATIONS } from "../testFiles/constants.ts";

describe("PHP Invocation resolver", () => {
const files = getPHPFilesMap();
const registree = new PHPRegistree(files);
const incluseres = new PHPIncluseResolver(registree);
const resolver = new PHPInvocationResolver(incluseres);

test("resolves invocations", () => {
const invocations = resolver.getInvocationsForFile(INVOCATIONS);
expect(invocations.unresolved.size >= 1).toBe(true);
expect(invocations.unresolved).toContainEqual("doesnotexistlmao");
const resolved = Array.from(invocations.resolved.keys());
expect(resolved).toContainEqual("array");
expect(resolved).toContainEqual("my_function");
expect(resolved).toContainEqual("a");
expect(resolved).toContainEqual("MyClass");
expect(resolved).toContainEqual("All\\My\\Fellas\\f");
});
});
73 changes: 73 additions & 0 deletions src/languagePlugins/php/invocationResolver/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { Invocations } from "./types.ts";
import { PHP_INVOCATION_QUERY } from "./queries.ts";
import type { PHPIncluseResolver } from "../incluseResolver/index.ts";
import type Parser from "tree-sitter";
import type { ExportedSymbol } from "../exportResolver/types.ts";
import { SymbolNode } from "../registree/types.ts";

export class PHPInvocationResolver {
incluseResolver: PHPIncluseResolver;

constructor(incluseResolver: PHPIncluseResolver) {
this.incluseResolver = incluseResolver;
}

getInvocationsForNode(
node: Parser.SyntaxNode,
filepath: string,
symbolname: string | undefined = undefined,
): Invocations {
const currentfile = this.incluseResolver.registree.registry.files.get(
filepath,
)!;
const availableSymbols = this.incluseResolver
.resolveImports(currentfile)?.resolved;
const localSymbols = currentfile.symbols;
const unresolved = new Set<string>();
const resolved = new Map<string, ExportedSymbol[]>();
const captures = PHP_INVOCATION_QUERY.captures(node);
for (const capture of captures) {
const name = capture.node.text;
// if the symbol name is the same as the one we are looking at, skip it
if (symbolname && name === symbolname) {
continue;
}
if (availableSymbols && availableSymbols.has(name)) {
const availableSymbol = availableSymbols.get(name);
if (!availableSymbol) {
unresolved.add(name);
continue;
}
resolved.set(name, availableSymbol);
} else if (localSymbols && localSymbols.has(name)) {
const localSymbol = localSymbols.get(name);
if (!localSymbol) {
unresolved.add(name);
continue;
}
resolved.set(name, localSymbol);
} else if (capture.name === "qualified") {
const qualSymbol = this.incluseResolver.registree.tree.findNode(name);
if (qualSymbol && qualSymbol instanceof SymbolNode) {
resolved.set(name, qualSymbol.symbols);
} else {
unresolved.add(name);
}
} else {
unresolved.add(name);
}
}
return {
resolved,
unresolved,
};
}

getInvocationsForFile(filepath: string): Invocations {
const file = this.incluseResolver.registree.registry.files.get(filepath);
if (!file) {
throw new Error(`File not found: ${filepath}`);
}
return this.getInvocationsForNode(file.rootNode, file.path);
}
}
10 changes: 10 additions & 0 deletions src/languagePlugins/php/invocationResolver/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Parser from "tree-sitter";
import { phpParser } from "../../../helpers/treeSitter/parsers.ts";

export const PHP_INVOCATION_QUERY = new Parser.Query(
phpParser.getLanguage(),
`
(name) @name
(qualified_name) @qualified
`,
);
6 changes: 6 additions & 0 deletions src/languagePlugins/php/invocationResolver/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { ExportedSymbol } from "../exportResolver/types.ts";

export interface Invocations {
resolved: Map<string, ExportedSymbol[]>;
unresolved: Set<string>;
}
1 change: 1 addition & 0 deletions src/languagePlugins/php/testFiles/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const LEARN_PHP = join(phpFilesFolder, "learnphp.php");
export const NESTED = join(phpFilesFolder, "nested.php");
export const INCLUDE = join(phpFilesFolder, "include.php");
export const USE = join(phpFilesFolder, "use.php");
export const INVOCATIONS = join(phpFilesFolder, "invocations.php");
12 changes: 12 additions & 0 deletions src/languagePlugins/php/testFiles/phpFiles/invocations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace NewMexico\Albuquerque;
require 'learnphp.php';

$one = $array[0];
echo my_function();
$ahundred = $a;
$walter = new MyClass('Walter');

$error = $doesnotexistlmao;

class Product implements All\My\Fellas\f {}