Skip to content

Add a php parser to more correctly parse classes #65

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 2 commits into
base: master
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
out/*
36 changes: 36 additions & 0 deletions package-lock.json

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

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Mehedi Hassan <[email protected]>",
"license": "SEE LICENSE IN LICENSE",
"engines": {
"vscode": "^1.15.0"
"vscode": "^1.34.0"
},
"categories": [
"Other"
Expand All @@ -33,7 +33,7 @@
"onCommand:namespaceResolver.highlightNotUsed",
"onCommand:namespaceResolver.generateNamespace"
],
"main": "./src/extension",
"main": "./out/extension.js",
"icon": "images/icon.png",
"contributes": {
"menus": {
Expand Down Expand Up @@ -208,13 +208,13 @@
"bugs": {
"url": "https://github.com/MehediDracula/PHP-Namespace-Resolver/issues"
},
"scripts": {
"postinstall": "node ./node_modules/vscode/bin/install"
},
"devDependencies": {
"vscode": "^1.0.0"
"@types/node": "^8.10.25",
"@types/vscode": "^1.34.0",
"typescript": "^3.5.1"
},
"dependencies": {
"node-natural-sort": "^0.8.6"
"node-natural-sort": "^0.8.6",
"php-parser": "^3.0.0-prerelease.8"
}
}
22 changes: 22 additions & 0 deletions src/ClassInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Position } from 'vscode';
import { Location } from './Location';
export class ClassInfo {
public name: string;
public location: Location;
constructor(parsedClassObject: any) {
this.name = parsedClassObject.name;
this.location = parsedClassObject.loc;
}
get startPosition(): Position {
return new Position(this.location.start.line - 1, this.location.start.column);
}
get endPosition(): Position {
return new Position(this.location.end.line - 1, this.location.end.column);
}
get baseName(): string {
return this.name.split('\\').pop();
}
public hasSameBaseName(classInfo: ClassInfo): boolean {
return this.baseName === classInfo.baseName;
}
}
12 changes: 12 additions & 0 deletions src/Location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type Location = {
start: {
line: number;
column: number;
offset: number;
};
end: {
line: number;
column: number;
offset: number;
};
};
5 changes: 5 additions & 0 deletions src/ParsedResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ClassInfo } from "./ClassInfo";
export type ParsedResult = {
useStatements: ClassInfo[];
classesUsed: ClassInfo[];
};
156 changes: 156 additions & 0 deletions src/Parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import phpParser from 'php-parser';
import { ParsedResult } from './ParsedResult';
import { ClassInfo } from './ClassInfo';


export class Parser {
private text: string;
private parser: any;
public constructor(text: string) {
this.text = text;

this.parser = new phpParser({
parser: {
extractDoc: false,
php7: true
},
ast: {
withPositions: true
}
});
}

public parse(): ParsedResult {
return this.getClasses();
}

private getClasses(): ParsedResult {
let parsedCode = this.parser.parseCode(this.text, '');

return {
useStatements: this.getUseStatements(parsedCode),
classesUsed: this.getClassesInBody(parsedCode)
};
}

private getClassesInBody(parsedCode: object): ClassInfo[] {
let allClasses = [];
this.getBodyElements(parsedCode).forEach(row => {
if (this.isObject(row)) {
allClasses.push(...this.getClassesForObject(row));
}
});

allClasses = this.filterOutFunctions(allClasses);

return allClasses.concat(this.getExtendedClasses(parsedCode));
}

private getExtendedClasses(parsedCode: any): ClassInfo[] {
const classBody = this.getClass(parsedCode);
if (!classBody) {
return [];
}
const classes = [];

if ((classBody.extends)) {
classes.push(new ClassInfo(classBody.extends));
}

if (Array.isArray(classBody.implements)) {
classes.push(...classBody.implements.map(row => {
return new ClassInfo(row);
}));
}
return classes;
}

private filterOutFunctions(classes: ClassInfo[]): ClassInfo[] {
return classes.filter(row => {
const charAfterClass = this.text.substring(row.location.end.offset, row.location.end.offset + 1);
if (charAfterClass === '(') {
const charsBeforeClass = this.text.substring(row.location.start.offset - 4, row.location.start.offset);
if (charsBeforeClass !== 'new ') {
return false;
}
}
return true;
});
}

private getClassesForObject(row: Record<string, any>): ClassInfo[] {
const classes: ClassInfo[] = [];

Object.entries(row).forEach(([key, value]) => {
if (key === 'kind' && value === 'classreference') {
classes.push(new ClassInfo(row));
} else if (Array.isArray(value)) {
value.forEach(row => {
if (this.isObject(row)) {
classes.push(...this.getClassesForObject(row));
}
});
} else if (this.isObject(value)) {
classes.push(...this.getClassesForObject(value));
}
});
return classes;
}

private isObject(value: any) {
if (value === null) {
return false;
}

return typeof value === 'object';
}

private getElements(parsedCode: any): any[] {
let children = parsedCode.children;

const nameSpaceObject = children.find(row => {
return row.kind === 'namespace';
});
if (nameSpaceObject) {
return nameSpaceObject.children;
}
return children;
}

private getClass(parsedCode: object): any | null {
const bodyType = this.getElements(parsedCode).find(row => {
return row.kind === 'class';
});

return bodyType;
}

private getBodyElements(parsedCode: any): any[] {
const classObject = this.getClass(parsedCode);

if (classObject) {
return classObject.body;
}

const returnType = parsedCode.children.find(row => {
return row.kind === 'return';
});

if (returnType) {
return returnType.expr.items;
}
return [];
}

private getUseStatements(parsedCode: object): ClassInfo[] {
return this.getElements(parsedCode).flatMap(child => {
if (child.kind === 'usegroup') {
return child.items.map(item => {
return new ClassInfo(item);
});
}
return [];
});
}

}
Loading