|
| 1 | +/** |
| 2 | + * copyright @ctf0 https://github.com/ctf0 |
| 3 | + * ================================================ |
| 4 | + * read the namespaces using `namespaceResolver.checkForNamespaces.classMapFileGlob` |
| 5 | + * read the php_built-in classes using `namespaceResolver.php.builtIns` |
| 6 | + * use regex to search for any class imports `FQN, partial FQN` project wide excluding folders we dont care about using `ripgrep` |
| 7 | + * filter the statements that we already know about (using the data we have from `autoload_classmap` & `php_built-in`) |
| 8 | + * add the unknown statements to the diagnose panel |
| 9 | + */ |
| 10 | + |
| 11 | +import escapeStringRegexp from 'escape-string-regexp'; |
| 12 | +import { execa } from 'execa'; |
| 13 | +import * as vscode from 'vscode'; |
| 14 | +import Resolver from './Resolver'; |
| 15 | +const _groupBy = require('lodash.groupby'); |
| 16 | + |
| 17 | +export default async function checkForNamespaces(resolver: Resolver, createDiagnosticCollection: vscode.DiagnosticCollection) { |
| 18 | + if (resolver.CWD) { |
| 19 | + vscode.window.withProgress({ |
| 20 | + location : vscode.ProgressLocation.Notification, |
| 21 | + cancellable : false, |
| 22 | + title : 'Please Wait', |
| 23 | + }, async () => { |
| 24 | + try { |
| 25 | + const autoloadFilesData = [...await getAutoloadFileData(resolver)] |
| 26 | + .map((item) => item.namespace) |
| 27 | + .concat(resolver.BUILT_IN_CLASSES) |
| 28 | + .flat(); |
| 29 | + |
| 30 | + const filesWithUseStatements: any = await searchAllFilesForImports(resolver); |
| 31 | + const nonFoundNamespaces = findNonFoundNamespaces( |
| 32 | + [...new Set(autoloadFilesData)], |
| 33 | + filesWithUseStatements, |
| 34 | + ); |
| 35 | + |
| 36 | + const list: any = []; |
| 37 | + |
| 38 | + for (const item of nonFoundNamespaces) { |
| 39 | + list.push([ |
| 40 | + vscode.Uri.file(item.file.replace(new RegExp('^\.', 'm'), resolver.CWD)), |
| 41 | + [createDiagnostic(item)], |
| 42 | + ]); |
| 43 | + } |
| 44 | + |
| 45 | + createDiagnosticCollection.set(list); |
| 46 | + |
| 47 | + await vscode.commands.executeCommand('workbench.panel.markers.view.focus'); |
| 48 | + } catch (error) { |
| 49 | + console.error(error); |
| 50 | + } |
| 51 | + }); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +async function getAutoloadFileData(resolver: Resolver): Promise<any> { |
| 56 | + const classMapFileGlob = resolver.config('checkForNamespaces.classMapFileGlob'); |
| 57 | + |
| 58 | + if (!classMapFileGlob) { |
| 59 | + throw new Error('config required : classMapFileGlob'); |
| 60 | + } |
| 61 | + |
| 62 | + const files = await vscode.workspace.findFiles(classMapFileGlob, null); |
| 63 | + |
| 64 | + const _data: any = await Promise |
| 65 | + .all( |
| 66 | + files.map(async (file) => { |
| 67 | + const fPath = file.path.replace(`${resolver.CWD}/`, ''); |
| 68 | + |
| 69 | + return Object |
| 70 | + .entries(await resolver.runPhpCli(`include("${fPath}")`)) |
| 71 | + .map(([key, value]) => { |
| 72 | + const file: any = value; |
| 73 | + |
| 74 | + return { |
| 75 | + namespace: key, |
| 76 | + // file : file, |
| 77 | + // name : resolver.getFileNameFromPath(file), |
| 78 | + }; |
| 79 | + }); |
| 80 | + }), |
| 81 | + ); |
| 82 | + |
| 83 | + return _data.flat(); |
| 84 | +} |
| 85 | + |
| 86 | +async function searchAllFilesForImports(resolver: Resolver): Promise<any> { |
| 87 | + const rgCommand = resolver.config('checkForNamespaces.rg.command'); |
| 88 | + const rgExcludeDirs = resolver.config('checkForNamespaces.rg.excludeDirs').map((item) => escapeStringRegexp(item)).join(','); |
| 89 | + |
| 90 | + if (!rgCommand || !rgExcludeDirs) { |
| 91 | + throw new Error('config required : rgCommand,rgExcludeDirs'); |
| 92 | + } |
| 93 | + |
| 94 | + const { stdout } = await execa(rgCommand, [ |
| 95 | + "'((namespace|use) )?\\\\?(\\w+\\\\)+\\w+(?=[ ;:\\(])'", |
| 96 | + '--mmap', |
| 97 | + '--pcre2', |
| 98 | + '--no-messages', |
| 99 | + '--line-number', |
| 100 | + '--only-matching', |
| 101 | + "--glob='!*blade.php'", |
| 102 | + `--glob='!{${rgExcludeDirs}}'`, |
| 103 | + "--glob='**/*.php'", |
| 104 | + "./", |
| 105 | + ], { |
| 106 | + cwd : resolver.CWD, |
| 107 | + shell : vscode.env.shell, |
| 108 | + }); |
| 109 | + |
| 110 | + const uses: any = []; |
| 111 | + |
| 112 | + return stdout |
| 113 | + .split('\n') |
| 114 | + .filter((line) => !line.includes(':namespace ')) |
| 115 | + .map((item) => { |
| 116 | + const matches: any = item.split(':'); |
| 117 | + const _file = matches[0]; |
| 118 | + const _import = matches[2].replace(new RegExp('^\\\\', 'm'), ''); // \App > App |
| 119 | + |
| 120 | + /* -------------------------------------------------------------------------- */ |
| 121 | + if (_import.startsWith('use ') && !uses.includes(_import)) { |
| 122 | + uses.push({ |
| 123 | + file : _file, |
| 124 | + import : _import, |
| 125 | + found : 0, |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + if (!_import.startsWith('use ')) { |
| 130 | + const classBaseName: any = new RegExp(/\w+(?=\\)/).exec(_import); |
| 131 | + |
| 132 | + if (classBaseName !== null) { |
| 133 | + const found = uses.find((_use) => _use.import.endsWith(classBaseName[0]) && _use.file === _file); |
| 134 | + |
| 135 | + if (found) { |
| 136 | + uses.find((_use) => { |
| 137 | + if (_use.file === _file) { |
| 138 | + _use.found++; |
| 139 | + |
| 140 | + return true; |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + return; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + /* -------------------------------------------------------------------------- */ |
| 149 | + |
| 150 | + return { |
| 151 | + file : _file, |
| 152 | + line : parseInt(matches[1]), |
| 153 | + import : _import.replace('use ', ''), |
| 154 | + }; |
| 155 | + }) |
| 156 | + .map((obj: any) => { |
| 157 | + if (obj && uses.find((_use) => _use.file === obj.file && _use.found > 0)) { |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + return obj; |
| 162 | + }) |
| 163 | + .filter((e) => e); |
| 164 | +} |
| 165 | + |
| 166 | +function findNonFoundNamespaces(appNamespaces, importsFiles) { |
| 167 | + importsFiles = _groupBy(importsFiles, 'import'); |
| 168 | + const list: any = []; |
| 169 | + |
| 170 | + for (const namespace of Object.keys(importsFiles)) { |
| 171 | + if (!appNamespaces.includes(namespace)) { |
| 172 | + list.push(importsFiles[namespace]); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return list.flat(); |
| 177 | +} |
| 178 | + |
| 179 | +function createDiagnostic(item) { |
| 180 | + const line = item.line - 1; |
| 181 | + const diagnostic = new vscode.Diagnostic( |
| 182 | + new vscode.Range(line, 0, line, 0), |
| 183 | + `unknown namespace : ${item.import}`, |
| 184 | + vscode.DiagnosticSeverity.Warning, |
| 185 | + ); |
| 186 | + |
| 187 | + diagnostic.source = 'PHP Namespace Resolver'; |
| 188 | + |
| 189 | + return diagnostic; |
| 190 | +} |
0 commit comments