Skip to content

Fix namespace generation issue when project name contains one of the psr4 namespace #66 #67

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 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions src/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,17 @@ class Resolver {
}

async generateNamespace() {
let currentFile = this.activeEditor().document.uri.path;
let currentUri = this.activeEditor().document.uri;
let currentFile = currentUri.path;
let currentPath = currentFile.substr(0, currentFile.lastIndexOf('/'));
let composerFile = await vscode.workspace.findFiles('composer.json');
let projectFolder = await vscode.workspace.getWorkspaceFolder(currentUri);

if (projectFolder === undefined) {
return this.showErrorMessage('No project folder found, automatic namespace generation failed');
}

let projectPath = projectFolder.uri.path;

if (! composerFile.length) {
return this.showErrorMessage('No composer.json file found, automatic namespace generation failed');
Expand All @@ -635,15 +643,17 @@ class Resolver {
psr4 = {...psr4, ...devPsr4};
}

let currentRelativePath = currentPath.split(projectPath)[1];

let namespaceBase = Object.keys(psr4).filter(function (namespaceBase) {
return currentPath.split(psr4[namespaceBase])[1];
return currentRelativePath.split(psr4[namespaceBase])[1];
}).concat(Object.keys(psr4))[0];

let baseDir = psr4[namespaceBase];

namespaceBase = namespaceBase.replace(/\\$/, '');

let namespace = currentPath.split(baseDir);
let namespace = currentRelativePath.split(baseDir);

if (namespace[1]) {
namespace = namespace[1]
Expand Down